@@ -89,7 +89,8 @@ type FakeIDP struct {
89
89
hookAuthenticateClient func (t testing.TB ,req * http.Request ) (url.Values ,error )
90
90
serve bool
91
91
// optional middlewares
92
- middlewares chi.Middlewares
92
+ middlewares chi.Middlewares
93
+ defaultExpire time.Duration
93
94
}
94
95
95
96
func StatusError (code int ,err error )error {
@@ -134,6 +135,23 @@ func WithRefresh(hook func(email string) error) func(*FakeIDP) {
134
135
}
135
136
}
136
137
138
+ func WithDefaultExpire (d time.Duration )func (* FakeIDP ) {
139
+ return func (f * FakeIDP ) {
140
+ f .defaultExpire = d
141
+ }
142
+ }
143
+
144
+ func WithStaticCredentials (id ,secret string )func (* FakeIDP ) {
145
+ return func (f * FakeIDP ) {
146
+ if id != "" {
147
+ f .clientID = id
148
+ }
149
+ if secret != "" {
150
+ f .clientSecret = secret
151
+ }
152
+ }
153
+ }
154
+
137
155
// WithExtra returns extra fields that be accessed on the returned Oauth Token.
138
156
// These extra fields can override the default fields (id_token, access_token, etc).
139
157
func WithMutateToken (mutateToken func (token map [string ]interface {}))func (* FakeIDP ) {
@@ -219,6 +237,7 @@ func NewFakeIDP(t testing.TB, opts ...FakeIDPOpt) *FakeIDP {
219
237
hookOnRefresh :func (_ string )error {return nil },
220
238
hookUserInfo :func (email string ) (jwt.MapClaims ,error ) {return jwt.MapClaims {},nil },
221
239
hookValidRedirectURL :func (redirectURL string )error {return nil },
240
+ defaultExpire :time .Minute * 5 ,
222
241
}
223
242
224
243
for _ ,opt := range opts {
@@ -272,8 +291,23 @@ func (f *FakeIDP) updateIssuerURL(t testing.TB, issuer string) {
272
291
func (f * FakeIDP )realServer (t testing.TB )* httptest.Server {
273
292
t .Helper ()
274
293
294
+ srvURL := "localhost:0"
295
+ issURL ,err := url .Parse (f .issuer )
296
+ if err == nil {
297
+ if issURL .Hostname ()== "localhost" || issURL .Hostname ()== "127.0.0.1" {
298
+ srvURL = issURL .Host
299
+ }
300
+ }
301
+
302
+ l ,err := net .Listen ("tcp" ,srvURL )
303
+ require .NoError (t ,err ,"failed to create listener" )
304
+
275
305
ctx ,cancel := context .WithCancel (context .Background ())
276
- srv := httptest .NewUnstartedServer (f .handler )
306
+ srv := & httptest.Server {
307
+ Listener :l ,
308
+ Config :& http.Server {Handler :f .handler },
309
+ }
310
+
277
311
srv .Config .BaseContext = func (_ net.Listener ) context.Context {
278
312
return ctx
279
313
}
@@ -731,15 +765,15 @@ func (f *FakeIDP) httpHandler(t testing.TB) http.Handler {
731
765
return
732
766
}
733
767
734
- exp := time .Now ().Add (time . Minute * 5 )
768
+ exp := time .Now ().Add (f . defaultExpire )
735
769
claims ["exp" ]= exp .UnixMilli ()
736
770
email := getEmail (claims )
737
771
refreshToken := f .newRefreshTokens (email )
738
772
token := map [string ]interface {}{
739
773
"access_token" :f .newToken (email ),
740
774
"refresh_token" :refreshToken ,
741
775
"token_type" :"Bearer" ,
742
- "expires_in" :int64 ((time . Minute * 5 ).Seconds ()),
776
+ "expires_in" :int64 ((f . defaultExpire ).Seconds ()),
743
777
"id_token" :f .encodeClaims (t ,claims ),
744
778
}
745
779
if f .hookMutateToken != nil {