-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MyriadFlow/thisisommore/add-authentication-1
Introduce solana auth integration
- Loading branch information
Showing
19 changed files
with
1,695 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# This configuration file was automatically generated by Gitpod. | ||
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file) | ||
# and commit this file to your remote git repository to share the goodness with others. | ||
|
||
tasks: | ||
- init: go get && go build ./... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package app | ||
|
||
import ( | ||
"collection/dto/dtoapis" | ||
"collection/internal/pkg/errorso" | ||
"collection/logger" | ||
"collection/service" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/TheLazarusNetwork/go-helpers/httpo" | ||
"github.com/gin-gonic/gin" | ||
// "github.com/gorilla/mux" | ||
) | ||
|
||
func (u FlowIdHandler) Authenticate(c *gin.Context) { | ||
var req dtoapis.AuthenticateRequest | ||
err := c.BindJSON(&req) | ||
if err != nil { | ||
httpo.NewErrorResponse(http.StatusBadRequest, "failed to validate body"). | ||
Send(c, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
pasetoToken, err := u.service.VerifySignAndGetPaseto(req.Signature, req.FlowId) | ||
if err != nil { | ||
logger.Errorf("failed to get paseto: %s", err) | ||
|
||
// If signature denied | ||
if errors.Is(err, service.ErrSignDenied) { | ||
httpo.NewErrorResponse(httpo.SignatureDenied, "signature denied"). | ||
Send(c, http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if errors.Is(err, errorso.ErrRecordNotFound) { | ||
httpo.NewErrorResponse(httpo.FlowIdNotFound, "flow id not found"). | ||
Send(c, http.StatusNotFound) | ||
return | ||
} | ||
|
||
// If unexpected error | ||
httpo.NewErrorResponse(500, "failed to verify and get paseto").Send(c, 500) | ||
return | ||
} else { | ||
payload := dtoapis.AuthenticatePayload{ | ||
Token: pasetoToken, | ||
} | ||
httpo.NewSuccessResponse(http.StatusOK, "Token generated successfully", payload). | ||
Send(c, http.StatusOK) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package app | ||
|
||
import ( | ||
"collection/config/envconfig" | ||
"collection/dto/dtoapis" | ||
"collection/logger" | ||
"collection/service" | ||
"net/http" | ||
|
||
"github.com/TheLazarusNetwork/go-helpers/httpo" | ||
"github.com/gin-gonic/gin" | ||
"github.com/streamingfast/solana-go" | ||
// "github.com/gorilla/mux" | ||
) | ||
|
||
type FlowIdHandler struct { | ||
service service.DefaultFlowIdService | ||
} | ||
|
||
func (u FlowIdHandler) GetFlowId(c *gin.Context) { | ||
walletAddress := c.Query("walletAddress") | ||
|
||
if walletAddress == "" { | ||
httpo.NewErrorResponse(http.StatusBadRequest, "wallet address (walletAddress) is required"). | ||
Send(c, http.StatusBadRequest) | ||
return | ||
} | ||
_, err := solana.PublicKeyFromBase58(walletAddress) | ||
if err != nil { | ||
logger.Errorf("failed to get pubkey from wallet address (base58) %s: %s", walletAddress, err) | ||
httpo.NewErrorResponse(httpo.WalletAddressInvalid, "failed to parse wallet address (walletAddress)").Send(c, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
flowId, err := u.service.CreateFlowId(walletAddress) | ||
if err != nil { | ||
logger.Errorf("failed to generate flow id: %s", err) | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occured").Send(c, http.StatusInternalServerError) | ||
return | ||
} | ||
userAuthEULA := envconfig.EnvVars.AUTH_EULA | ||
payload := dtoapis.GetFlowIdPayload{ | ||
FlowId: flowId, | ||
Eula: userAuthEULA, | ||
} | ||
httpo.NewSuccessResponse(http.StatusOK, "Flowid successfully generated", payload).Send(c, http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package envconfig | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/caarlos0/env/v6" | ||
_ "github.com/joho/godotenv/autoload" | ||
) | ||
|
||
type config struct { | ||
PASETO_PRIVATE_KEY string `env:"PASETO_PRIVATE_KEY,required"` | ||
PASETO_EXPIRATION time.Duration `env:"PASETO_EXPIRATION,required"` | ||
|
||
APP_PORT int `env:"APP_PORT,required"` | ||
AUTH_EULA string `env:"AUTH_EULA,required"` | ||
GIN_MODE string `env:"GIN_MODE,required"` | ||
DB_HOST string `env:"DB_HOST,required"` | ||
DB_USERNAME string `env:"DB_USERNAME,required"` | ||
DB_PASSWORD string `env:"DB_PASSWORD,required"` | ||
DB_NAME string `env:"DB_NAME,required"` | ||
DB_PORT int `env:"DB_PORT,required"` | ||
ALLOWED_ORIGIN []string `env:"ALLOWED_ORIGIN,required" envSeparator:","` | ||
SIGNED_BY string `env:"SIGNED_BY,required"` | ||
COLLECTION_PATH string `env:"COLLECTION_PATH,required"` | ||
} | ||
|
||
var EnvVars config = config{} | ||
|
||
func InitEnvVars() { | ||
if err := env.Parse(&EnvVars); err != nil { | ||
log.Fatalf("failed to parse EnvVars: %s", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package domain | ||
|
||
import ( | ||
"collection/dto" | ||
"collection/internal/pkg/errorso" | ||
|
||
_ "github.com/lib/pq" | ||
"gorm.io/gorm" | ||
// _ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
type FlowIdRepositoryDb struct { | ||
client *gorm.DB | ||
} | ||
|
||
func (i *FlowIdRepositoryDb) GetFlowId(flowId string) (*dto.FlowId, error) { | ||
db := i.client | ||
var userFlowId dto.FlowId | ||
res := db.Find(&userFlowId, &dto.FlowId{ | ||
FlowId: flowId, | ||
}) | ||
|
||
if err := res.Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
if res.RowsAffected == 0 { | ||
return nil, errorso.ErrRecordNotFound | ||
} | ||
return &userFlowId, nil | ||
} | ||
|
||
// Adds flow id into database for given wallet Address | ||
func (i *FlowIdRepositoryDb) AddFlowId(walletAddr string, flowId string) error { | ||
db := i.client | ||
err := db.Create(&dto.FlowId{ | ||
WalletAddress: walletAddr, | ||
FlowId: flowId, | ||
}).Error | ||
|
||
return err | ||
} | ||
|
||
func (i *FlowIdRepositoryDb) DeleteFlowId(flowId string) error { | ||
db := i.client | ||
err := db.Delete(&dto.FlowId{ | ||
FlowId: flowId, | ||
}).Error | ||
|
||
return err | ||
} | ||
|
||
func NewFlowIdRepositoryDb(dbCLient *gorm.DB) FlowIdRepositoryDb { | ||
return FlowIdRepositoryDb{dbCLient} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.