@@ -78,9 +78,12 @@ type FakeIDP struct {
78
78
// "Authorized Redirect URLs". This can be used to emulate that.
79
79
hookValidRedirectURL func (redirectURL string ) error
80
80
hookUserInfo func (email string ) (jwt.MapClaims , error )
81
- hookMutateToken func (token map [string ]interface {})
82
- fakeCoderd func (req * http.Request ) (* http.Response , error )
83
- hookOnRefresh func (email string ) error
81
+ // defaultIDClaims is if a new client connects and we didn't preset
82
+ // some claims.
83
+ defaultIDClaims jwt.MapClaims
84
+ hookMutateToken func (token map [string ]interface {})
85
+ fakeCoderd func (req * http.Request ) (* http.Response , error )
86
+ hookOnRefresh func (email string ) error
84
87
// Custom authentication for the client. This is useful if you want
85
88
// to test something like PKI auth vs a client_secret.
86
89
hookAuthenticateClient func (t testing.TB , req * http.Request ) (url.Values , error )
@@ -162,6 +165,12 @@ func WithStaticUserInfo(info jwt.MapClaims) func(*FakeIDP) {
162
165
}
163
166
}
164
167
168
+ func WithDefaultIDClaims (claims jwt.MapClaims ) func (* FakeIDP ) {
169
+ return func (f * FakeIDP ) {
170
+ f .defaultIDClaims = claims
171
+ }
172
+ }
173
+
165
174
func WithDynamicUserInfo (userInfoFunc func (email string ) (jwt.MapClaims , error )) func (* FakeIDP ) {
166
175
return func (f * FakeIDP ) {
167
176
f .hookUserInfo = userInfoFunc
@@ -679,7 +688,7 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
679
688
// Always invalidate the code after it is used.
680
689
f .codeToStateMap .Delete (code )
681
690
682
- idTokenClaims , ok := f .stateToIDTokenClaims . Load ( stateStr )
691
+ idTokenClaims , ok := f .getClaims ( f . stateToIDTokenClaims , stateStr )
683
692
if ! ok {
684
693
t .Errorf ("missing id token claims" )
685
694
http .Error (rw , "missing id token claims" , http .StatusBadRequest )
@@ -699,7 +708,7 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
699
708
return
700
709
}
701
710
702
- idTokenClaims , ok := f .refreshIDTokenClaims . Load ( refreshToken )
711
+ idTokenClaims , ok := f .getClaims ( f . refreshIDTokenClaims , refreshToken )
703
712
if ! ok {
704
713
t .Errorf ("missing id token claims in refresh" )
705
714
http .Error (rw , "missing id token claims in refresh" , http .StatusBadRequest )
@@ -971,6 +980,10 @@ func (f *FakeIDP) ExternalAuthConfig(t testing.TB, id string, custom *ExternalAu
971
980
return cfg
972
981
}
973
982
983
+ func (f * FakeIDP ) AppCredentials () (clientID string , clientSecret string ) {
984
+ return f .clientID , f .clientSecret
985
+ }
986
+
974
987
// OIDCConfig returns the OIDC config to use for Coderd.
975
988
func (f * FakeIDP ) OIDCConfig (t testing.TB , scopes []string , opts ... func (cfg * coderd.OIDCConfig )) * coderd.OIDCConfig {
976
989
t .Helper ()
@@ -1023,6 +1036,17 @@ func (f *FakeIDP) OIDCConfig(t testing.TB, scopes []string, opts ...func(cfg *co
1023
1036
return cfg
1024
1037
}
1025
1038
1039
+ func (f * FakeIDP ) getClaims (m * syncmap.Map [string , jwt.MapClaims ], key string ) (jwt.MapClaims , bool ) {
1040
+ v , ok := m .Load (key )
1041
+ if ! ok {
1042
+ if f .defaultIDClaims != nil {
1043
+ return f .defaultIDClaims , true
1044
+ }
1045
+ return nil , false
1046
+ }
1047
+ return v , true
1048
+ }
1049
+
1026
1050
func httpErrorCode (defaultCode int , err error ) int {
1027
1051
var stautsErr statusHookError
1028
1052
status := defaultCode
0 commit comments