@@ -133,7 +133,7 @@ type AccessTokenResponse struct {
133
133
IDToken string `json:"id_token,omitempty"`
134
134
}
135
135
136
- func newAccessTokenResponse (grant * models.OAuth2Grant , signingKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
136
+ func newAccessTokenResponse (grant * models.OAuth2Grant , serverKey , clientKey oauth2.JWTSigningKey ) (* AccessTokenResponse , * AccessTokenError ) {
137
137
if setting .OAuth2 .InvalidateRefreshTokens {
138
138
if err := grant .IncreaseCounter (); err != nil {
139
139
return nil , & AccessTokenError {
@@ -151,7 +151,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
151
151
ExpiresAt : expirationDate .AsTime ().Unix (),
152
152
},
153
153
}
154
- signedAccessToken , err := accessToken .SignToken ()
154
+ signedAccessToken , err := accessToken .SignToken (serverKey )
155
155
if err != nil {
156
156
return nil , & AccessTokenError {
157
157
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -169,7 +169,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
169
169
ExpiresAt : refreshExpirationDate ,
170
170
},
171
171
}
172
- signedRefreshToken , err := refreshToken .SignToken ()
172
+ signedRefreshToken , err := refreshToken .SignToken (serverKey )
173
173
if err != nil {
174
174
return nil , & AccessTokenError {
175
175
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -225,7 +225,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
225
225
idToken .EmailVerified = user .IsActive
226
226
}
227
227
228
- signedIDToken , err = idToken .SignToken (signingKey )
228
+ signedIDToken , err = idToken .SignToken (clientKey )
229
229
if err != nil {
230
230
return nil , & AccessTokenError {
231
231
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
@@ -541,24 +541,25 @@ func AccessTokenOAuth(ctx *context.Context) {
541
541
}
542
542
}
543
543
544
- signingKey := oauth2 .DefaultSigningKey
545
- if signingKey .IsSymmetric () {
546
- clientKey , err := oauth2 .CreateJWTSingingKey (signingKey .SigningMethod ().Alg (), []byte (form .ClientSecret ))
544
+ serverKey := oauth2 .DefaultSigningKey
545
+ clientKey := serverKey
546
+ if serverKey .IsSymmetric () {
547
+ var err error
548
+ clientKey , err = oauth2 .CreateJWTSingingKey (serverKey .SigningMethod ().Alg (), []byte (form .ClientSecret ))
547
549
if err != nil {
548
550
handleAccessTokenError (ctx , AccessTokenError {
549
551
ErrorCode : AccessTokenErrorCodeInvalidRequest ,
550
552
ErrorDescription : "Error creating signing key" ,
551
553
})
552
554
return
553
555
}
554
- signingKey = clientKey
555
556
}
556
557
557
558
switch form .GrantType {
558
559
case "refresh_token" :
559
- handleRefreshToken (ctx , form , signingKey )
560
+ handleRefreshToken (ctx , form , serverKey , clientKey )
560
561
case "authorization_code" :
561
- handleAuthorizationCode (ctx , form , signingKey )
562
+ handleAuthorizationCode (ctx , form , serverKey , clientKey )
562
563
default :
563
564
handleAccessTokenError (ctx , AccessTokenError {
564
565
ErrorCode : AccessTokenErrorCodeUnsupportedGrantType ,
@@ -567,8 +568,8 @@ func AccessTokenOAuth(ctx *context.Context) {
567
568
}
568
569
}
569
570
570
- func handleRefreshToken (ctx * context.Context , form forms.AccessTokenForm , signingKey oauth2.JWTSigningKey ) {
571
- token , err := oauth2 .ParseToken (form .RefreshToken )
571
+ func handleRefreshToken (ctx * context.Context , form forms.AccessTokenForm , serverKey , clientKey oauth2.JWTSigningKey ) {
572
+ token , err := oauth2 .ParseToken (form .RefreshToken , serverKey )
572
573
if err != nil {
573
574
handleAccessTokenError (ctx , AccessTokenError {
574
575
ErrorCode : AccessTokenErrorCodeUnauthorizedClient ,
@@ -595,15 +596,15 @@ func handleRefreshToken(ctx *context.Context, form forms.AccessTokenForm, signin
595
596
log .Warn ("A client tried to use a refresh token for grant_id = %d was used twice!" , grant .ID )
596
597
return
597
598
}
598
- accessToken , tokenErr := newAccessTokenResponse (grant , signingKey )
599
+ accessToken , tokenErr := newAccessTokenResponse (grant , serverKey , clientKey )
599
600
if tokenErr != nil {
600
601
handleAccessTokenError (ctx , * tokenErr )
601
602
return
602
603
}
603
604
ctx .JSON (http .StatusOK , accessToken )
604
605
}
605
606
606
- func handleAuthorizationCode (ctx * context.Context , form forms.AccessTokenForm , signingKey oauth2.JWTSigningKey ) {
607
+ func handleAuthorizationCode (ctx * context.Context , form forms.AccessTokenForm , serverKey , clientKey oauth2.JWTSigningKey ) {
607
608
app , err := models .GetOAuth2ApplicationByClientID (form .ClientID )
608
609
if err != nil {
609
610
handleAccessTokenError (ctx , AccessTokenError {
@@ -657,7 +658,7 @@ func handleAuthorizationCode(ctx *context.Context, form forms.AccessTokenForm, s
657
658
ErrorDescription : "cannot proceed your request" ,
658
659
})
659
660
}
660
- resp , tokenErr := newAccessTokenResponse (authorizationCode .Grant , signingKey )
661
+ resp , tokenErr := newAccessTokenResponse (authorizationCode .Grant , serverKey , clientKey )
661
662
if tokenErr != nil {
662
663
handleAccessTokenError (ctx , * tokenErr )
663
664
return
0 commit comments