@@ -98,17 +98,6 @@ func isProviderUnavailable(statusCode int) bool {
98
98
return statusCode == http .StatusTooManyRequests || statusCode == http .StatusServiceUnavailable
99
99
}
100
100
101
- func extendDeadline (ttl time.Duration ) time.Time {
102
- return time .Now ().Add (ttl ).Truncate (time .Second )
103
- }
104
-
105
- func (p * SSOProvider ) withinGracePeriod (s * sessions.SessionState ) bool {
106
- if s .GracePeriodStart .IsZero () {
107
- s .GracePeriodStart = time .Now ()
108
- }
109
- return s .GracePeriodStart .Add (p .GracePeriodTTL ).After (time .Now ())
110
- }
111
-
112
101
// Redeem takes a redirectURL and code and redeems the SessionState
113
102
func (p * SSOProvider ) Redeem (redirectURL , code string ) (* sessions.SessionState , error ) {
114
103
if code == "" {
@@ -165,9 +154,9 @@ func (p *SSOProvider) Redeem(redirectURL, code string) (*sessions.SessionState,
165
154
AccessToken : jsonResponse .AccessToken ,
166
155
RefreshToken : jsonResponse .RefreshToken ,
167
156
168
- RefreshDeadline : extendDeadline (time .Duration (jsonResponse .ExpiresIn ) * time .Second ),
169
- LifetimeDeadline : extendDeadline (p .SessionLifetimeTTL ),
170
- ValidDeadline : extendDeadline (p .SessionValidTTL ),
157
+ RefreshDeadline : sessions . ExtendDeadline (time .Duration (jsonResponse .ExpiresIn ) * time .Second ),
158
+ LifetimeDeadline : sessions . ExtendDeadline (p .SessionLifetimeTTL ),
159
+ ValidDeadline : sessions . ExtendDeadline (p .SessionValidTTL ),
171
160
172
161
Email : jsonResponse .Email ,
173
162
User : user ,
@@ -245,9 +234,9 @@ func (p *SSOProvider) UserGroups(email string, groups []string, accessToken stri
245
234
return jsonResponse .Groups , nil
246
235
}
247
236
248
- // RefreshSession takes a SessionState and allowedGroups and refreshes the session access token,
237
+ // RefreshSession takes a SessionState and refreshes the session access token,
249
238
// returns `true` on success, and `false` on error
250
- func (p * SSOProvider ) RefreshSession (s * sessions.SessionState , allowedGroups [] string ) (bool , error ) {
239
+ func (p * SSOProvider ) RefreshSession (s * sessions.SessionState ) (bool , error ) {
251
240
logger := log .NewLogEntry ()
252
241
253
242
if s .RefreshToken == "" {
@@ -259,35 +248,17 @@ func (p *SSOProvider) RefreshSession(s *sessions.SessionState, allowedGroups []s
259
248
// When we detect that the auth provider is not explicitly denying
260
249
// authentication, and is merely unavailable, we refresh and continue
261
250
// as normal during the "grace period"
262
- if err == ErrAuthProviderUnavailable && p . withinGracePeriod ( s ) {
251
+ if err == ErrAuthProviderUnavailable && s . IsWithinGracePeriod ( p . GracePeriodTTL ) {
263
252
tags := []string {"action:refresh_session" , "error:redeem_token_failed" }
264
253
p .StatsdClient .Incr ("provider_error_fallback" , tags , 1.0 )
265
- s .RefreshDeadline = extendDeadline (p .SessionValidTTL )
254
+ s .RefreshDeadline = sessions . ExtendDeadline (p .SessionValidTTL )
266
255
return true , nil
267
256
}
268
257
return false , err
269
258
}
270
259
271
- inGroups , validGroup , err := p .ValidateGroup (s .Email , allowedGroups , newToken )
272
- if err != nil {
273
- // When we detect that the auth provider is not explicitly denying
274
- // authentication, and is merely unavailable, we refresh and continue
275
- // as normal during the "grace period"
276
- if err == ErrAuthProviderUnavailable && p .withinGracePeriod (s ) {
277
- tags := []string {"action:refresh_session" , "error:user_groups_failed" }
278
- p .StatsdClient .Incr ("provider_error_fallback" , tags , 1.0 )
279
- s .RefreshDeadline = extendDeadline (p .SessionValidTTL )
280
- return true , nil
281
- }
282
- return false , err
283
- }
284
- if ! validGroup {
285
- return false , errors .New ("Group membership revoked" )
286
- }
287
- s .Groups = inGroups
288
-
289
260
s .AccessToken = newToken
290
- s .RefreshDeadline = extendDeadline (duration )
261
+ s .RefreshDeadline = sessions . ExtendDeadline (duration )
291
262
s .GracePeriodStart = time.Time {}
292
263
logger .WithUser (s .Email ).WithRefreshDeadline (s .RefreshDeadline ).Info ("refreshed session access token" )
293
264
return true , nil
@@ -340,16 +311,16 @@ func (p *SSOProvider) redeemRefreshToken(refreshToken string) (token string, exp
340
311
return
341
312
}
342
313
343
- // ValidateSessionState takes a sessionState and allowedGroups and validates the session state
344
- func (p * SSOProvider ) ValidateSessionState (s * sessions.SessionState , allowedGroups [] string ) bool {
314
+ // ValidateSessionToken takes a sessionState and validates the session token
315
+ func (p * SSOProvider ) ValidateSessionToken (s * sessions.SessionState ) bool {
345
316
logger := log .NewLogEntry ()
346
317
347
318
// we validate the user's access token is valid
348
319
params := url.Values {}
349
320
params .Add ("client_id" , p .ClientID )
350
321
req , err := p .newRequest ("GET" , fmt .Sprintf ("%s?%s" , p .ValidateURL .String (), params .Encode ()), nil )
351
322
if err != nil {
352
- logger .WithUser (s .Email ).Error (err , "error validating session state " )
323
+ logger .WithUser (s .Email ).Error (err , "error validating session access token " )
353
324
return false
354
325
}
355
326
@@ -358,52 +329,29 @@ func (p *SSOProvider) ValidateSessionState(s *sessions.SessionState, allowedGrou
358
329
359
330
resp , err := httpClient .Do (req )
360
331
if err != nil {
361
- logger .WithUser (s .Email ).Error ("error making request to validate access token" )
332
+ logger .WithUser (s .Email ).Error ("error making request to validate session access token" )
362
333
return false
363
334
}
364
335
365
336
if resp .StatusCode != 200 {
366
337
// When we detect that the auth provider is not explicitly denying
367
338
// authentication, and is merely unavailable, we validate and continue
368
339
// as normal during the "grace period"
369
- if isProviderUnavailable (resp .StatusCode ) && p . withinGracePeriod ( s ) {
340
+ if isProviderUnavailable (resp .StatusCode ) && s . IsWithinGracePeriod ( p . GracePeriodTTL ) {
370
341
tags := []string {"action:validate_session" , "error:validation_failed" }
371
342
p .StatsdClient .Incr ("provider_error_fallback" , tags , 1.0 )
372
- s .ValidDeadline = extendDeadline (p .SessionValidTTL )
343
+ s .ValidDeadline = sessions . ExtendDeadline (p .SessionValidTTL )
373
344
return true
374
345
}
375
346
logger .WithUser (s .Email ).WithHTTPStatus (resp .StatusCode ).Info (
376
- "could not validate user access token" )
377
- return false
378
- }
379
-
380
- // check the user is in the proper group(s)
381
- inGroups , validGroup , err := p .ValidateGroup (s .Email , allowedGroups , s .AccessToken )
382
- if err != nil {
383
- // When we detect that the auth provider is not explicitly denying
384
- // authentication, and is merely unavailable, we validate and continue
385
- // as normal during the "grace period"
386
- if err == ErrAuthProviderUnavailable && p .withinGracePeriod (s ) {
387
- tags := []string {"action:validate_session" , "error:user_groups_failed" }
388
- p .StatsdClient .Incr ("provider_error_fallback" , tags , 1.0 )
389
- s .ValidDeadline = extendDeadline (p .SessionValidTTL )
390
- return true
391
- }
392
- logger .WithUser (s .Email ).Error (err , "error fetching group memberships" )
393
- return false
394
- }
395
-
396
- if ! validGroup {
397
- logger .WithUser (s .Email ).WithAllowedGroups (allowedGroups ).Info (
398
- "user is no longer in valid groups" )
347
+ "could not validate session access token" )
399
348
return false
400
349
}
401
- s .Groups = inGroups
402
350
403
- s .ValidDeadline = extendDeadline (p .SessionValidTTL )
351
+ s .ValidDeadline = sessions . ExtendDeadline (p .SessionValidTTL )
404
352
s .GracePeriodStart = time.Time {}
405
353
406
- logger .WithUser (s .Email ).WithSessionValid (s .ValidDeadline ).Info ("validated session" )
354
+ logger .WithUser (s .Email ).WithSessionValid (s .ValidDeadline ).Info ("validated session access token " )
407
355
408
356
return true
409
357
}
0 commit comments