@@ -105,6 +105,7 @@ type FakeIDP struct {
105
105
// "Authorized Redirect URLs". This can be used to emulate that.
106
106
hookValidRedirectURL func (redirectURL string ) error
107
107
hookUserInfo func (email string ) (jwt.MapClaims , error )
108
+ hookAccessTokenJWT func (email string , exp time.Time ) jwt.MapClaims
108
109
// defaultIDClaims is if a new client connects and we didn't preset
109
110
// some claims.
110
111
defaultIDClaims jwt.MapClaims
@@ -154,6 +155,12 @@ func WithMiddlewares(mws ...func(http.Handler) http.Handler) func(*FakeIDP) {
154
155
}
155
156
}
156
157
158
+ func WithAccessTokenJWTHook (hook func (email string , exp time.Time ) jwt.MapClaims ) func (* FakeIDP ) {
159
+ return func (f * FakeIDP ) {
160
+ f .hookAccessTokenJWT = hook
161
+ }
162
+ }
163
+
157
164
func WithHookWellKnown (hook func (r * http.Request , j * ProviderJSON ) error ) func (* FakeIDP ) {
158
165
return func (f * FakeIDP ) {
159
166
f .hookWellKnown = hook
@@ -316,8 +323,7 @@ const (
316
323
func NewFakeIDP (t testing.TB , opts ... FakeIDPOpt ) * FakeIDP {
317
324
t .Helper ()
318
325
319
- block , _ := pem .Decode ([]byte (testRSAPrivateKey ))
320
- pkey , err := x509 .ParsePKCS1PrivateKey (block .Bytes )
326
+ pkey , err := FakeIDPKey ()
321
327
require .NoError (t , err )
322
328
323
329
idp := & FakeIDP {
@@ -676,8 +682,13 @@ func (f *FakeIDP) newCode(state string) string {
676
682
677
683
// newToken enforces the access token exchanged is actually a valid access token
678
684
// created by the IDP.
679
- func (f * FakeIDP ) newToken (email string , expires time.Time ) string {
685
+ func (f * FakeIDP ) newToken (t testing. TB , email string , expires time.Time ) string {
680
686
accessToken := uuid .NewString ()
687
+ if f .hookAccessTokenJWT != nil {
688
+ claims := f .hookAccessTokenJWT (email , expires )
689
+ accessToken = f .encodeClaims (t , claims )
690
+ }
691
+
681
692
f .accessTokens .Store (accessToken , token {
682
693
issued : time .Now (),
683
694
email : email ,
@@ -963,7 +974,7 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
963
974
email := getEmail (claims )
964
975
refreshToken := f .newRefreshTokens (email )
965
976
token := map [string ]interface {}{
966
- "access_token" : f .newToken (email , exp ),
977
+ "access_token" : f .newToken (t , email , exp ),
967
978
"refresh_token" : refreshToken ,
968
979
"token_type" : "Bearer" ,
969
980
"expires_in" : int64 ((f .defaultExpire ).Seconds ()),
@@ -1553,3 +1564,8 @@ d8h4Ht09E+f3nhTEc87mODkl7WJZpHL6V2sORfeq/eIkds+H6CJ4hy5w/bSw8tjf
1553
1564
sz9Di8sGIaUbLZI2rd0CQQCzlVwEtRtoNCyMJTTrkgUuNufLP19RZ5FpyXxBO5/u
1554
1565
QastnN77KfUwdj3SJt44U/uh1jAIv4oSLBr8HYUkbnI8
1555
1566
-----END RSA PRIVATE KEY-----`
1567
+
1568
+ func FakeIDPKey () (* rsa.PrivateKey , error ) {
1569
+ block , _ := pem .Decode ([]byte (testRSAPrivateKey ))
1570
+ return x509 .ParsePKCS1PrivateKey (block .Bytes )
1571
+ }
0 commit comments