From 8474e5ddd3f0f1b643d16623fac1cda937b4368e Mon Sep 17 00:00:00 2001 From: Punarv Pawade Date: Fri, 12 Apr 2024 17:09:48 +0530 Subject: [PATCH] auth: fix middleware --- api/middleware/auth/paseto/paseto.go | 10 ++++------ api/middleware/auth/paseto/types.go | 9 +++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/api/middleware/auth/paseto/paseto.go b/api/middleware/auth/paseto/paseto.go index 5d8e998..1acec48 100644 --- a/api/middleware/auth/paseto/paseto.go +++ b/api/middleware/auth/paseto/paseto.go @@ -32,7 +32,6 @@ func PASETO(authOptional bool) func(*gin.Context) { c.AbortWithStatus(http.StatusInternalServerError) return } - if headers.Authorization == "" { if authOptional { c.Next() @@ -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 { @@ -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() } } diff --git a/api/middleware/auth/paseto/types.go b/api/middleware/auth/paseto/types.go index 0c8e991..3fc2efb 100644 --- a/api/middleware/auth/paseto/types.go +++ b/api/middleware/auth/paseto/types.go @@ -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"` +}