Skip to content

Commit 150129c

Browse files
committed
Fix logout
1 parent c87604a commit 150129c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ If a [refresh token](https://openid.net/specs/openid-connect-core-1_0.html#Refre
3636

3737
### Logout
3838

39-
Requests made to the `/logout` location invalidate both the ID token and refresh token by erasing them from the key-value store. Therefore, subsequent requests to protected resources will be treated as a first-time request and send the client to the IdP for authentication.
39+
Requests made to the `/logout` location invalidate both the ID token and refresh token by erasing them from the key-value store. Therefore, subsequent requests to protected resources will be treated as a first-time request and send the client to the IdP for authentication. Note that the IdP may issue cookies such that an authenticated session still exists at the IdP.
4040

4141
## Installation
4242

@@ -157,6 +157,10 @@ Any errors generated by the OpenID Connect flow are logged in a separate file, `
157157
* Check the error log `/var/log/nginx/oidc_error.log` for JWT/JWK errors.
158158
* Ensure that the JWK file (`$oidc_jwt_keyfile` variable) is correct and that the nginx user has permission to read it.
159159

160+
* **Logged out but next request does not require authentication**
161+
* This is typically caused by the IdP issuing its own session cookie(s) to the client. NGINX Plus sends the request to the IdP for authentication and the IdP immediately sends back a new authorization code because the session is still valid.
162+
* Check your IdP configuration if this behavior is not desired.
163+
160164
## Support
161165

162166
This reference implementation for OpenID Connect is supported for NGINX Plus subscribers.

frontend.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ keyval $cookie_auth_token $refresh_token zone=refresh_tokens; # Exchange cookie
2020
keyval $request_id $new_session zone=opaque_sessions; # For initial session creation
2121
keyval $request_id $new_refresh zone=refresh_tokens; # "
2222

23+
map $refresh_token $no_refresh {
24+
"" 1; # Before login
25+
"-" 1; # After logout
26+
default 0;
27+
}
28+
2329
# JWK Set will be fetched from $oidc_jwks_uri and cached here - ensure writable by nginx user
2430
proxy_cache_path /var/cache/nginx/jwk levels=1 keys_zone=jwk:64k max_size=1m;
2531

openid_connect.server_conf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
location @oidc_auth {
2-
if ($refresh_token = "") {
2+
if ($no_refresh) {
33
# No refresh token so redirect this request to the OpenID Connect identity provider login
44
# page for this server{} using authorization code flow (nonce sent to IdP is hash of $request_id)
55
add_header Set-Cookie "auth_nonce=$request_id; Path=/; HttpOnly;"; # Random value
@@ -87,14 +87,15 @@
8787
}
8888

8989
location = /logout {
90-
set $session_jwt "-"; # Clear tokens from keyval, set to - to indicate logout,
91-
set $refresh_token "-"; # and so that the new value is propagated by zone_sync.
92-
add_header Set-Cookie "auth_token=; HttpOnly;";
93-
return 302 "$oidc_logout_redirect";
94-
access_log /var/log/nginx/oidc_auth.log main_jwt;
90+
set $session_jwt -; # Clear tokens from keyval, set to - to indicate logout,
91+
set $refresh_token -; # and so that the new value is propagated by zone_sync.
92+
add_header Set-Cookie "auth_token=; Path=/; HttpOnly;"; # Send empty cookie
93+
add_header Set-Cookie "auth_redir=; Path=/; HttpOnly;"; # Erase original cookie
94+
return 302 $oidc_logout_redirect;
9595
}
9696

9797
location = /_logout {
98+
# This location is the default value of $oidc_logout_redirect (in case it wasn't configured)
9899
default_type text/plain;
99100
return 200 "Logged out\n";
100101
}

0 commit comments

Comments
 (0)