mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-10-15 19:42:04 +00:00
Backport #25701 by @CaiCandong
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: caicandong <50507092+CaiCandong@users.noreply.github.com>
Co-authored-by: Jason Song <i@wolfogre.com>
(cherry picked from commit ee87b4e18c
)
This commit is contained in:
parent
2e6d978065
commit
968ab550e5
3 changed files with 54 additions and 26 deletions
|
@ -81,12 +81,22 @@ func (b *Group) Free() error {
|
|||
// 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 {
|
||||
|
@ -97,5 +107,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