Skip to content

Commit bb97a82

Browse files
committed
Enhance custom query params for authz endpoint
1 parent 6ad8ec6 commit bb97a82

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

openid_connect.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
var newSession = false; // Used by oidcAuth() and validateIdToken()
77

8+
const EXTRA_PARAMS = 1;
9+
const REPLACE_PARAMS = 2;
10+
811
export default {auth, codeExchange, validateIdToken, logout};
912

1013
function retryOriginalRequest(r) {
@@ -48,7 +51,7 @@ function auth(r, afterSyncCheck) {
4851
return;
4952
}
5053
// Redirect the client to the IdP login page with the cookies we need for state
51-
r.return(302, r.variables.oidc_authz_endpoint + getAuthZArgs(r));
54+
r.return(302, r.variables.oidc_authz_endpoint + getQueryParamsAuthZ(r));
5255
return;
5356
}
5457

@@ -260,18 +263,15 @@ function logout(r) {
260263
r.return(302, r.variables.oidc_logout_redirect);
261264
}
262265

263-
function getAuthZArgs(r) {
266+
function getQueryParamsAuthZ(r) {
264267
// Choose a nonce for this flow for the client, and hash it for the IdP
265268
var noncePlain = r.variables.request_id;
266269
var c = require('crypto');
267270
var h = c.createHmac('sha256', r.variables.oidc_hmac_key).update(noncePlain);
268271
var nonceHash = h.digest('base64url');
269-
var authZArgs = "?response_type=code&scope=" + r.variables.oidc_scopes + "&client_id=" + r.variables.oidc_client + "&redirect_uri="+ r.variables.redirect_base + r.variables.redir_location + "&nonce=" + nonceHash;
270-
271-
if (r.variables.oidc_authz_extra_args) {
272-
authZArgs += "&" + r.variables.oidc_authz_extra_args;
273-
}
272+
var queryParams = "?response_type=code&scope=" + r.variables.oidc_scopes + "&client_id=" + r.variables.oidc_client + "&redirect_uri="+ r.variables.redirect_base + r.variables.redir_location + "&nonce=" + nonceHash;
274273

274+
r.variables.nonce_hash = nonceHash;
275275
r.headersOut['Set-Cookie'] = [
276276
"auth_redir=" + r.variables.request_uri + "; " + r.variables.oidc_cookie_flags,
277277
"auth_nonce=" + noncePlain + "; " + r.variables.oidc_cookie_flags
@@ -282,12 +282,18 @@ function getAuthZArgs(r) {
282282
r.variables.pkce_id = c.createHash('sha256').update(String(Math.random())).digest('base64url');
283283
var pkce_code_challenge = c.createHash('sha256').update(pkce_code_verifier).digest('base64url');
284284
r.variables.pkce_code_verifier = pkce_code_verifier;
285+
r.variables.pkce_code_challenge = pkce_code_challenge;
285286

286-
authZArgs += "&code_challenge_method=S256&code_challenge=" + pkce_code_challenge + "&state=" + r.variables.pkce_id;
287+
queryParams += "&code_challenge_method=S256&code_challenge=" + pkce_code_challenge + "&state=" + r.variables.pkce_id;
287288
} else {
288-
authZArgs += "&state=0";
289+
queryParams += "&state=0";
290+
}
291+
if (r.variables.oidc_authz_query_params_option == REPLACE_PARAMS) {
292+
queryParams = '?' + r.variables.oidc_authz_query_params;
293+
} else if (r.variables.oidc_authz_query_params_option == EXTRA_PARAMS) {
294+
queryParams += '&' + r.variables.oidc_authz_query_params;
289295
}
290-
return authZArgs;
296+
return queryParams;
291297
}
292298

293299
function idpClientAuth(r) {

openid_connect_configuration.conf

+20-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ map $host $oidc_authz_endpoint {
88
#www.example.com "https://my-idp/oauth2/v1/authorize";
99
}
1010

11-
map $host $oidc_authz_extra_args {
12-
# Extra arguments to include in the request to the IdP's authorization
13-
# endpoint.
14-
# Some IdPs provide extended capabilities controlled by extra arguments,
15-
# for example Keycloak can select an IdP to delegate to via the
16-
# "kc_idp_hint" argument.
17-
# Arguments must be expressed as query string parameters and URL-encoded
11+
map $host $oidc_authz_query_params_option {
12+
# The option of custom query params in the request of $oidc_authz_endpoint.
13+
# 0: built-in params (e.g. response_type, client_id, redirect_uri, nonce)
14+
# 1: extra args($oidc_authz_query_params) are extended after built-in params
15+
# 2: replace built-in params with custom params($oidc_authz_query_params)
16+
default 0;
17+
}
18+
19+
map $host $oidc_authz_query_params {
20+
# Each IdP may use different query params of the $oidc_authz_endpoint. For
21+
# example, Keycloak can select an IdP to delegate to via the "kc_idp_hint"
22+
# argument. It must be expressed as query string parameters and URL-encoded
1823
# if required.
1924
default "";
20-
#www.example.com "kc_idp_hint=another_provider"
25+
#extra.args.example "kc_idp_hint=another_provider";
26+
#replace.args.example "response_type=code&scope=$oidc_scopes&client_id=$oidc_client&redirect_uri=$redirect_base$redir_location&nonce=$nonce_hash&state=0&audience=https://auth0.com/api/v2/";
2127
}
2228

2329
map $host $oidc_token_endpoint {
@@ -89,13 +95,17 @@ proxy_cache_path /var/cache/nginx/jwk levels=1 keys_zone=jwk:64k max_size=1m;
8995
# Change timeout values to at least the validity period of each token type
9096
keyval_zone zone=oidc_id_tokens:1M state=conf.d/oidc_id_tokens.json timeout=1h;
9197
keyval_zone zone=refresh_tokens:1M state=conf.d/refresh_tokens.json timeout=8h;
92-
keyval_zone zone=oidc_pkce:128K timeout=90s; # Temporary storage for PKCE code verifier.
98+
keyval_zone zone=oidc_pkce_verifier:128K timeout=90s; # Temporary storage for PKCE code verifier.
99+
keyval_zone zone=oidc_pkce_challenge:128K timeout=90s; # Temporary storage for PKCE code challenge.
100+
keyval_zone zone=oidc_nonce:128K timeout=90s; # Temporary storage for nonce.
93101

94102
keyval $cookie_auth_token $session_jwt zone=oidc_id_tokens; # Exchange cookie for JWT
95103
keyval $cookie_auth_token $refresh_token zone=refresh_tokens; # Exchange cookie for refresh token
96104
keyval $request_id $new_session zone=oidc_id_tokens; # For initial session creation
97105
keyval $request_id $new_refresh zone=refresh_tokens; # ''
98-
keyval $pkce_id $pkce_code_verifier zone=oidc_pkce;
106+
keyval $request_id $nonce_hash zone=oidc_nonce;
107+
keyval $pkce_id $pkce_code_verifier zone=oidc_pkce_verifier;
108+
keyval $pkce_id $pkce_code_challenge zone=oidc_pkce_challenge;
99109

100110
auth_jwt_claim_set $jwt_audience aud; # In case aud is an array
101111
js_import oidc from conf.d/openid_connect.js;

0 commit comments

Comments
 (0)