4
4
package auth
5
5
6
6
import (
7
- stdContext "context"
7
+ go_context "context"
8
8
"encoding/base64"
9
9
"errors"
10
10
"fmt"
11
11
"html"
12
12
"io"
13
13
"net/http"
14
14
"net/url"
15
+ "sort"
15
16
"strings"
16
17
17
18
"code.gitea.io/gitea/models/auth"
@@ -39,6 +40,7 @@ import (
39
40
"github.com/golang-jwt/jwt/v4"
40
41
"github.com/markbates/goth"
41
42
"github.com/markbates/goth/gothic"
43
+ go_oauth2 "golang.org/x/oauth2"
42
44
)
43
45
44
46
const (
@@ -143,7 +145,7 @@ type AccessTokenResponse struct {
143
145
IDToken string `json:"id_token,omitempty"`
144
146
}
145
147
146
- func newAccessTokenResponse (ctx stdContext .Context , grant * auth.OAuth2Grant , serverKey , clientKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
148
+ func newAccessTokenResponse (ctx go_context .Context , grant * auth.OAuth2Grant , serverKey , clientKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
147
149
if setting .OAuth2 .InvalidateRefreshTokens {
148
150
if err := grant .IncreaseCounter (ctx ); err != nil {
149
151
return nil , & AccessTokenError {
@@ -886,6 +888,17 @@ func SignInOAuth(ctx *context.Context) {
886
888
func SignInOAuthCallback (ctx * context.Context ) {
887
889
provider := ctx .Params (":provider" )
888
890
891
+ if ctx .Req .FormValue ("error" ) != "" {
892
+ var errorKeyValues []string
893
+ for k , vv := range ctx .Req .Form {
894
+ for _ , v := range vv {
895
+ errorKeyValues = append (errorKeyValues , fmt .Sprintf ("%s = %s" , html .EscapeString (k ), html .EscapeString (v )))
896
+ }
897
+ }
898
+ sort .Strings (errorKeyValues )
899
+ ctx .Flash .Error (strings .Join (errorKeyValues , "<br>" ), true )
900
+ }
901
+
889
902
// first look if the provider is still active
890
903
authSource , err := auth .GetActiveOAuth2SourceByName (provider )
891
904
if err != nil {
@@ -894,7 +907,7 @@ func SignInOAuthCallback(ctx *context.Context) {
894
907
}
895
908
896
909
if authSource == nil {
897
- ctx .ServerError ("SignIn" , errors .New ("No valid provider found, check configured callback url in provider" ))
910
+ ctx .ServerError ("SignIn" , errors .New ("no valid provider found, check configured callback url in provider" ))
898
911
return
899
912
}
900
913
@@ -920,6 +933,9 @@ func SignInOAuthCallback(ctx *context.Context) {
920
933
ctx .Redirect (setting .AppSubURL + "/user/login" )
921
934
return
922
935
}
936
+ if err , ok := err .(* go_oauth2.RetrieveError ); ok {
937
+ ctx .Flash .Error ("OAuth2 RetrieveError: " + err .Error (), true )
938
+ }
923
939
ctx .ServerError ("UserSignIn" , err )
924
940
return
925
941
}
0 commit comments