Spaces:
Runtime error
Runtime error
Download middleware/authentication_middleware.go from lifedebugger/quzuu-api-dev-v2: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/lifedebugger/quzuu-api-dev-v2/resolve/main/middleware/authentication_middleware.go
- Command line
-
hf download hf://spaces/lifedebugger/quzuu-api-dev-v2/middleware/authentication_middleware.go
-
curl -L -o authentication_middleware.go https://huggingface.co/spaces/lifedebugger/quzuu-api-dev-v2/resolve/main/middleware/authentication_middleware.go
1.21 kB
| package middleware | |
| import ( | |
| "errors" | |
| "fmt" | |
| "strings" | |
| http_error "abdanhafidz.com/go-boilerplate/models/error" | |
| "abdanhafidz.com/go-boilerplate/services" | |
| utils "abdanhafidz.com/go-boilerplate/utils" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| type AuthenticationMiddleware interface { | |
| VerifyAccount(ctx *gin.Context) | |
| } | |
| type authenticationMiddleware struct { | |
| jwtService services.JWTService | |
| } | |
| func NewAuthenticationMiddleware(jwtService services.JWTService) AuthenticationMiddleware { | |
| return &authenticationMiddleware{ | |
| jwtService: jwtService, | |
| } | |
| } | |
| func (m *authenticationMiddleware) VerifyAccount(c *gin.Context) { | |
| authorizationBearer := c.Request.Header["Authorization"] | |
| if authorizationBearer != nil { | |
| token := strings.Split(authorizationBearer[0], " ")[1] | |
| claim, err := m.jwtService.ValidateToken(c.Request.Context(), token) | |
| if err != nil && errors.Is(err, http_error.INVALID_TOKEN) { | |
| utils.ResponseFAILED(c, claim, http_error.INVALID_TOKEN) | |
| c.Abort() | |
| return | |
| } | |
| fmt.Println("Claims:", claim) | |
| c.Set("account_id", claim.AccountId) | |
| c.Next() | |
| } else { | |
| utils.ResponseFAILED(c, "Empty Token", http_error.UNAUTHORIZED) | |
| return | |
| } | |
| } | |