Skip to content

Commit 62b5e04

Browse files
authored
Merge pull request #52 from route443/main
Fix: ZoneSync delays in some installations caused problems Fix: Double URL encoding in proxy upstream after internalRedirect
2 parents db991ec + bd4e9cb commit 62b5e04

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

openid_connect.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,30 @@ var newSession = false; // Used by oidcAuth() and validateIdToken()
77

88
export 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) {

openid_connect_configuration.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ map $host $oidc_hmac_key {
4343
default "ChangeMe";
4444
}
4545

46+
map $host $zone_sync_leeway {
47+
# Specifies the maximum timeout for synchronizing ID tokens between cluster
48+
# nodes when you use shared memory zone content sync. This option is only
49+
# recommended for scenarios where cluster nodes can randomly process
50+
# requests from user agents and there may be a situation where node "A"
51+
# successfully received a token, and node "B" receives the next request in
52+
# less than zone_sync_interval.
53+
default 0; # Time in milliseconds, e.g. (zone_sync_interval * 2 * 1000)
54+
}
55+
4656
map $proto $oidc_cookie_flags {
4757
http "Path=/; SameSite=lax;"; # For HTTP/plaintext testing
4858
https "Path=/; SameSite=lax; HttpOnly; Secure;"; # Production recommendation

0 commit comments

Comments
 (0)