Skip to content

Commit e30b20d

Browse files
KN4CK3R6543lafrikslunny
authored
Show OAuth callback error message (#18185)
* Show callback error message. * lint * Use error code to display a message. Co-authored-by: 6543 <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 3dbdf36 commit e30b20d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ oauth_signup_submit = Complete Account
317317
oauth_signin_tab = Link to Existing Account
318318
oauth_signin_title = Sign In to Authorize Linked Account
319319
oauth_signin_submit = Link Account
320+
oauth.signin.error = There was an error processing the authorization request. If this error persists, please contact the site administrator.
321+
oauth.signin.error.access_denied = The authorization request was denied.
322+
oauth.signin.error.temporarily_unavailable = Authorization failed because the authentication server is temporarily unavailable. Please try again later.
320323
openid_connect_submit = Connect
321324
openid_connect_title = Connect to an existing account
322325
openid_connect_desc = The chosen OpenID URI is unknown. Associate it with a new account here.

routers/web/auth/oauth.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ func (err AccessTokenError) Error() string {
106106
return fmt.Sprintf("%s: %s", err.ErrorCode, err.ErrorDescription)
107107
}
108108

109+
// errCallback represents a oauth2 callback error
110+
type errCallback struct {
111+
Code string
112+
Description string
113+
}
114+
115+
func (err errCallback) Error() string {
116+
return err.Description
117+
}
118+
109119
// TokenType specifies the kind of token
110120
type TokenType string
111121

@@ -810,7 +820,6 @@ func SignInOAuthCallback(ctx *context.Context) {
810820
}
811821

812822
u, gothUser, err := oAuth2UserLoginCallback(authSource, ctx.Req, ctx.Resp)
813-
814823
if err != nil {
815824
if user_model.IsErrUserProhibitLogin(err) {
816825
uplerr := err.(*user_model.ErrUserProhibitLogin)
@@ -819,6 +828,19 @@ func SignInOAuthCallback(ctx *context.Context) {
819828
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
820829
return
821830
}
831+
if callbackErr, ok := err.(errCallback); ok {
832+
log.Info("Failed OAuth callback: (%v) %v", callbackErr.Code, callbackErr.Description)
833+
switch callbackErr.Code {
834+
case "access_denied":
835+
ctx.Flash.Error(ctx.Tr("auth.oauth.signin.error.access_denied"))
836+
case "temporarily_unavailable":
837+
ctx.Flash.Error(ctx.Tr("auth.oauth.signin.error.temporarily_unavailable"))
838+
default:
839+
ctx.Flash.Error(ctx.Tr("auth.oauth.signin.error"))
840+
}
841+
ctx.Redirect(setting.AppSubURL + "/user/login")
842+
return
843+
}
822844
ctx.ServerError("UserSignIn", err)
823845
return
824846
}
@@ -1065,6 +1087,18 @@ func oAuth2UserLoginCallback(authSource *auth.Source, request *http.Request, res
10651087
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)
10661088
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)
10671089
}
1090+
// goth does not provide the original error message
1091+
// https://github.com/markbates/goth/issues/348
1092+
if strings.Contains(err.Error(), "server response missing access_token") || strings.Contains(err.Error(), "could not find a matching session for this request") {
1093+
errorCode := request.FormValue("error")
1094+
errorDescription := request.FormValue("error_description")
1095+
if errorCode != "" || errorDescription != "" {
1096+
return nil, goth.User{}, errCallback{
1097+
Code: errorCode,
1098+
Description: errorDescription,
1099+
}
1100+
}
1101+
}
10681102
return nil, goth.User{}, err
10691103
}
10701104

0 commit comments

Comments
 (0)