-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathautoscript.ts
More file actions
157 lines (130 loc) · 5.41 KB
/
autoscript.ts
File metadata and controls
157 lines (130 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* @forgerock/javascript-sdk
*
* autoscript.ts
*
* Copyright (c) 2020 - 2025 Ping Identity Corporation. All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
// @ts-nocheck
import * as forgerock from '@forgerock/javascript-sdk';
import { delay as rxDelay, map, mergeMap } from 'rxjs/operators';
import { from } from 'rxjs';
function autoscript() {
const delay = 0;
const url = new URL(window.location.href);
const amUrl = url.searchParams.get('amUrl') || 'http://localhost:9443/am';
const realmPath = url.searchParams.get('realmPath') || 'root';
const tree = url.searchParams.get('tree') || 'Registration';
const un = url.searchParams.get('un') || 'f9022889-4452-48a0-aa94-182436645551';
const pw = url.searchParams.get('pw') || 'password';
const email = url.searchParams.get('email') || 'sally.tester@me.com';
console.log('Configure the SDK');
forgerock.Config.set({
realmPath,
tree,
serverConfig: {
baseUrl: amUrl,
},
});
try {
forgerock.SessionManager.logout();
} catch (err) {
// Do nothing
}
console.log('Initiate first step with `undefined`');
// Wrapping in setTimeout to give the test time to bind listener to console.log
setTimeout(function () {
from(forgerock.FRAuth.next())
.pipe(
rxDelay(delay),
mergeMap((step) => {
console.log('Handle ValidatedCreateUsernameCallback');
const unCb = step.getCallbackOfType('ValidatedCreateUsernameCallback');
console.log(`Prompt from UsernameCallback is ${unCb.getPrompt()}`);
unCb.setName(un);
console.log('Handle ValidatedCreatePasswordCallback');
const pwCb = step.getCallbackOfType('ValidatedCreatePasswordCallback');
console.log(`Prompt from PasswordCallback is ${pwCb.getPrompt()}`);
pwCb.setPassword(pw);
const [saCb1, saCb2, saCb3] = step.getCallbacksOfType('StringAttributeInputCallback');
console.log(`Prompt 1: ${saCb1.getPrompt()}`);
console.log(`Prompt 2: ${saCb2.getPrompt()}`);
console.log(`Prompt 3: ${saCb3.getPrompt()}`);
saCb1.setInputValue('Sally');
saCb2.setInputValue('Tester');
saCb3.setInputValue(email);
const [baCb1, baCb2] = step.getCallbacksOfType('BooleanAttributeInputCallback');
console.log(`Prompt 4: ${baCb1.getPrompt()}`);
console.log(`Prompt 5: ${baCb2.getPrompt()}`);
baCb1.setInputValue(false);
baCb2.setInputValue(false);
// const naCb = step.getCallbackOfType('NumberAttributeInputCallback');
// console.log(`Prompt 6: ${naCb.getPrompt()}`);
// naCb.setInputValue(40);
console.log('Handle KbaCreateCallback');
const [kbCb1, kbCb2] = step.getCallbacksOfType('KbaCreateCallback');
console.log(`Prompt 7: ${kbCb1.getPrompt()}`);
console.log(`Prompt 8: ${kbCb2.getPrompt()}`);
const [pdq1, pdq2] = kbCb1.getPredefinedQuestions();
console.log(`Predefined Question1: ${pdq1}`);
console.log(`Predefined Question 2: ${pdq2}`);
const isAllowedUserDefinedQuestions1 = kbCb1.isAllowedUserDefinedQuestions();
const isAllowedUserDefinedQuestions2 = kbCb2.isAllowedUserDefinedQuestions();
console.log(`kbCb1 is allowed user defined questions: ${isAllowedUserDefinedQuestions1}`);
console.log(`kbCb2 is allowed user defined questions: ${isAllowedUserDefinedQuestions2}`);
kbCb1.setQuestion(pdq1);
kbCb1.setAnswer('Red');
kbCb2.setQuestion('Who was your first pet?');
kbCb2.setAnswer('Fluffy');
const customQuestion = kbCb2.getInputValue();
console.log(`Custom Question from kbCb2: ${customQuestion}`);
console.log('Handle TermsAndConditionsCallback');
const tcCb = step.getCallbackOfType('TermsAndConditionsCallback');
console.log(`Terms version: ${tcCb.getVersion()}`);
console.log(`Terms text: ${tcCb.getTerms()}`);
tcCb.setAccepted();
return forgerock.FRAuth.next(step);
}),
rxDelay(delay),
map(
(step) => {
if (step.payload.status === 401) {
throw new Error('Auth_Error');
} else if (step.payload.tokenId) {
console.log('Basic login successful');
document.body.innerHTML = '<p class="Logged_In">Login successful</p>';
} else {
throw new Error('Something went wrong.');
}
},
(step) => step,
),
rxDelay(delay),
mergeMap((step) => {
return forgerock.SessionManager.logout();
}),
map((response) => {
if (response.ok) {
console.log('Logout successful');
document.body.innerHTML = '<p class="Logged_Out">Logout successful</p>';
} else {
throw new Error('Logout_Error');
}
}),
)
.subscribe({
error: (err) => {
console.log(`Error: ${err.message}`);
document.body.innerHTML = `<p class='Test_Complete'>${err.message}</p>`;
},
complete: () => {
console.log('Test script complete');
document.body.innerHTML = `<p class='Test_Complete'>Test script complete</p>`;
},
});
}, 250);
}
autoscript();
export default autoscript;