@@ -25,6 +25,7 @@ import (
25
25
26
26
"github.com/ory/hydra/v2/oauth2/trust"
27
27
28
+ "github.com/ory/hydra/v2/driver/config"
28
29
"github.com/ory/hydra/v2/x"
29
30
30
31
"github.com/ory/fosite/storage"
@@ -225,32 +226,34 @@ func TestHelperRunner(t *testing.T, store InternalRegistry, k string) {
225
226
t .Run (fmt .Sprintf ("case=testHelperDeleteAccessTokens/db=%s" , k ), testHelperDeleteAccessTokens (store ))
226
227
t .Run (fmt .Sprintf ("case=testHelperRevokeAccessToken/db=%s" , k ), testHelperRevokeAccessToken (store ))
227
228
t .Run (fmt .Sprintf ("case=testFositeJWTBearerGrantStorage/db=%s" , k ), testFositeJWTBearerGrantStorage (store ))
229
+ t .Run (fmt .Sprintf ("case=testHelperRevokeRefreshTokenMaybeGracePeriod/db=%s" , k ), testHelperRevokeRefreshTokenMaybeGracePeriod (store ))
228
230
}
229
231
230
232
func testHelperRequestIDMultiples (m InternalRegistry , _ string ) func (t * testing.T ) {
231
233
return func (t * testing.T ) {
232
- requestId := uuid .New ()
233
- mockRequestForeignKey (t , requestId , m )
234
+ ctx := context .Background ()
235
+ requestID := uuid .New ()
236
+ mockRequestForeignKey (t , requestID , m )
234
237
cl := & client.Client {ID : "foobar" }
235
238
236
239
fositeRequest := & fosite.Request {
237
- ID : requestId ,
240
+ ID : requestID ,
238
241
Client : cl ,
239
242
RequestedAt : time .Now ().UTC ().Round (time .Second ),
240
243
Session : NewSession ("bar" ),
241
244
}
242
245
243
246
for i := 0 ; i < 4 ; i ++ {
244
247
signature := uuid .New ()
245
- err := m .OAuth2Storage ().CreateRefreshTokenSession (context . TODO () , signature , fositeRequest )
248
+ err := m .OAuth2Storage ().CreateRefreshTokenSession (ctx , signature , fositeRequest )
246
249
assert .NoError (t , err )
247
- err = m .OAuth2Storage ().CreateAccessTokenSession (context . TODO () , signature , fositeRequest )
250
+ err = m .OAuth2Storage ().CreateAccessTokenSession (ctx , signature , fositeRequest )
248
251
assert .NoError (t , err )
249
- err = m .OAuth2Storage ().CreateOpenIDConnectSession (context . TODO () , signature , fositeRequest )
252
+ err = m .OAuth2Storage ().CreateOpenIDConnectSession (ctx , signature , fositeRequest )
250
253
assert .NoError (t , err )
251
- err = m .OAuth2Storage ().CreatePKCERequestSession (context . TODO () , signature , fositeRequest )
254
+ err = m .OAuth2Storage ().CreatePKCERequestSession (ctx , signature , fositeRequest )
252
255
assert .NoError (t , err )
253
- err = m .OAuth2Storage ().CreateAuthorizeCodeSession (context . TODO () , signature , fositeRequest )
256
+ err = m .OAuth2Storage ().CreateAuthorizeCodeSession (ctx , signature , fositeRequest )
254
257
assert .NoError (t , err )
255
258
}
256
259
}
@@ -475,7 +478,7 @@ func testHelperNilAccessToken(x InternalRegistry) func(t *testing.T) {
475
478
m := x .OAuth2Storage ()
476
479
c := & client.Client {ID : "nil-request-client-id-123" }
477
480
require .NoError (t , x .ClientManager ().CreateClient (context .Background (), c ))
478
- err := m .CreateAccessTokenSession (context .TODO (), "nil-request-id" , & fosite.Request {
481
+ err := m .CreateAccessTokenSession (context .Background (), "nil-request-id" , & fosite.Request {
479
482
ID : "" ,
480
483
RequestedAt : time .Now ().UTC ().Round (time .Second ),
481
484
Client : c ,
@@ -553,6 +556,63 @@ func testHelperRevokeAccessToken(x InternalRegistry) func(t *testing.T) {
553
556
}
554
557
}
555
558
559
+ func testHelperRevokeRefreshTokenMaybeGracePeriod (x InternalRegistry ) func (t * testing.T ) {
560
+ return func (t * testing.T ) {
561
+ ctx := context .Background ()
562
+
563
+ t .Run ("Revokes refresh token when grace period not configured" , func (t * testing.T ) {
564
+ // SETUP
565
+ m := x .OAuth2Storage ()
566
+
567
+ refreshTokenSession := fmt .Sprintf ("refresh_token_%d" , time .Now ().Unix ())
568
+ err := m .CreateRefreshTokenSession (ctx , refreshTokenSession , & defaultRequest )
569
+ require .NoError (t , err , "precondition failed: could not create refresh token session" )
570
+
571
+ // ACT
572
+ err = m .RevokeRefreshTokenMaybeGracePeriod (ctx , defaultRequest .GetID (), refreshTokenSession )
573
+ require .NoError (t , err )
574
+
575
+ tmpSession := new (fosite.Session )
576
+ _ , err = m .GetRefreshTokenSession (ctx , refreshTokenSession , * tmpSession )
577
+
578
+ // ASSERT
579
+ // a revoked refresh token returns an error when getting the token again
580
+ assert .ErrorIs (t , err , fosite .ErrInactiveToken )
581
+ })
582
+
583
+ t .Run ("refresh token enters grace period when configured," , func (t * testing.T ) {
584
+ // SETUP
585
+ x .Config ().MustSet (ctx , config .KeyRefreshTokenRotationGracePeriod , "1m" )
586
+
587
+ // always reset back to the default
588
+ t .Cleanup (func () {
589
+ x .Config ().MustSet (ctx , config .KeyRefreshTokenRotationGracePeriod , "0m" )
590
+ })
591
+
592
+ m := x .OAuth2Storage ()
593
+
594
+ refreshTokenSession := fmt .Sprintf ("refresh_token_%d_with_grace_period" , time .Now ().Unix ())
595
+ err := m .CreateRefreshTokenSession (ctx , refreshTokenSession , & defaultRequest )
596
+ require .NoError (t , err , "precondition failed: could not create refresh token session" )
597
+
598
+ // ACT
599
+ require .NoError (t , m .RevokeRefreshTokenMaybeGracePeriod (ctx , defaultRequest .GetID (), refreshTokenSession ))
600
+ require .NoError (t , m .RevokeRefreshTokenMaybeGracePeriod (ctx , defaultRequest .GetID (), refreshTokenSession ))
601
+ require .NoError (t , m .RevokeRefreshTokenMaybeGracePeriod (ctx , defaultRequest .GetID (), refreshTokenSession ))
602
+
603
+ req , err := m .GetRefreshTokenSession (ctx , refreshTokenSession , nil )
604
+
605
+ // ASSERT
606
+ // when grace period is configured the refresh token can be obtained within
607
+ // the grace period without error
608
+ assert .NoError (t , err )
609
+
610
+ assert .Equal (t , defaultRequest .GetID (), req .GetID ())
611
+ })
612
+ }
613
+
614
+ }
615
+
556
616
func testHelperCreateGetDeletePKCERequestSession (x InternalRegistry ) func (t * testing.T ) {
557
617
return func (t * testing.T ) {
558
618
m := x .OAuth2Storage ()
@@ -880,6 +940,7 @@ func testFositeStoreClientAssertionJWTValid(m InternalRegistry) func(*testing.T)
880
940
881
941
func testFositeJWTBearerGrantStorage (x InternalRegistry ) func (t * testing.T ) {
882
942
return func (t * testing.T ) {
943
+ ctx := context .Background ()
883
944
grantManager := x .GrantManager ()
884
945
keyManager := x .KeyManager ()
885
946
grantStorage := x .OAuth2Storage ().(rfc7523.RFC7523KeyStorage )
@@ -902,28 +963,28 @@ func testFositeJWTBearerGrantStorage(x InternalRegistry) func(t *testing.T) {
902
963
ExpiresAt : time .Now ().UTC ().Round (time .Second ).AddDate (1 , 0 , 0 ),
903
964
}
904
965
905
- storedKeySet , err := grantStorage .GetPublicKeys (context . TODO () , issuer , subject )
966
+ storedKeySet , err := grantStorage .GetPublicKeys (ctx , issuer , subject )
906
967
require .NoError (t , err )
907
968
require .Len (t , storedKeySet .Keys , 0 )
908
969
909
- err = grantManager .CreateGrant (context . TODO () , grant , publicKey )
970
+ err = grantManager .CreateGrant (ctx , grant , publicKey )
910
971
require .NoError (t , err )
911
972
912
- storedKeySet , err = grantStorage .GetPublicKeys (context . TODO () , issuer , subject )
973
+ storedKeySet , err = grantStorage .GetPublicKeys (ctx , issuer , subject )
913
974
require .NoError (t , err )
914
975
assert .Len (t , storedKeySet .Keys , 1 )
915
976
916
- storedKey , err := grantStorage .GetPublicKey (context . TODO () , issuer , subject , publicKey .KeyID )
977
+ storedKey , err := grantStorage .GetPublicKey (ctx , issuer , subject , publicKey .KeyID )
917
978
require .NoError (t , err )
918
979
assert .Equal (t , publicKey .KeyID , storedKey .KeyID )
919
980
assert .Equal (t , publicKey .Use , storedKey .Use )
920
981
assert .Equal (t , publicKey .Key , storedKey .Key )
921
982
922
- storedScopes , err := grantStorage .GetPublicKeyScopes (context . TODO () , issuer , subject , publicKey .KeyID )
983
+ storedScopes , err := grantStorage .GetPublicKeyScopes (ctx , issuer , subject , publicKey .KeyID )
923
984
require .NoError (t , err )
924
985
assert .Equal (t , grant .Scope , storedScopes )
925
986
926
- storedKeySet , err = keyManager .GetKey (context . TODO () , issuer , publicKey .KeyID )
987
+ storedKeySet , err = keyManager .GetKey (ctx , issuer , publicKey .KeyID )
927
988
require .NoError (t , err )
928
989
assert .Equal (t , publicKey .KeyID , storedKeySet .Keys [0 ].KeyID )
929
990
assert .Equal (t , publicKey .Use , storedKeySet .Keys [0 ].Use )
@@ -953,7 +1014,7 @@ func testFositeJWTBearerGrantStorage(x InternalRegistry) func(t *testing.T) {
953
1014
954
1015
keySet2ToReturn , err := jwk .GenerateJWK (context .Background (), jose .ES256 , "maria-key-2" , "sig" )
955
1016
require .NoError (t , err )
956
- require .NoError (t , grantManager .CreateGrant (context . TODO () , trust.Grant {
1017
+ require .NoError (t , grantManager .CreateGrant (ctx , trust.Grant {
957
1018
ID : uuid .New (),
958
1019
Issuer : issuer ,
959
1020
Subject : subject ,
@@ -1011,22 +1072,22 @@ func testFositeJWTBearerGrantStorage(x InternalRegistry) func(t *testing.T) {
1011
1072
ExpiresAt : time .Now ().UTC ().Round (time .Second ).AddDate (1 , 0 , 0 ),
1012
1073
}
1013
1074
1014
- err = grantManager .CreateGrant (context . TODO () , grant , publicKey )
1075
+ err = grantManager .CreateGrant (ctx , grant , publicKey )
1015
1076
require .NoError (t , err )
1016
1077
1017
- _ , err = grantStorage .GetPublicKey (context . TODO () , issuer , subject , grant .PublicKey .KeyID )
1078
+ _ , err = grantStorage .GetPublicKey (ctx , issuer , subject , grant .PublicKey .KeyID )
1018
1079
require .NoError (t , err )
1019
1080
1020
- _ , err = keyManager .GetKey (context . TODO () , issuer , publicKey .KeyID )
1081
+ _ , err = keyManager .GetKey (ctx , issuer , publicKey .KeyID )
1021
1082
require .NoError (t , err )
1022
1083
1023
- err = grantManager .DeleteGrant (context . TODO () , grant .ID )
1084
+ err = grantManager .DeleteGrant (ctx , grant .ID )
1024
1085
require .NoError (t , err )
1025
1086
1026
- _ , err = grantStorage .GetPublicKey (context . TODO () , issuer , subject , publicKey .KeyID )
1087
+ _ , err = grantStorage .GetPublicKey (ctx , issuer , subject , publicKey .KeyID )
1027
1088
assert .Error (t , err )
1028
1089
1029
- _ , err = keyManager .GetKey (context . TODO () , issuer , publicKey .KeyID )
1090
+ _ , err = keyManager .GetKey (ctx , issuer , publicKey .KeyID )
1030
1091
assert .Error (t , err )
1031
1092
})
1032
1093
@@ -1048,22 +1109,22 @@ func testFositeJWTBearerGrantStorage(x InternalRegistry) func(t *testing.T) {
1048
1109
ExpiresAt : time .Now ().UTC ().Round (time .Second ).AddDate (1 , 0 , 0 ),
1049
1110
}
1050
1111
1051
- err = grantManager .CreateGrant (context . TODO () , grant , publicKey )
1112
+ err = grantManager .CreateGrant (ctx , grant , publicKey )
1052
1113
require .NoError (t , err )
1053
1114
1054
- _ , err = grantStorage .GetPublicKey (context . TODO () , issuer , subject , publicKey .KeyID )
1115
+ _ , err = grantStorage .GetPublicKey (ctx , issuer , subject , publicKey .KeyID )
1055
1116
require .NoError (t , err )
1056
1117
1057
- _ , err = keyManager .GetKey (context . TODO () , issuer , publicKey .KeyID )
1118
+ _ , err = keyManager .GetKey (ctx , issuer , publicKey .KeyID )
1058
1119
require .NoError (t , err )
1059
1120
1060
- err = keyManager .DeleteKey (context . TODO () , issuer , publicKey .KeyID )
1121
+ err = keyManager .DeleteKey (ctx , issuer , publicKey .KeyID )
1061
1122
require .NoError (t , err )
1062
1123
1063
- _ , err = keyManager .GetKey (context . TODO () , issuer , publicKey .KeyID )
1124
+ _ , err = keyManager .GetKey (ctx , issuer , publicKey .KeyID )
1064
1125
assert .Error (t , err )
1065
1126
1066
- _ , err = grantManager .GetConcreteGrant (context . TODO () , grant .ID )
1127
+ _ , err = grantManager .GetConcreteGrant (ctx , grant .ID )
1067
1128
assert .Error (t , err )
1068
1129
})
1069
1130
@@ -1085,25 +1146,25 @@ func testFositeJWTBearerGrantStorage(x InternalRegistry) func(t *testing.T) {
1085
1146
ExpiresAt : time .Now ().UTC ().Round (time .Second ).AddDate (1 , 0 , 0 ),
1086
1147
}
1087
1148
1088
- err = grantManager .CreateGrant (context . TODO () , grant , publicKey )
1149
+ err = grantManager .CreateGrant (ctx , grant , publicKey )
1089
1150
require .NoError (t , err )
1090
1151
1091
1152
// All three get methods should only return the public key when using the valid subject
1092
- _ , err = grantStorage .GetPublicKey (context . TODO () , issuer , "any-subject-1" , publicKey .KeyID )
1153
+ _ , err = grantStorage .GetPublicKey (ctx , issuer , "any-subject-1" , publicKey .KeyID )
1093
1154
require .Error (t , err )
1094
- _ , err = grantStorage .GetPublicKey (context . TODO () , issuer , subject , publicKey .KeyID )
1155
+ _ , err = grantStorage .GetPublicKey (ctx , issuer , subject , publicKey .KeyID )
1095
1156
require .NoError (t , err )
1096
1157
1097
- _ , err = grantStorage .GetPublicKeyScopes (context . TODO () , issuer , "any-subject-2" , publicKey .KeyID )
1158
+ _ , err = grantStorage .GetPublicKeyScopes (ctx , issuer , "any-subject-2" , publicKey .KeyID )
1098
1159
require .Error (t , err )
1099
- _ , err = grantStorage .GetPublicKeyScopes (context . TODO () , issuer , subject , publicKey .KeyID )
1160
+ _ , err = grantStorage .GetPublicKeyScopes (ctx , issuer , subject , publicKey .KeyID )
1100
1161
require .NoError (t , err )
1101
1162
1102
- jwks , err := grantStorage .GetPublicKeys (context . TODO () , issuer , "any-subject-3" )
1163
+ jwks , err := grantStorage .GetPublicKeys (ctx , issuer , "any-subject-3" )
1103
1164
require .NoError (t , err )
1104
1165
require .NotNil (t , jwks )
1105
1166
require .Empty (t , jwks .Keys )
1106
- jwks , err = grantStorage .GetPublicKeys (context . TODO () , issuer , subject )
1167
+ jwks , err = grantStorage .GetPublicKeys (ctx , issuer , subject )
1107
1168
require .NoError (t , err )
1108
1169
require .NotNil (t , jwks )
1109
1170
require .NotEmpty (t , jwks .Keys )
@@ -1126,17 +1187,17 @@ func testFositeJWTBearerGrantStorage(x InternalRegistry) func(t *testing.T) {
1126
1187
ExpiresAt : time .Now ().UTC ().Round (time .Second ).AddDate (1 , 0 , 0 ),
1127
1188
}
1128
1189
1129
- err = grantManager .CreateGrant (context . TODO () , grant , publicKey )
1190
+ err = grantManager .CreateGrant (ctx , grant , publicKey )
1130
1191
require .NoError (t , err )
1131
1192
1132
1193
// All three get methods should always return the public key
1133
- _ , err = grantStorage .GetPublicKey (context . TODO () , issuer , "any-subject-1" , publicKey .KeyID )
1194
+ _ , err = grantStorage .GetPublicKey (ctx , issuer , "any-subject-1" , publicKey .KeyID )
1134
1195
require .NoError (t , err )
1135
1196
1136
- _ , err = grantStorage .GetPublicKeyScopes (context . TODO () , issuer , "any-subject-2" , publicKey .KeyID )
1197
+ _ , err = grantStorage .GetPublicKeyScopes (ctx , issuer , "any-subject-2" , publicKey .KeyID )
1137
1198
require .NoError (t , err )
1138
1199
1139
- jwks , err := grantStorage .GetPublicKeys (context . TODO () , issuer , "any-subject-3" )
1200
+ jwks , err := grantStorage .GetPublicKeys (ctx , issuer , "any-subject-3" )
1140
1201
require .NoError (t , err )
1141
1202
require .NotNil (t , jwks )
1142
1203
require .NotEmpty (t , jwks .Keys )
@@ -1159,10 +1220,10 @@ func testFositeJWTBearerGrantStorage(x InternalRegistry) func(t *testing.T) {
1159
1220
ExpiresAt : time .Now ().UTC ().Round (time .Second ).AddDate (- 1 , 0 , 0 ),
1160
1221
}
1161
1222
1162
- err = grantManager .CreateGrant (context . TODO () , grant , publicKey )
1223
+ err = grantManager .CreateGrant (ctx , grant , publicKey )
1163
1224
require .NoError (t , err )
1164
1225
1165
- keys , err := grantStorage .GetPublicKeys (context . TODO () , issuer , "any-subject-3" )
1226
+ keys , err := grantStorage .GetPublicKeys (ctx , issuer , "any-subject-3" )
1166
1227
require .NoError (t , err )
1167
1228
assert .Len (t , keys .Keys , 0 )
1168
1229
})
0 commit comments