mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-03 18:02:28 +02:00
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>
This commit is contained in:
parent
2f31d2d56c
commit
491cc06ffe
3 changed files with 54 additions and 26 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue