33 *
44 * autoscript.ts
55 *
6- * Copyright (c) 2020 ForgeRock . All rights reserved.
6+ * Copyright (c) 2020 - 2025 Ping Identity Corporation . 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,49 +16,50 @@ async function autoscript() {
1616 const delay = 0 ;
1717
1818 const url = new URL ( window . location . href ) ;
19+ const preAuthenticated = url . searchParams . get ( 'preAuthenticated' ) || 'false' ;
1920 const code = url . searchParams . get ( 'code' ) || '' ;
20- const error = url . searchParams . get ( 'error' ) || '' ;
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' ;
2126 const state = url . searchParams . get ( 'state' ) || '' ;
27+ const acr_values = url . searchParams . get ( 'acr' ) || 'SpecificTree' ;
2228 // in central login we use an auth query param for the return of our mock 401 request
2329 // this is to prevent the evaluation of the page before we have technically authenticated
2430 const auth = url . searchParams . get ( 'auth' ) || false ;
25- const acr_values = url . searchParams . get ( 'acr' ) || 'SpecificTree' ;
31+ let wellknown =
32+ url . searchParams . get ( 'wellknown' ) || 'http://localhost:9443/am/.well-known/oidc-configuration' ;
2633
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' ;
34+ let tokenStore = url . searchParams . get ( 'tokenStore' ) || 'localStorage' ;
3435
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 ) ;
36+ // Support full redirects by setting storage, rather than rely purely on URL
37+ if ( ! localStorage . getItem ( 'tokenStore' ) ) {
38+ localStorage . setItem ( 'tokenStore' , tokenStore ) ;
4239 } else {
43- wellKnownUrl = localStorage . getItem ( 'wellknown' ) ;
44- clientId = localStorage . getItem ( 'clientId' ) ;
45- realmPath = localStorage . getItem ( 'realmPath' ) ;
46- scope = localStorage . getItem ( 'scope' ) ;
40+ tokenStore = localStorage . getItem ( 'tokenStore' ) ;
4741 }
48- await forgerock . Config . setAsync ( {
49- clientId,
42+
43+ console . log ( 'Configure the SDK' ) ;
44+ forgerock . Config . setAsync ( {
45+ clientId : clientId || client_id || 'CentralLoginOAuthClient' ,
5046 realmPath,
51- redirectUri : `${ url . origin } /src/authn-central-login-wellknown/` ,
47+ redirectUri : `${ url . origin } /src/${
48+ preAuthenticated === 'false' ? 'authn-central-login' : '_callback'
49+ } /`,
5250 scope,
5351 serverConfig : {
54- wellknown : wellKnownUrl ,
52+ wellknown,
5553 } ,
54+ tokenStore,
5655 } ) ;
5756
58- try {
59- forgerock . SessionManager . logout ( ) ;
60- } catch ( err ) {
61- // Do nothing
57+ if ( ! code && ! state ) {
58+ try {
59+ forgerock . SessionManager . logout ( ) ;
60+ } catch ( err ) {
61+ // Do nothing
62+ }
6263 }
6364
6465 console . log ( 'Initiate first step with `undefined`' ) ;
@@ -67,15 +68,29 @@ async function autoscript() {
6768 setTimeout ( ( ) => {
6869 from ( [ 1 ] )
6970 . pipe (
70- mergeMap ( ( ) => {
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 ) => {
7186 let tokens ;
72- // detect when in iframe as to not call `/authorize` needlessly
73- if ( window . self !== window . top ) {
87+ if ( error ) {
88+ // Do nothing
7489 return ;
7590 } else if ( code && state ) {
7691 tokens = forgerock . TokenManager . getTokens ( {
7792 login : 'redirect' ,
78- query : { code, state } ,
93+ query : { code, state, acr_values } ,
7994 } ) ;
8095 } else {
8196 tokens = forgerock . TokenManager . getTokens ( {
@@ -98,7 +113,6 @@ async function autoscript() {
98113 console . log ( 'Remove cookie' ) ;
99114 document . cookie = '' ;
100115 console . log ( 'Initiate logout' ) ;
101- // You have to allow specific origins to CORS for OAuth client
102116 return forgerock . FRUser . logout ( ) ;
103117 } ) ,
104118 )
@@ -116,14 +130,12 @@ async function autoscript() {
116130 }
117131 console . log ( `Error: ${ err . message } ` ) ;
118132 document . body . innerHTML = `<p class="Test_Complete">${ err . message } </p>` ;
133+ localStorage . clear ( ) ;
119134 } ,
120135 complete : ( ) => {
121136 console . log ( 'Test script complete' ) ;
122137 document . body . innerHTML = `<p class="Test_Complete">Test script complete</p>` ;
123- localStorage . removeItem ( 'wellknown' ) ;
124- localStorage . removeItem ( 'clientId' ) ;
125- localStorage . removeItem ( 'realmPath' ) ;
126- localStorage . removeItem ( 'scope' ) ;
138+ localStorage . clear ( ) ;
127139 } ,
128140 } ) ;
129141 } , 250 ) ;
0 commit comments