@@ -7,7 +7,30 @@ var newSession = false; // Used by oidcAuth() and validateIdToken()
77
88export default { auth, codeExchange, validateIdToken, logout} ;
99
10- function auth ( r ) {
10+ function retryOriginalRequest ( r ) {
11+ delete r . headersOut [ "WWW-Authenticate" ] ; // Remove evidence of original failed auth_jwt
12+ r . internalRedirect ( r . variables . uri + r . variables . is_args + ( r . variables . args || '' ) ) ;
13+ }
14+
15+ // If the ID token has not been synced yet, poll the variable every 100ms until
16+ // get a value or after a timeout.
17+ function waitForSessionSync ( r , timeLeft ) {
18+ if ( r . variables . session_jwt ) {
19+ retryOriginalRequest ( r ) ;
20+ } else if ( timeLeft > 0 ) {
21+ setTimeout ( waitForSessionSync , 100 , r , timeLeft - 100 ) ;
22+ } else {
23+ auth ( r , true ) ;
24+ }
25+ }
26+
27+ function auth ( r , afterSyncCheck ) {
28+ // If a cookie was sent but the ID token is not in the key-value database, wait for the token to be in sync.
29+ if ( r . variables . cookie_auth_token && ! r . variables . session_jwt && ! afterSyncCheck && r . variables . zone_sync_leeway > 0 ) {
30+ waitForSessionSync ( r , r . variables . zone_sync_leeway ) ;
31+ return ;
32+ }
33+
1134 if ( ! r . variables . refresh_token || r . variables . refresh_token == "-" ) {
1235 newSession = true ;
1336
@@ -88,8 +111,7 @@ function auth(r) {
88111 r . variables . refresh_token = tokenset . refresh_token ; // Update key-value store
89112 }
90113
91- delete r . headersOut [ "WWW-Authenticate" ] ; // Remove evidence of original failed auth_jwt
92- r . internalRedirect ( r . variables . request_uri ) ; // Continue processing original request
114+ retryOriginalRequest ( r ) ; // Continue processing original request
93115 }
94116 ) ;
95117 } catch ( e ) {
0 commit comments