33 *
44 * autoscript.ts
55 *
6- * Copyright (c) 2020 - 2025 Ping Identity Corporation . All rights reserved.
6+ * Copyright (c) 2020 ForgeRock . All rights reserved.
77 * This software may be modified and distributed under the terms
88 * of the MIT license. See the LICENSE file for details.
99 */
@@ -16,50 +16,49 @@ async function autoscript() {
1616 const delay = 0 ;
1717
1818 const url = new URL ( window . location . href ) ;
19- const preAuthenticated = url . searchParams . get ( 'preAuthenticated' ) || 'false' ;
2019 const code = url . searchParams . get ( 'code' ) || '' ;
21- const clientId = url . searchParams . get ( 'clientId' ) ;
22- const client_id = url . searchParams . get ( 'client_id' ) ;
23- const error = url . searchParams . get ( 'error_description' ) || false ;
24- const realmPath = url . searchParams . get ( 'realmPath' ) || 'root' ;
25- const scope = url . searchParams . get ( 'scope' ) || 'openid profile me.read' ;
20+ const error = url . searchParams . get ( 'error' ) || '' ;
2621 const state = url . searchParams . get ( 'state' ) || '' ;
27- const acr_values = url . searchParams . get ( 'acr' ) || 'SpecificTree' ;
2822 // in central login we use an auth query param for the return of our mock 401 request
2923 // this is to prevent the evaluation of the page before we have technically authenticated
3024 const auth = url . searchParams . get ( 'auth' ) || false ;
31- let wellknown =
32- url . searchParams . get ( 'wellknown' ) || 'http://localhost:9443/am/.well-known/oidc-configuration' ;
25+ const acr_values = url . searchParams . get ( 'acr' ) || 'SpecificTree' ;
3326
34- let tokenStore = url . searchParams . get ( 'tokenStore' ) || 'localStorage' ;
27+ let clientId = url . searchParams . get ( 'clientId' ) || 'CentralLoginOAuthClient' ;
28+ let realmPath = url . searchParams . get ( 'realmPath' ) || 'root' ;
29+ // The `revoke` scope is required for PingOne support
30+ let scope = url . searchParams . get ( 'scope' ) || 'openid profile me.read revoke' ;
31+ let wellKnownUrl =
32+ url . searchParams . get ( 'wellKnownUrl' ) ||
33+ 'http://localhost:9443/am/.well-known/oidc-configuration' ;
3534
36- // Support full redirects by setting storage, rather than rely purely on URL
37- if ( ! localStorage . getItem ( 'tokenStore' ) ) {
38- localStorage . setItem ( 'tokenStore' , tokenStore ) ;
35+ console . log ( 'Configure the SDK' ) ;
36+
37+ if ( wellKnownUrl ) {
38+ localStorage . setItem ( 'wellknown' , wellKnownUrl ) ;
39+ localStorage . setItem ( 'clientId' , clientId ) ;
40+ localStorage . setItem ( 'realmPath' , realmPath ) ;
41+ localStorage . setItem ( 'scope' , scope ) ;
3942 } else {
40- tokenStore = localStorage . getItem ( 'tokenStore' ) ;
43+ wellKnownUrl = localStorage . getItem ( 'wellknown' ) ;
44+ clientId = localStorage . getItem ( 'clientId' ) ;
45+ realmPath = localStorage . getItem ( 'realmPath' ) ;
46+ scope = localStorage . getItem ( 'scope' ) ;
4147 }
42-
43- console . log ( 'Configure the SDK' ) ;
44- forgerock . Config . setAsync ( {
45- clientId : clientId || client_id || 'CentralLoginOAuthClient' ,
48+ await forgerock . Config . setAsync ( {
49+ clientId,
4650 realmPath,
47- redirectUri : `${ url . origin } /src/${
48- preAuthenticated === 'false' ? 'authn-central-login' : '_callback'
49- } /`,
51+ redirectUri : `${ url . origin } /src/authn-central-login-wellknown/` ,
5052 scope,
5153 serverConfig : {
52- wellknown,
54+ wellknown : wellKnownUrl ,
5355 } ,
54- tokenStore,
5556 } ) ;
5657
57- if ( ! code && ! state ) {
58- try {
59- forgerock . SessionManager . logout ( ) ;
60- } catch ( err ) {
61- // Do nothing
62- }
58+ try {
59+ forgerock . SessionManager . logout ( ) ;
60+ } catch ( err ) {
61+ // Do nothing
6362 }
6463
6564 console . log ( 'Initiate first step with `undefined`' ) ;
@@ -68,29 +67,15 @@ async function autoscript() {
6867 setTimeout ( ( ) => {
6968 from ( [ 1 ] )
7069 . pipe (
71- map ( ( ) => {
72- if ( preAuthenticated === 'true' ) {
73- console . log ( 'Set mock cookie to represent existing session' ) ;
74- document . cookie = 'iPlanetDirectoryPro=abcd1234; domain=localhost; path=/' ;
75- if ( code && state ) {
76- window . sessionStorage . setItem (
77- `FR-SDK-authflow-${ clientId } ` ,
78- JSON . stringify ( { responseType : 'code' , state, verifier : '1234' } ) ,
79- ) ;
80- }
81- }
82- return ;
83- } ) ,
84- rxDelay ( delay ) ,
85- mergeMap ( ( step ) => {
70+ mergeMap ( ( ) => {
8671 let tokens ;
87- if ( error ) {
88- // Do nothing
72+ // detect when in iframe as to not call `/authorize` needlessly
73+ if ( window . self !== window . top ) {
8974 return ;
9075 } else if ( code && state ) {
9176 tokens = forgerock . TokenManager . getTokens ( {
9277 login : 'redirect' ,
93- query : { code, state, acr_values } ,
78+ query : { code, state } ,
9479 } ) ;
9580 } else {
9681 tokens = forgerock . TokenManager . getTokens ( {
@@ -113,6 +98,7 @@ async function autoscript() {
11398 console . log ( 'Remove cookie' ) ;
11499 document . cookie = '' ;
115100 console . log ( 'Initiate logout' ) ;
101+ // You have to allow specific origins to CORS for OAuth client
116102 return forgerock . FRUser . logout ( ) ;
117103 } ) ,
118104 )
@@ -130,12 +116,14 @@ async function autoscript() {
130116 }
131117 console . log ( `Error: ${ err . message } ` ) ;
132118 document . body . innerHTML = `<p class="Test_Complete">${ err . message } </p>` ;
133- localStorage . clear ( ) ;
134119 } ,
135120 complete : ( ) => {
136121 console . log ( 'Test script complete' ) ;
137122 document . body . innerHTML = `<p class="Test_Complete">Test script complete</p>` ;
138- localStorage . clear ( ) ;
123+ localStorage . removeItem ( 'wellknown' ) ;
124+ localStorage . removeItem ( 'clientId' ) ;
125+ localStorage . removeItem ( 'realmPath' ) ;
126+ localStorage . removeItem ( 'scope' ) ;
139127 } ,
140128 } ) ;
141129 } , 250 ) ;
0 commit comments