Skip to content

refactor auth interface to return error when verify failure #22119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 28, 2022
Prev Previous commit
Next Next commit
improvement
  • Loading branch information
lunny committed Dec 13, 2022
commit 0376f236c36c3c20618ea05d710679e6ff269285
2 changes: 1 addition & 1 deletion modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func Auth(authMethod auth.Method) func(*Context) {
ctx.Doer, err = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
if err != nil {
log.Error("Failed to verify user %v: %v", ctx.Req.RemoteAddr, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, we don't log API verification failures?
This line is missing in APIAuth

ctx.Error(401, "Verify")
ctx.Error(http.StatusUnauthorized, "Verify")
return
}
if ctx.Doer != nil {
Expand Down
6 changes: 2 additions & 4 deletions routers/api/packages/container/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ func (a *Auth) Name() string {
func (a *Auth) Verify(req *http.Request, w http.ResponseWriter, store auth.DataStore, sess auth.SessionStore) (*user_model.User, error) {
uid, err := packages.ParseAuthorizationToken(req)
if err != nil {
if err.Error() != "no token" {
log.Trace("ParseAuthorizationToken: %v", err)
return nil, err
}
log.Trace("ParseAuthorizationToken: %v", err)
return nil, err
}

if uid == 0 {
Expand Down
9 changes: 7 additions & 2 deletions services/packages/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ func CreateAuthorizationToken(u *user_model.User) (string, error) {
}

func ParseAuthorizationToken(req *http.Request) (int64, error) {
parts := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
h := req.Header.Get("Authorization")
if h == "" {
return 0, nil
}

parts := strings.SplitN(h, " ", 2)
if len(parts) != 2 {
return 0, fmt.Errorf("no token")
return 0, fmt.Errorf("split token failed: %s", h)
}

token, err := jwt.ParseWithClaims(parts[1], &packageClaims{}, func(t *jwt.Token) (interface{}, error) {
Expand Down