44package auth
55
66import (
7- stdContext "context"
7+ go_context "context"
88 "encoding/base64"
99 "errors"
1010 "fmt"
1111 "html"
1212 "io"
1313 "net/http"
1414 "net/url"
15+ "sort"
1516 "strings"
1617
1718 "code.gitea.io/gitea/models/auth"
@@ -39,6 +40,7 @@ import (
3940 "github.com/golang-jwt/jwt/v4"
4041 "github.com/markbates/goth"
4142 "github.com/markbates/goth/gothic"
43+ go_oauth2 "golang.org/x/oauth2"
4244)
4345
4446const (
@@ -143,7 +145,7 @@ type AccessTokenResponse struct {
143145 IDToken string `json:"id_token,omitempty"`
144146}
145147
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 ) {
147149 if setting .OAuth2 .InvalidateRefreshTokens {
148150 if err := grant .IncreaseCounter (ctx ); err != nil {
149151 return nil , & AccessTokenError {
@@ -886,6 +888,17 @@ func SignInOAuth(ctx *context.Context) {
886888func SignInOAuthCallback (ctx * context.Context ) {
887889 provider := ctx .Params (":provider" )
888890
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+
889902 // first look if the provider is still active
890903 authSource , err := auth .GetActiveOAuth2SourceByName (provider )
891904 if err != nil {
@@ -894,7 +907,7 @@ func SignInOAuthCallback(ctx *context.Context) {
894907 }
895908
896909 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" ))
898911 return
899912 }
900913
@@ -920,6 +933,9 @@ func SignInOAuthCallback(ctx *context.Context) {
920933 ctx .Redirect (setting .AppSubURL + "/user/login" )
921934 return
922935 }
936+ if err , ok := err .(* go_oauth2.RetrieveError ); ok {
937+ ctx .Flash .Error ("OAuth2 RetrieveError: " + err .Error (), true )
938+ }
923939 ctx .ServerError ("UserSignIn" , err )
924940 return
925941 }
0 commit comments