@@ -115,7 +115,7 @@ type AccessTokenResponse struct {
115
115
IDToken string `json:"id_token,omitempty"`
116
116
}
117
117
118
- func newAccessTokenResponse (grant * models.OAuth2Grant , signingKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
118
+ func newAccessTokenResponse (grant * models.OAuth2Grant , serverKey , clientKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
119
119
if setting .OAuth2 .InvalidateRefreshTokens {
120
120
if err := grant .IncreaseCounter (); err != nil {
121
121
return nil , & AccessTokenError {
@@ -133,7 +133,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
133
133
ExpiresAt : expirationDate .AsTime ().Unix (),
134
134
},
135
135
}
136
- signedAccessToken , err := accessToken .SignToken ()
136
+ signedAccessToken , err := accessToken .SignToken (serverKey )
137
137
if err != nil {
138
138
return nil , & AccessTokenError {
139
139
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -151,7 +151,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
151
151
ExpiresAt : refreshExpirationDate ,
152
152
},
153
153
}
154
- signedRefreshToken , err := refreshToken .SignToken ()
154
+ signedRefreshToken , err := refreshToken .SignToken (serverKey )
155
155
if err != nil {
156
156
return nil , & AccessTokenError {
157
157
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -207,7 +207,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
207
207
idToken .EmailVerified = user .IsActive
208
208
}
209
209
210
- signedIDToken , err = idToken .SignToken (signingKey )
210
+ signedIDToken , err = idToken .SignToken (clientKey )
211
211
if err != nil {
212
212
return nil , & AccessTokenError {
213
213
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -265,7 +265,7 @@ func IntrospectOAuth(ctx *context.Context) {
265
265
}
266
266
267
267
form := web .GetForm (ctx ).(* forms.IntrospectTokenForm )
268
- token , err := oauth2 .ParseToken (form .Token )
268
+ token , err := oauth2 .ParseToken (form .Token , oauth2 . DefaultSigningKey )
269
269
if err == nil {
270
270
if token .Valid () == nil {
271
271
grant , err := models .GetOAuth2GrantByID (token .GrantID )
@@ -544,24 +544,25 @@ func AccessTokenOAuth(ctx *context.Context) {
544
544
}
545
545
}
546
546
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 ))
550
552
if err != nil {
551
553
handleAccessTokenError (ctx , AccessTokenError {
552
554
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
553
555
ErrorDescription : "Error creating signing key" ,
554
556
})
555
557
return
556
558
}
557
- signingKey = clientKey
558
559
}
559
560
560
561
switch form .GrantType {
561
562
case "refresh_token" :
562
- handleRefreshToken (ctx , form , signingKey )
563
+ handleRefreshToken (ctx , form , serverKey , clientKey )
563
564
case "authorization_code" :
564
- handleAuthorizationCode (ctx , form , signingKey )
565
+ handleAuthorizationCode (ctx , form , serverKey , clientKey )
565
566
default :
566
567
handleAccessTokenError (ctx , AccessTokenError {
567
568
ErrorCode : AccessTokenErrorCodeUnsupportedGrantType ,
@@ -570,8 +571,8 @@ func AccessTokenOAuth(ctx *context.Context) {
570
571
}
571
572
}
572
573
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 )
575
576
if err != nil {
576
577
handleAccessTokenError (ctx , AccessTokenError {
577
578
ErrorCode : AccessTokenErrorCodeUnauthorizedClient ,
@@ -598,15 +599,15 @@ func handleRefreshToken(ctx *context.Context, form forms.AccessTokenForm, signin
598
599
log .Warn ("A client tried to use a refresh token for grant_id = %d was used twice!" , grant .ID )
599
600
return
600
601
}
601
- accessToken , tokenErr := newAccessTokenResponse (grant , signingKey )
602
+ accessToken , tokenErr := newAccessTokenResponse (grant , serverKey , clientKey )
602
603
if tokenErr != nil {
603
604
handleAccessTokenError (ctx , * tokenErr )
604
605
return
605
606
}
606
607
ctx .JSON (http .StatusOK , accessToken )
607
608
}
608
609
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 ) {
610
611
app , err := models .GetOAuth2ApplicationByClientID (form .ClientID )
611
612
if err != nil {
612
613
handleAccessTokenError (ctx , AccessTokenError {
@@ -660,7 +661,7 @@ func handleAuthorizationCode(ctx *context.Context, form forms.AccessTokenForm, s
660
661
ErrorDescription : "cannot proceed your request" ,
661
662
})
662
663
}
663
- resp , tokenErr := newAccessTokenResponse (authorizationCode .Grant , signingKey )
664
+ resp , tokenErr := newAccessTokenResponse (authorizationCode .Grant , serverKey , clientKey )
664
665
if tokenErr != nil {
665
666
handleAccessTokenError (ctx , * tokenErr )
666
667
return
0 commit comments