Skip to content

Commit

Permalink
fix: solved error on Authenticator's Authenticate function implementa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
ralvarezdev committed Jan 12, 2025
1 parent 36e19e5 commit 2ace88c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
2 changes: 1 addition & 1 deletion http/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func (d *DefaultHandler) HandleValidations(
w,
gonethttpresponse.NewFailResponse(
validations,
http.StatusBadRequest,
nil,
http.StatusBadRequest,
),
)
}
Expand Down
25 changes: 4 additions & 21 deletions http/middleware/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,19 @@ func (m *Middleware) Authenticate(
return func(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
// Get the context
ctx := r.Context()

// Get the authorization from the header
authorization := ctx.Value(gojwtnethttp.AuthorizationHeaderKey)

// Parse the authorization to a string
authorizationStr, ok := authorization.(string)
if !ok {
m.handler.HandleResponse(
w,
gonethttpresponse.NewErrorResponse(
gonethttp.ErrInvalidAuthorizationHeader,
nil, nil,
http.StatusUnauthorized,
),
)
return
}
authorization := r.Header.Get(gojwtnethttp.AuthorizationHeaderKey)

// Check if the authorization is a bearer token
parts := strings.Split(authorizationStr, " ")
parts := strings.Split(authorization, " ")

// Return an error if the authorization is missing or invalid
if len(parts) < 2 || parts[0] != gojwt.BearerPrefix {
m.handler.HandleResponse(
w,
gonethttpresponse.NewErrorResponse(
gonethttpresponse.NewFailResponse(
gonethttp.ErrInvalidAuthorizationHeader,
nil, nil,
nil,
http.StatusUnauthorized,
),
)
Expand Down
2 changes: 1 addition & 1 deletion http/response/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func NewDebugFailResponse(
// NewFailResponse creates a new fail response
func NewFailResponse(
data interface{},
httpStatus int,
errorCode *int,
httpStatus int,
) *Response {
return NewDebugFailResponse(data, data, errorCode, httpStatus)
}
Expand Down

0 comments on commit 2ace88c

Please sign in to comment.