Skip to content

Commit

Permalink
fix login rejection message to be more clear about possible causes (#…
Browse files Browse the repository at this point in the history
…2658)

* move header check to early in the handler, avoid loading users if there is no point

* tweak message to conceal if it was user or password which were bad
  • Loading branch information
andreas-kupries committed Oct 16, 2023
1 parent b312089 commit 684653d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions internal/api/v1/middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func basicAuthentication(ctx *gin.Context) (auth.User, apierrors.APIErrors) {
logger := requestctx.Logger(reqCtx).WithName("basicAuthentication")
logger.V(1).Info("starting Basic Authentication")

// Bail early if the request has no proper credentials embedded into it.
username, password, ok := ctx.Request.BasicAuth()
if !ok {
return auth.User{}, apierrors.NewInternalError("Couldn't extract user or password from the auth header")
}

userMap, err := loadUsersMap(ctx, logger)
if err != nil {
return auth.User{}, apierrors.InternalError(err)
Expand All @@ -79,14 +85,9 @@ func basicAuthentication(ctx *gin.Context) (auth.User, apierrors.APIErrors) {
return auth.User{}, apierrors.NewAPIError("no user found", http.StatusUnauthorized)
}

username, password, ok := ctx.Request.BasicAuth()
if !ok {
return auth.User{}, apierrors.NewInternalError("Couldn't extract user from the auth header")
}

err = bcrypt.CompareHashAndPassword([]byte(userMap[username].Password), []byte(password))
if err != nil {
return auth.User{}, apierrors.NewAPIError("wrong password", http.StatusUnauthorized)
return auth.User{}, apierrors.NewAPIError("wrong user or password", http.StatusUnauthorized)
}

return userMap[username], nil
Expand Down

0 comments on commit 684653d

Please sign in to comment.