From 8077a2ccba453a6701bc0f3e11e94f5f5ce74d93 Mon Sep 17 00:00:00 2001 From: Leon Koth Date: Thu, 27 Jun 2024 15:24:54 +0200 Subject: [PATCH] feat: fix bug for access key and secret login (#3022) * fix: get username for keys * chore: move user nil check --- routers/authz_filter.go | 13 +++---------- routers/base.go | 15 ++++++++++----- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/routers/authz_filter.go b/routers/authz_filter.go index e2897f13..3794a9a3 100644 --- a/routers/authz_filter.go +++ b/routers/authz_filter.go @@ -35,20 +35,13 @@ type Object struct { } func getUsername(ctx *context.Context) (username string) { - defer func() { - if r := recover(); r != nil { - username, _ = getUsernameByClientIdSecret(ctx) - } - }() - - username = ctx.Input.Session("username").(string) - - if username == "" { + username, ok := ctx.Input.Session("username").(string) + if !ok || username == "" { username, _ = getUsernameByClientIdSecret(ctx) } if username == "" { - username = getUsernameByKeys(ctx) + username, _ = getUsernameByKeys(ctx) } return } diff --git a/routers/base.go b/routers/base.go index d4b923a9..82498afc 100644 --- a/routers/base.go +++ b/routers/base.go @@ -91,17 +91,22 @@ func getUsernameByClientIdSecret(ctx *context.Context) (string, error) { return fmt.Sprintf("app/%s", application.Name), nil } -func getUsernameByKeys(ctx *context.Context) string { +func getUsernameByKeys(ctx *context.Context) (string, error) { accessKey, accessSecret := getKeys(ctx) user, err := object.GetUserByAccessKey(accessKey) if err != nil { - panic(err) + return "", err } - if user != nil && accessSecret == user.AccessSecret { - return user.GetId() + if user == nil { + return "", fmt.Errorf("user not found for access key: %s", accessKey) } - return "" + + if accessSecret != user.AccessSecret { + return "", fmt.Errorf("incorrect access secret for user: %s", user.Name) + } + + return user.GetId(), nil } func getSessionUser(ctx *context.Context) string {