diff options
author | caicandong <50507092+CaiCandong@users.noreply.github.com> | 2023-07-11 10:04:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 10:04:28 +0800 |
commit | 491cc06ffe3491242ad9ff6227423d99e673d0c2 (patch) | |
tree | 66a9bd28de389fed613c7ff8fe14b4f07935165d /services/auth/group.go | |
parent | 2f31d2d56c22400b2e79b279a5d0e845febba137 (diff) | |
download | gitea-491cc06ffe3491242ad9ff6227423d99e673d0c2.tar.gz gitea-491cc06ffe3491242ad9ff6227423d99e673d0c2.zip |
Fix the error message when the token is incorrect (#25701)
we refactored `userIDFromToken` for the token parsing part into a new
function `parseToken`. `parseToken` returns the string `token` from
request, and a boolean `ok` representing whether the token exists or
not. So we can distinguish between token non-existence and token
inconsistency in the `verfity` function, thus solving the problem of no
proper error message when the token is inconsistent.
close #24439
related #22119
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'services/auth/group.go')
-rw-r--r-- | services/auth/group.go | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/services/auth/group.go b/services/auth/group.go index a1ff65f203..7193dfcf49 100644 --- a/services/auth/group.go +++ b/services/auth/group.go @@ -49,12 +49,22 @@ func (b *Group) Name() string { // Verify extracts and validates func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) { // Try to sign in with each of the enabled plugins + var retErr error for _, ssoMethod := range b.methods { user, err := ssoMethod.Verify(req, w, store, sess) if err != nil { - return nil, err + if retErr == nil { + retErr = err + } + // Try other methods if this one failed. + // Some methods may share the same protocol to detect if they are matched. + // For example, OAuth2 and conan.Auth both read token from "Authorization: Bearer <token>" header, + // If OAuth2 returns error, we should give conan.Auth a chance to try. + continue } + // If any method returns a user, we can stop trying. + // Return the user and ignore any error returned by previous methods. if user != nil { if store.GetData()["AuthedMethod"] == nil { if named, ok := ssoMethod.(Named); ok { @@ -65,5 +75,6 @@ func (b *Group) Verify(req *http.Request, w http.ResponseWriter, store DataStore } } - return nil, nil + // If no method returns a user, return the error returned by the first method. + return nil, retErr } |