55 */
66var newSession = false ; // Used by oidcAuth() and validateIdToken()
77
8+ const EXTRA_PARAMS = 1 ;
9+ const REPLACE_PARAMS = 2 ;
10+
811export default { auth, codeExchange, validateIdToken, logout} ;
912
1013function 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
293299function idpClientAuth ( r ) {
0 commit comments