Skip to content

Commit

Permalink
auth: fix middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
inciner8r committed Apr 12, 2024
1 parent dd31fb6 commit 8474e5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 4 additions & 6 deletions api/middleware/auth/paseto/paseto.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func PASETO(authOptional bool) func(*gin.Context) {
c.AbortWithStatus(http.StatusInternalServerError)
return
}

if headers.Authorization == "" {
if authOptional {
c.Next()
Expand All @@ -51,7 +50,6 @@ func PASETO(authOptional bool) func(*gin.Context) {
}

pasetoToken := strings.TrimPrefix(headers.Authorization, "Bearer ")

//auth req to gateway
contractReq, err := http.NewRequest(http.MethodGet, os.Getenv("GATEWAY_URL")+"/api/v1.0/authenticate", nil)
if err != nil {
Expand All @@ -73,16 +71,16 @@ func PASETO(authOptional bool) func(*gin.Context) {
return
}
defer resp.Body.Close()
var responseBody AuthenticateTokenPayload
var responseBody AuthenticateTokenResponse
err = json.NewDecoder(resp.Body).Decode(&responseBody)
if err != nil {
fmt.Printf("Failed to decode response body: %s\n", err)
return
} else {
if responseBody.WalletAddress != "" {
c.Set("CTX_WALLET_ADDRESS", responseBody.WalletAddress)
if responseBody.Payload.WalletAddress != "" {
c.Set(CTX_WALLET_ADDRESS, responseBody.Payload.WalletAddress)
}
c.Set("CTX_USER_ID", responseBody.UserId)
c.Set(CTX_USER_ID, responseBody.Payload.UserID)
c.Next()
}
}
Expand Down
9 changes: 9 additions & 0 deletions api/middleware/auth/paseto/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ type AuthenticateTokenPayload struct {
UserId string `json:"userId"`
WalletAddress string `json:"walletAddress"`
}

type AuthenticateTokenResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Payload struct {
UserID string `json:"userId"`
WalletAddress string `json:"walletAddress"`
} `json:"payload"`
}

0 comments on commit 8474e5d

Please sign in to comment.