Skip to content

Commit 98ac397

Browse files
committed
Fix login with oauth2 sometimes return could not find a matching session for this request
1 parent ad749fb commit 98ac397

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

routers/web/auth/oauth.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,9 @@ func SignInOAuth(ctx *context.Context) {
893893

894894
// try to do a direct callback flow, so we don't authenticate the user again but use the valid accesstoken to get the user
895895
user, gothUser, err := oAuth2UserLoginCallback(ctx, authSource, ctx.Req, ctx.Resp)
896+
if ctx.Written() {
897+
return
898+
}
896899
if err == nil && user != nil {
897900
// we got the user without going through the whole OAuth2 authentication flow again
898901
handleOAuth2SignIn(ctx, authSource, user, gothUser)
@@ -943,6 +946,9 @@ func SignInOAuthCallback(ctx *context.Context) {
943946
}
944947

945948
u, gothUser, err := oAuth2UserLoginCallback(ctx, authSource, ctx.Req, ctx.Resp)
949+
if ctx.Written() {
950+
return
951+
}
946952
if err != nil {
947953
if user_model.IsErrUserProhibitLogin(err) {
948954
uplerr := err.(user_model.ErrUserProhibitLogin)
@@ -1275,8 +1281,12 @@ func oAuth2UserLoginCallback(ctx *context.Context, authSource *auth.Source, requ
12751281
if err.Error() == "securecookie: the value is too long" || strings.Contains(err.Error(), "Data too long") {
12761282
log.Error("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", authSource.Name, setting.OAuth2.MaxTokenLength)
12771283
err = fmt.Errorf("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", authSource.Name, setting.OAuth2.MaxTokenLength)
1284+
return nil, goth.User{}, err
1285+
} else {
1286+
log.Error("OAuth2 Provider %s error(start BeginAuthHandler): %v", authSource.Name, err)
1287+
gothic.BeginAuthHandler(response, request)
1288+
return nil, goth.User{}, nil
12781289
}
1279-
return nil, goth.User{}, err
12801290
}
12811291

12821292
if oauth2Source.RequiredClaimName != "" {

0 commit comments

Comments
 (0)