5
5
*/
6
6
var newSession = false ; // Used by oidcAuth() and validateIdToken()
7
7
8
+ const EXTRA_PARAMS = 1 ;
9
+ const REPLACE_PARAMS = 2 ;
10
+
8
11
export default {
9
12
auth,
10
13
codeExchange,
11
14
validateIdToken,
12
15
logout,
13
- v2logout,
14
16
redirectPostLogin,
15
- redirectPostLogout
17
+ redirectPostLogout,
18
+ userInfo
16
19
} ;
17
20
18
21
function retryOriginalRequest ( r ) {
@@ -112,7 +115,11 @@ function auth(r, afterSyncCheck) {
112
115
// ID Token is valid, update keyval
113
116
r . log ( "OIDC refresh success, updating id_token for " + r . variables . cookie_auth_token ) ;
114
117
r . variables . session_jwt = tokenset . id_token ; // Update key-value store
115
- r . variables . access_token = tokenset . access_token ;
118
+ if ( tokenset . access_token ) {
119
+ r . variables . access_token = tokenset . access_token ;
120
+ } else {
121
+ r . variables . access_token = "-" ;
122
+ }
116
123
117
124
// Update refresh token (if we got a new one)
118
125
if ( r . variables . refresh_token != tokenset . refresh_token ) {
@@ -196,7 +203,12 @@ function codeExchange(r) {
196
203
// Add opaque token to keyval session store
197
204
r . log ( "OIDC success, creating session " + r . variables . request_id ) ;
198
205
r . variables . new_session = tokenset . id_token ; // Create key-value store entry
199
- r . variables . new_access_token = tokenset . access_token ;
206
+ if ( tokenset . access_token ) {
207
+ r . variables . new_access_token = tokenset . access_token ;
208
+ } else {
209
+ r . variables . new_access_token = "-" ;
210
+ }
211
+
200
212
r . headersOut [ "Set-Cookie" ] = "auth_token=" + r . variables . request_id + "; " + r . variables . oidc_cookie_flags ;
201
213
r . return ( 302 , r . variables . redirect_base + r . variables . cookie_auth_redir ) ;
202
214
}
@@ -263,12 +275,31 @@ function validateIdToken(r) {
263
275
}
264
276
}
265
277
278
+ //
279
+ // Default RP-Initiated or Custom Logout w/ OP.
280
+ //
281
+ // - An RP requests that the OP log out the end-user by redirecting the
282
+ // end-user's User Agent to the OP's Logout endpoint.
283
+ // - https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RPLogout
284
+ // - https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RedirectionAfterLogout
285
+ //
266
286
function logout ( r ) {
267
287
r . log ( "OIDC logout for " + r . variables . cookie_auth_token ) ;
268
- r . variables . session_jwt = "-" ;
269
- r . variables . access_token = "-" ;
270
- r . variables . refresh_token = "-" ;
271
- r . return ( 302 , r . variables . oidc_logout_redirect ) ;
288
+ var idToken = r . variables . session_jwt ;
289
+ var queryParams = '?post_logout_redirect_uri=' +
290
+ r . variables . redirect_base +
291
+ r . variables . oidc_logout_redirect +
292
+ '&id_token_hint=' + idToken ;
293
+ if ( r . variables . oidc_end_session_query_params_option == REPLACE_PARAMS ) {
294
+ queryParams = '?' + r . variables . oidc_end_session_query_params ;
295
+ } else if ( r . variables . oidc_end_session_query_params_option == EXTRA_PARAMS ) {
296
+ queryParams += '&' + r . variables . oidc_end_session_query_params ;
297
+ }
298
+ r . variables . request_id = '-' ;
299
+ r . variables . session_jwt = '-' ;
300
+ r . variables . access_token = '-' ;
301
+ r . variables . refresh_token = '-' ;
302
+ r . return ( 302 , r . variables . oidc_end_session_endpoint + queryParams ) ;
272
303
}
273
304
274
305
function getAuthZArgs ( r ) {
@@ -312,66 +343,56 @@ function idpClientAuth(r) {
312
343
}
313
344
314
345
//
315
- // Redirect URI after logging in the IDP.
316
- function redirectPostLogin ( r ) {
317
- r . return ( 302 , r . variables . redirect_base + getIDTokenArgsAfterLogin ( r ) ) ;
318
- }
319
-
320
- //
321
- // Get query parameter of ID token after sucessful login:
322
- //
323
- // - For the variable of `returnTokenToClientOnLogin` of the APIM, this config
324
- // is only effective for /login endpoint. By default, our implementation MUST
325
- // not return any token back to the client app.
326
- // - If its configured it can send id_token in the request uri as
327
- // `?id_token=sdfsdfdsfs` after successful login.
328
- //
346
+ // Redirect URI after successful login from the OP.
329
347
//
330
- function getIDTokenArgsAfterLogin ( r ) {
331
- if ( r . variables . return_token_to_client_on_login == 'id_token' ) {
332
- return '?id_token=' + r . variables . id_token ;
348
+ function redirectPostLogin ( r ) {
349
+ if ( r . variables . oidc_landing_page ) {
350
+ r . return ( 302 , r . variables . oidc_landing_page ) ;
351
+ } else {
352
+ r . return ( 302 , r . variables . redirect_base + r . variables . cookie_auth_redir ) ;
333
353
}
334
- return '' ;
335
- }
336
-
337
- //
338
- // RP-Initiated or Custom Logout w/ Idp.
339
- //
340
- // - An RP requests that the Idp log out the end-user by redirecting the
341
- // end-user's User Agent to the Idp's Logout endpoint.
342
- // - TODO: Handle custom logout parameters if Idp doesn't support standard spec
343
- // of 'OpenID Connect RP-Initiated Logout 1.0'.
344
- //
345
- // - https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RPLogout
346
- // - https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RedirectionAfterLogout
347
- //
348
- function v2logout ( r ) {
349
- r . log ( "OIDC logout for " + r . variables . cookie_auth_token ) ;
350
- var idToken = r . variables . session_jwt ;
351
- var queryParams = getRPInitiatedLogoutArgs ( r , idToken ) ;
352
-
353
- r . variables . request_id = '-' ;
354
- r . variables . session_jwt = '-' ;
355
- r . variables . access_token = '-' ;
356
- r . variables . refresh_token = '-' ;
357
- r . return ( 302 , r . variables . oidc_end_session_endpoint + queryParams ) ;
358
354
}
359
355
360
356
//
361
- // Get query params for RP-initiated logout:
357
+ // Redirect URI after logged-out from the OP.
362
358
//
363
- // - https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RPLogout
364
- // - https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RedirectionAfterLogout
365
- //
366
- function getRPInitiatedLogoutArgs ( r , idToken ) {
367
- return '?post_logout_redirect_uri=' + r . variables . redirect_base
368
- + r . variables . oidc_logout_redirect_uri +
369
- '&id_token_hint=' + idToken ;
359
+ function redirectPostLogout ( r ) {
360
+ r . return ( 302 , r . variables . post_logout_return_uri ) ;
370
361
}
371
362
372
363
//
373
- // Redirect URI after logged-out from the IDP.
364
+ // Return necessary user info claims after receiving and extracting all claims
365
+ // that are received from the OpenID Connect Provider(OP).
374
366
//
375
- function redirectPostLogout ( r ) {
376
- r . return ( 302 , r . variables . post_logout_return_uri ) ;
377
- }
367
+ function userInfo ( r ) {
368
+ r . subrequest ( '/_userinfo' ,
369
+ function ( res ) {
370
+ if ( res . status == 200 ) {
371
+ var error_log = "OIDC userinfo JSON failure" ;
372
+ var claimsOP = '' ; // Claims that are received by the OP.
373
+ try {
374
+ claimsOP = JSON . parse ( res . responseBody ) ;
375
+ } catch ( e ) {
376
+ error_log += ": " + res . responseBody ;
377
+ r . error ( error_log ) ;
378
+ r . return ( 500 ) ;
379
+ return ;
380
+ }
381
+ // The claimsRP is to extract claims that are configured in
382
+ // $oidc_userinfo_response_data in the RP and send them to
383
+ // the client using the response of the OP.
384
+ var claimsRP = r . variables . oidc_userinfo_response_data . split ( "," ) ;
385
+ var ret = { } ;
386
+ for ( var i in claimsRP ) {
387
+ if ( claimsRP [ i ] in claimsOP ) {
388
+ ret [ claimsRP [ i ] ] = claimsOP [ claimsRP [ i ] ] ;
389
+ }
390
+ }
391
+ r . variables . user_info = JSON . stringify ( ret ) ;
392
+ r . return ( 200 , r . variables . user_info ) ;
393
+ } else {
394
+ r . return ( res . status )
395
+ }
396
+ }
397
+ ) ;
398
+ }
0 commit comments