-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
138 lines (122 loc) · 4.16 KB
/
index.ts
File metadata and controls
138 lines (122 loc) · 4.16 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
import { deviceClient } from '@forgerock/device-client';
import type { ConfigOptions, DeviceClient } from '@forgerock/device-client/types';
import {
CallbackType,
Config,
FRAuth,
FRLoginFailure,
FRLoginSuccess,
FRStep,
NameCallback,
PasswordCallback,
SessionManager,
TokenManager,
UserManager,
} from '@forgerock/javascript-sdk';
import { Console, Effect } from 'effect';
const logout = Effect.ignore(
Effect.tryPromise({
try: () => SessionManager.logout(),
catch: (err) => new Error(`Logout failed: ${err}`),
}),
);
const start = Effect.tryPromise({
try: () => FRAuth.start(),
catch: (err) => new Error(`Authentication start failed: ${err}`),
}).pipe(Effect.tap((step) => Console.log('Called start', step)));
const checkFRStep = (step: FRStep | FRLoginFailure | FRLoginSuccess) =>
Effect.try({
try: () => {
if (step.type == 'LoginSuccess' || step.type == 'LoginFailure') {
throw new Error(`Unexpected step type: ${step.type}`);
} else {
return step;
}
},
catch: (err) => new Error(`Failed to start authentication: ${err}`),
});
const callNext = (step: FRStep) =>
Effect.tryPromise({
try: () => FRAuth.next(step),
catch: (err) => new Error(`Failed to proceed to next step: ${err}`),
}).pipe(Effect.tap((step) => Console.log('Got next step', step)));
const getTokens = Effect.tryPromise({
try: () => TokenManager.getTokens(),
catch: (err) => new Error(`Failed to get tokens: ${err}`),
}).pipe(Effect.tap((tokens) => Console.log('Got Tokens', tokens)));
const checkForLoginSuccess = (step: FRStep | FRLoginSuccess | FRLoginFailure) => {
if (step.type === 'LoginSuccess') {
return Effect.succeed(step);
} else if (step.type === 'LoginFailure') {
return Effect.fail(new Error(`Login failed`));
} else {
return Effect.fail(
new Error(`Unexpected step, expected to be in a LoginSuccess but got ${step.type}`),
);
}
};
export const LoginAndGetClient = Effect.gen(function* () {
const url = new URL(window.location.href);
const amUrl = url.searchParams.get('amUrl') || 'https://openam-sdks.forgeblocks.com/am';
const realmPath = url.searchParams.get('realmPath') || 'alpha';
const platformHeader = url.searchParams.get('platformHeader') === 'true' ? true : false;
const tree = url.searchParams.get('tree') || 'selfservice';
/**
* Make sure this `un` is a real user
* this is a manual test and requires a real tenant and a real user
* that has devices.
*/
const un = url.searchParams.get('un') || 'devicetestuser';
const pw = url.searchParams.get('pw') || 'password';
const config: ConfigOptions = {
realmPath,
serverConfig: {
baseUrl: amUrl,
timeout: 3000,
},
};
yield* Effect.try(() =>
Config.set({
platformHeader,
realmPath,
tree,
clientId: 'WebOAuthClient',
scope: 'profile email me.read openid',
redirectUri: `${window.location.origin}/src/_callback/index.html`,
serverConfig: {
baseUrl: amUrl,
timeout: 3000,
},
}),
);
yield* logout;
yield* start.pipe(
Effect.flatMap((step) => checkFRStep(step)),
Effect.map((step) => {
step.getCallbackOfType<NameCallback>(CallbackType.NameCallback).setName(un);
step.getCallbackOfType<PasswordCallback>(CallbackType.PasswordCallback).setPassword(pw);
return step;
}),
Effect.flatMap((step) => callNext(step)),
/**
* Don't explicitly need this but if the journey changes
* maybe we dont get a LoginSuccess
*/
Effect.flatMap((step) => checkForLoginSuccess(step)),
Effect.flatMap(() => getTokens),
);
const client: DeviceClient = deviceClient(config);
return client;
});
export const getUser = Effect.tryPromise({
try: () => UserManager.getCurrentUser() as Promise<Record<string, string>>,
catch: (err) => new Error(`Failed to get current user: ${err}`),
});
export const handleError = (err: unknown) => {
console.error(err);
document.body.innerHTML = `<p class="Test_Failed">Test script failed: ${err}</p>`;
};
export const handleSuccess = () => {
console.log('Test script complete');
document.body.innerHTML = `<p class="Test_Complete">Test script complete</p>`;
};