@@ -115,7 +115,7 @@ type AccessTokenResponse struct {
115115 IDToken string `json:"id_token,omitempty"`
116116}
117117
118- func newAccessTokenResponse (grant * models.OAuth2Grant , signingKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
118+ func newAccessTokenResponse (grant * models.OAuth2Grant , serverKey , clientKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
119119 if setting .OAuth2 .InvalidateRefreshTokens {
120120 if err := grant .IncreaseCounter (); err != nil {
121121 return nil , & AccessTokenError {
@@ -133,7 +133,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
133133 ExpiresAt : expirationDate .AsTime ().Unix (),
134134 },
135135 }
136- signedAccessToken , err := accessToken .SignToken ()
136+ signedAccessToken , err := accessToken .SignToken (serverKey )
137137 if err != nil {
138138 return nil , & AccessTokenError {
139139 ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -151,7 +151,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
151151 ExpiresAt : refreshExpirationDate ,
152152 },
153153 }
154- signedRefreshToken , err := refreshToken .SignToken ()
154+ signedRefreshToken , err := refreshToken .SignToken (serverKey )
155155 if err != nil {
156156 return nil , & AccessTokenError {
157157 ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -207,7 +207,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
207207 idToken .EmailVerified = user .IsActive
208208 }
209209
210- signedIDToken , err = idToken .SignToken (signingKey )
210+ signedIDToken , err = idToken .SignToken (clientKey )
211211 if err != nil {
212212 return nil , & AccessTokenError {
213213 ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -265,7 +265,7 @@ func IntrospectOAuth(ctx *context.Context) {
265265 }
266266
267267 form := web .GetForm (ctx ).(* forms.IntrospectTokenForm )
268- token , err := oauth2 .ParseToken (form .Token )
268+ token , err := oauth2 .ParseToken (form .Token , oauth2 . DefaultSigningKey )
269269 if err == nil {
270270 if token .Valid () == nil {
271271 grant , err := models .GetOAuth2GrantByID (token .GrantID )
@@ -544,24 +544,25 @@ func AccessTokenOAuth(ctx *context.Context) {
544544 }
545545 }
546546
547- signingKey := oauth2 .DefaultSigningKey
548- if signingKey .IsSymmetric () {
549- clientKey , err := oauth2 .CreateJWTSigningKey (signingKey .SigningMethod ().Alg (), []byte (form .ClientSecret ))
547+ serverKey := oauth2 .DefaultSigningKey
548+ clientKey := serverKey
549+ if serverKey .IsSymmetric () {
550+ var err error
551+ clientKey , err = oauth2 .CreateJWTSigningKey (serverKey .SigningMethod ().Alg (), []byte (form .ClientSecret ))
550552 if err != nil {
551553 handleAccessTokenError (ctx , AccessTokenError {
552554 ErrorCode : AccessTokenErrorCodeInvalidRequest ,
553555 ErrorDescription : "Error creating signing key" ,
554556 })
555557 return
556558 }
557- signingKey = clientKey
558559 }
559560
560561 switch form .GrantType {
561562 case "refresh_token" :
562- handleRefreshToken (ctx , form , signingKey )
563+ handleRefreshToken (ctx , form , serverKey , clientKey )
563564 case "authorization_code" :
564- handleAuthorizationCode (ctx , form , signingKey )
565+ handleAuthorizationCode (ctx , form , serverKey , clientKey )
565566 default :
566567 handleAccessTokenError (ctx , AccessTokenError {
567568 ErrorCode : AccessTokenErrorCodeUnsupportedGrantType ,
@@ -570,8 +571,8 @@ func AccessTokenOAuth(ctx *context.Context) {
570571 }
571572}
572573
573- func handleRefreshToken (ctx * context.Context , form forms.AccessTokenForm , signingKey oauth2.JWTSigningKey ) {
574- token , err := oauth2 .ParseToken (form .RefreshToken )
574+ func handleRefreshToken (ctx * context.Context , form forms.AccessTokenForm , serverKey , clientKey oauth2.JWTSigningKey ) {
575+ token , err := oauth2 .ParseToken (form .RefreshToken , serverKey )
575576 if err != nil {
576577 handleAccessTokenError (ctx , AccessTokenError {
577578 ErrorCode : AccessTokenErrorCodeUnauthorizedClient ,
@@ -598,15 +599,15 @@ func handleRefreshToken(ctx *context.Context, form forms.AccessTokenForm, signin
598599 log .Warn ("A client tried to use a refresh token for grant_id = %d was used twice!" , grant .ID )
599600 return
600601 }
601- accessToken , tokenErr := newAccessTokenResponse (grant , signingKey )
602+ accessToken , tokenErr := newAccessTokenResponse (grant , serverKey , clientKey )
602603 if tokenErr != nil {
603604 handleAccessTokenError (ctx , * tokenErr )
604605 return
605606 }
606607 ctx .JSON (http .StatusOK , accessToken )
607608}
608609
609- func handleAuthorizationCode (ctx * context.Context , form forms.AccessTokenForm , signingKey oauth2.JWTSigningKey ) {
610+ func handleAuthorizationCode (ctx * context.Context , form forms.AccessTokenForm , serverKey , clientKey oauth2.JWTSigningKey ) {
610611 app , err := models .GetOAuth2ApplicationByClientID (form .ClientID )
611612 if err != nil {
612613 handleAccessTokenError (ctx , AccessTokenError {
@@ -660,7 +661,7 @@ func handleAuthorizationCode(ctx *context.Context, form forms.AccessTokenForm, s
660661 ErrorDescription : "cannot proceed your request" ,
661662 })
662663 }
663- resp , tokenErr := newAccessTokenResponse (authorizationCode .Grant , signingKey )
664+ resp , tokenErr := newAccessTokenResponse (authorizationCode .Grant , serverKey , clientKey )
664665 if tokenErr != nil {
665666 handleAccessTokenError (ctx , * tokenErr )
666667 return
0 commit comments