55 */
66
77// Constants for common error message. These will be cleaned up.
8- var ERR_CFG_VARS = 'OIDC missing configuration variables: ' ;
9- var ERR_AC_TOKEN = 'OIDC Access Token validation error: ' ;
10- var ERR_ID_TOKEN = 'OIDC ID Token validation error: ' ;
11- var ERR_IDP_AUTH = 'OIDC unexpected response from IdP in code exchange' ;
12- var ERR_TOKEN_RES = 'OIDC AuthZ code sent but token response is not JSON. ' ;
13- var ERR_X_CLIENT_ID_COOKIE = 'X-Client-Id should be in cookie' ;
14- var ERR_X_CLIENT_ID_NOT_FOUND = 'X-Client-Id not found in the IdP app' ;
15- var WRN_SESSION = 'OIDC session is invalid' ;
16- var INF_REFRESH_TOKEN = 'OIDC refresh success, updating tokens for ' ;
17- var INF_REPLACE_TOKEN = 'OIDC replacing previous refresh token (' ;
8+ var ERR_CFG_VARS = 'OIDC missing configuration variables' ;
9+ var ERR_AC_TOKEN = 'OIDC Access Token validation error' ;
10+ var ERR_ID_TOKEN = 'OIDC ID Token validation error' ;
11+ var ERR_IDP_AUTH = 'OIDC unexpected response from IdP in code exchange' ;
12+ var ERR_TOKEN_RES = 'OIDC AuthZ code sent but token response is not JSON' ;
13+ var ERR_CLIENT_ID = 'Check if cookie is removed, and client_id is there' ;
14+ var ERR_IDP_APP_NAME = 'IdP app is not set in $oidc_app_name' ;
15+ var WRN_SESSION = 'OIDC session is invalid' ;
16+ var INF_SESSION = 'OIDC session is valid' ;
17+ var INF_REFRESH_TOKEN = 'OIDC refresh success, updating tokens for ' ;
18+ var INF_REPLACE_TOKEN = 'OIDC replacing previous refresh token' ;
1819
1920// Flag to check if there is still valid session cookie. It is used by auth()
2021// and validateIdToken().
@@ -133,11 +134,25 @@ function validateIdToken(r) {
133134//
134135// - https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowTokenValidation
135136// - https://openid.net/specs/openid-connect-core-1_0.html#ImplicitTokenValidation
137+ //
136138// - This function is called by the location of `_access_token_validation` which
137139// is called by either OIDC code exchange or refersh token request.
140+ //
138141// - The 'aud' claim isn't contained in general ID token from Amazon Cognito,
139142// although we can add it. Hence, the claim isn't part of this validation.
140143//
144+ // - This function is for the case when you want to validate the token within
145+ // NGINX layer to following the spec. of OpenID Connect Core 1.0.
146+ //
147+ // - But, this token is mostly validated by using one of following options.
148+ //
149+ // + Option 1. validate a token (assumtion: JWT format) by using
150+ // a NGINX auth_jwt directive to validate it via IdP URI.
151+ // auth_jwt "" token=$access_token;
152+ // auth_jwt_key_request /_jwks_uri;
153+ //
154+ // + Option 2. validate a token by using IdP token introspection endpoint.
155+ //
141156function validateAccessToken ( r ) {
142157 var missingClaims = [ ]
143158 if ( ! isValidRequiredClaims ( r , ERR_AC_TOKEN , missingClaims ) ) {
@@ -208,7 +223,6 @@ function generateCustomEndpoint(r, uri, isEnableCustomPath, paths) {
208223 var res = '' ;
209224 var key = '' ;
210225 var isKey = false ;
211- r . log ( '### paths: ' + paths )
212226 var items = JSON . parse ( paths ) ;
213227 for ( var i = 0 ; i < uri . length ; i ++ ) {
214228 switch ( uri [ i ] ) {
@@ -271,7 +285,7 @@ function startIdPAuthZ(r) {
271285 }
272286 }
273287 if ( missingConfig . length ) {
274- r . error ( ERR_CFG_VARS + '$oidc_' + missingConfig . join ( ' $oidc_' ) ) ;
288+ r . error ( ERR_CFG_VARS + ': $oidc_' + missingConfig . join ( ' $oidc_' ) ) ;
275289 r . return ( 500 , r . variables . internal_error_message ) ;
276290 return ;
277291 }
@@ -335,7 +349,7 @@ function handleSuccessfulRefreshResponse(r, res) {
335349 // Update new refresh token to key/value store if we got a new one.
336350 r . log ( INF_REFRESH_TOKEN + r . variables . cookie_session_id ) ;
337351 if ( r . variables . refresh_token != tokenset . refresh_token ) {
338- r . log ( INF_REPLACE_TOKEN + r . variables . refresh_token +
352+ r . log ( INF_REPLACE_TOKEN + ' (' + r . variables . refresh_token +
339353 ') with new value: ' + tokenset . refresh_token ) ;
340354 r . variables . refresh_token = tokenset . refresh_token ;
341355 }
@@ -473,7 +487,7 @@ function handleSuccessfulTokenResponse(r, res) {
473487 '; ' + r . variables . oidc_cookie_flags ;
474488 r . return ( 302 , r . variables . redirect_base + r . variables . cookie_auth_redir ) ;
475489 } catch ( e ) {
476- r . error ( ERR_TOKEN_RES + res . responseBody ) ;
490+ r . error ( ERR_TOKEN_RES + ' ' + res . responseBody ) ;
477491 r . return ( 502 ) ;
478492 }
479493}
@@ -512,7 +526,8 @@ function getAuthZArgs(r) {
512526 '&client_id=' + r . variables . oidc_client +
513527 '&redirect_uri=' + redirectURI +
514528 // uncomment when to need claims of access token in Auth0.
515- // '&audience=' + 'https://{{domain}} }/api/v2/' +
529+ // '&audience=' + 'https://{{domain}} }/api/v2/' +
530+ '&audience=' + 'https://dev-s4i2bm4p.us.auth0.com/api/v2/' +
516531 '&nonce=' + nonceHash ;
517532 var cookieFlags = r . variables . oidc_cookie_flags ;
518533 r . headersOut [ 'Set-Cookie' ] = [
@@ -704,7 +719,7 @@ function isValidRequiredClaims(r, msgPrefix, missingClaims) {
704719 }
705720 }
706721 if ( missingClaims . length ) {
707- r . error ( msgPrefix + 'missing claim(s) ' + missingClaims . join ( ' ' ) ) ;
722+ r . error ( msgPrefix + ': missing claim(s) ' + missingClaims . join ( ' ' ) ) ;
708723 return false ;
709724 }
710725 } catch ( e ) {
@@ -733,6 +748,8 @@ function isValidTokenSet(r, tokenset) {
733748 // The validateIdToken() logs error so that r.error() isn't used.
734749 return isErr ;
735750 }
751+ // The access token is mostly validated by IdP using auth_jwt directive.
752+ // This can be used when you want to validate the token set in NGINX.
736753 if ( r . variables . access_token_validation_enable == 1 &&
737754 ! isValidToken ( r , '/_access_token_validation' , tokenset . access_token ) ) {
738755 // The validateAccessToken() logs error so that r.error() isn't used.
@@ -804,12 +821,12 @@ function isValidSession(r) {
804821// the session cookie could play from any client (browsers or command line).
805822//
806823function validateSession ( r ) {
807- if ( r . variables . session_validation_enable == 1 && ! isValidSession ( r ) ) {
824+ if ( ! isValidSession ( r ) ) {
808825 r . warn ( WRN_SESSION )
809826 r . return ( 401 , '{"message": "' + WRN_SESSION + '"}\n' )
810827 return false ;
811828 }
812- r . return ( 200 , '{"message": "' + WRN_SESSION + '"}\n' )
829+ r . return ( 200 , '{"message": "' + INF_SESSION + '"}\n' )
813830 return true ;
814831}
815832
@@ -820,14 +837,13 @@ function validateSession(r) {
820837function isValidXClientId ( r ) {
821838 if ( r . variables . client_id_validation_enable == 1 ) {
822839 if ( ! r . variables . cookie_client_id ) {
823- r . warn ( ERR_X_CLIENT_ID_COOKIE )
824- r . return ( 400 , '{"message": "' + ERR_X_CLIENT_ID_COOKIE + '"}\n' )
840+ r . warn ( ERR_CLIENT_ID )
841+ r . return ( 400 , '{"message": "' + ERR_CLIENT_ID + '"}\n' )
825842 return false
826843 }
827844 if ( r . variables . oidc_app_name == '' ) {
828- var errMsg = ERR_X_CLIENT_ID_NOT_FOUND + ': ' + r . variables . cookie_client_id ;
829- r . warn ( errMsg )
830- r . return ( 404 , '{"message": "' + errMsg + '"}\n' )
845+ r . warn ( ERR_IDP_APP_NAME )
846+ r . return ( 404 , '{"message": "' + ERR_IDP_APP_NAME + '"}\n' )
831847 return false
832848 }
833849 }
0 commit comments