Skip to content

Commit

Permalink
fix: unauthorized error should return 401 (#18229)
Browse files Browse the repository at this point in the history
Co-authored-by: Qiu Jian <[email protected]>
  • Loading branch information
swordqiu and Qiu Jian authored Oct 8, 2023
1 parent 8c115ed commit 48df02e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
22 changes: 21 additions & 1 deletion pkg/keystone/tokens/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package tokens

import "yunion.io/x/pkg/errors"
import (
"yunion.io/x/pkg/errors"

"yunion.io/x/onecloud/pkg/httperrors"
)

const (
ErrVerMismatch = errors.Error("version mismatch")
Expand All @@ -31,3 +35,19 @@ const (
ErrInvalidAccessKeyId = errors.Error("invalid access key id")
ErrExpiredAccessKey = errors.Error("expired access key")
)

func init() {
httperrors.RegisterErrorHttpCode(ErrVerMismatch, 401)
httperrors.RegisterErrorHttpCode(ErrProjectDisabled, 401)
httperrors.RegisterErrorHttpCode(ErrUserDisabled, 401)
httperrors.RegisterErrorHttpCode(ErrInvalidToken, 401)
httperrors.RegisterErrorHttpCode(ErrExpiredToken, 401)
httperrors.RegisterErrorHttpCode(ErrInvalidFernetToken, 401)
httperrors.RegisterErrorHttpCode(ErrInvalidAuthMethod, 401)
httperrors.RegisterErrorHttpCode(ErrUserNotFound, 401)
httperrors.RegisterErrorHttpCode(ErrDomainDisabled, 401)
httperrors.RegisterErrorHttpCode(ErrEmptyAuth, 401)
httperrors.RegisterErrorHttpCode(ErrUserNotInProject, 401)
httperrors.RegisterErrorHttpCode(ErrInvalidAccessKeyId, 401)
httperrors.RegisterErrorHttpCode(ErrExpiredAccessKey, 401)
}
7 changes: 7 additions & 0 deletions pkg/keystone/tokens/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"

"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/rbacscope"
"yunion.io/x/sqlchemy"
Expand Down Expand Up @@ -66,6 +67,11 @@ func authenticateTokensV2(ctx context.Context, w http.ResponseWriter, r *http.Re
}
input.Auth.Context = FetchAuthContext(input.Auth.Context, r)
token, err := AuthenticateV2(ctx, input)
if err != nil {
log.Errorf("AuthenticateV2 error %s", err)
httperrors.GeneralServerError(ctx, w, err)
return
}
if token == nil {
httperrors.UnauthorizedError(ctx, w, "unauthorized %s", err)
return
Expand All @@ -90,6 +96,7 @@ func authenticateTokensV3(ctx context.Context, w http.ResponseWriter, r *http.Re
input.Auth.Context = FetchAuthContext(input.Auth.Context, r)
token, err := AuthenticateV3(ctx, input)
if err != nil {
log.Errorf("AuthenticateV3 error %s", err)
switch errors.Cause(err) {
case sqlchemy.ErrDuplicateEntry:
httperrors.ConflictError(ctx, w, "duplicate username")
Expand Down
9 changes: 7 additions & 2 deletions pkg/keystone/tokens/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,12 @@ func (t *SAuthToken) getTokenV3(

if len(roles) == 0 {
if project != nil || domain != nil {
return nil, ErrUserNotInProject
if project != nil {
return nil, errors.Wrapf(ErrUserNotInProject, "project %q", project.Name)
}
if domain != nil {
return nil, errors.Wrapf(ErrUserNotInProject, "domain %q", domain.Name)
}
}
/*extProjs, err := models.ProjectManager.FetchUserProjects(user.Id)
if err != nil {
Expand Down Expand Up @@ -453,7 +458,7 @@ func (t *SAuthToken) getTokenV2(

if len(roles) == 0 {
if project != nil {
return nil, ErrUserNotInProject
return nil, errors.Wrapf(ErrUserNotInProject, "project %q", project.Name)
}
extProjs, err := models.ProjectManager.FetchUserProjects(user.Id)
if err != nil {
Expand Down

0 comments on commit 48df02e

Please sign in to comment.