-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathping-protect.ts
More file actions
268 lines (221 loc) · 7.78 KB
/
ping-protect.ts
File metadata and controls
268 lines (221 loc) · 7.78 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
*
* Copyright (c) 2024 - 2026 Ping Identity Corporation. All right reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
**/
import {
CallbackType,
FRStep,
HiddenValueCallback,
MetadataCallback,
PingOneProtectEvaluationCallback,
PingOneProtectInitializeCallback,
} from '@forgerock/javascript-sdk';
import {
ProtectEvaluationConfig,
ProtectInitializeConfig,
ProtectNodeInitializeConfig,
SignalsInitializationOptions,
} from './ping-protect.types';
export interface Identifiers {
[key: string]: string;
}
export type InitParams =
| Omit<ProtectInitializeConfig, '_type' | '_action'>
| SignalsInitializationOptions;
// Add Signals SDK namespace to the window object
declare global {
interface Window {
_pingOneSignals: {
init: (initParams?: InitParams) => Promise<void>;
getData: () => Promise<string>;
pauseBehavioralData: () => void;
resumeBehavioralData: () => void;
};
}
}
/**
* @class PIProtect - Class to interact with the underlying PingOne Signals SDK
*/
export abstract class PIProtect {
/** ***********************************************************************************************
* The following methods are methods for the interacting with PingOne Signals SDK
*/
/**
* @method getData - Method to get the device data
* @returns {Promise<string>} - Returns the device data
*/
public static async getData(): Promise<string> {
return await window._pingOneSignals.getData();
}
/**
* @method start - Method to initialize and start the PingOne Signals SDK
* @param {InitParams} options - The init parameters
* @returns {Promise<void>} - Returns a promise
*/
public static async start(options: InitParams): Promise<void> {
try {
/*
* Load the Ping Signals SDK
* this automatically pollutes the window
* there are no exports of this module
*/
await import('./ping-signals-sdk.js' as string);
} catch (err) {
console.error('error loading ping signals', err);
}
await window._pingOneSignals.init(options);
if (options.behavioralDataCollection === true || options.behavioralDataCollection === 'true') {
PIProtect.resumeBehavioralData();
}
}
/**
* @method pauseBehavioralData - Method to pause the behavioral data collection
* @returns {void}
* @description Pause the behavioral data collection only; device profile data will still be collected
*/
public static pauseBehavioralData(): void {
window._pingOneSignals.pauseBehavioralData();
}
/**
* @method resumeBehavioralData - Method to resume the behavioral data collection
* @returns {void}
* @description Resume the behavioral data collection
*/
public static resumeBehavioralData(): void {
window._pingOneSignals.resumeBehavioralData();
}
/** ***********************************************************************************************
* Required when using the Ping Protect Marketplace nodes, which has generic callbacks
* But, can be used for native nodes and/or either callback type
*/
public static getPauseBehavioralData(step: FRStep): boolean {
// Check for native callback first
try {
const nativeCallback = step.getCallbackOfType<PingOneProtectEvaluationCallback>(
CallbackType.PingOneProtectEvaluationCallback,
);
const shouldPause = nativeCallback?.getPauseBehavioralData();
return shouldPause || false;
} catch (err) {
// Do nothing
}
// If we are here, we are dealing with Marketplace nodes
const cbs = step.getCallbacksOfType<MetadataCallback>(CallbackType.MetadataCallback);
if (!cbs.length) {
return false;
}
const protectMetadataCb = cbs.find((metadataCallback) => {
const data = metadataCallback.getData() as { _type: string; _action: string };
return data._type === 'PingOneProtect';
});
if (!protectMetadataCb) {
return false;
}
const data: ProtectInitializeConfig | ProtectEvaluationConfig = (
protectMetadataCb as MetadataCallback
).getData();
if (data._action === 'protect_risk_evaluation') {
return false;
} else {
return !!(data as ProtectInitializeConfig).behavioralDataCollection;
}
}
public static getNodeConfig(step: FRStep): ProtectNodeInitializeConfig | undefined {
// Check for native callback first
try {
const nativeCallback = step.getCallbackOfType<PingOneProtectInitializeCallback>(
CallbackType.PingOneProtectInitializeCallback,
);
const config = nativeCallback?.getConfig() as ProtectNodeInitializeConfig;
return config;
} catch (err) {
// Do nothing
}
const cbs = step.getCallbacksOfType<MetadataCallback>(CallbackType.MetadataCallback);
if (!cbs.length) {
return undefined;
}
const protectMetadataCb = cbs.find((metadataCallback) => {
const data = metadataCallback.getData() as { _type: string; _action: string };
return data._action === 'protect_initialize';
});
if (!protectMetadataCb) {
return undefined;
}
const data = (protectMetadataCb as MetadataCallback).getData() as ProtectInitializeConfig;
return data;
}
public static getPingProtectType(step: FRStep): 'initialize' | 'evaluate' | 'none' {
const cbs = step.getCallbacksOfType(CallbackType.MetadataCallback);
if (!cbs.length) {
return 'none';
}
const protectMetadataCb = cbs.find((cb) => {
const metadataCallback = cb as MetadataCallback;
const data = metadataCallback.getData() as { _type: string; _action: string };
return data._type === 'PingOneProtect';
});
if (!protectMetadataCb) {
return 'none';
}
const data = (protectMetadataCb as MetadataCallback).getData() as ProtectInitializeConfig;
return data._action === 'protect_initialize' ? 'initialize' : 'evaluate';
}
public static setNodeClientError(step: FRStep, value: string): void {
// Check for native callback first
const nativeEvaluationCallback = step.getCallbacksOfType<PingOneProtectEvaluationCallback>(
CallbackType.PingOneProtectEvaluationCallback,
);
const nativeInitializeCallback = step.getCallbacksOfType<PingOneProtectInitializeCallback>(
CallbackType.PingOneProtectInitializeCallback,
);
const arr = [...nativeEvaluationCallback, ...nativeInitializeCallback];
if (arr.length) {
const cb = arr[0];
cb.setClientError(value);
return;
}
// If we are here, we are dealing with Marketplace nodes
const cbs = step.getCallbacksOfType<HiddenValueCallback>(CallbackType.HiddenValueCallback);
if (!cbs.length) {
return;
}
const clientErrorCb = cbs.find((hiddenValueCallback) => {
const output = hiddenValueCallback.getOutputByName<string>('id', '');
return output === 'clientError';
});
if (!clientErrorCb) {
return;
}
clientErrorCb.setInputValue(value);
}
public static setNodeInputValue(step: FRStep, value: string): void {
// Check for native callback first
try {
const nativeCallback = step.getCallbackOfType<PingOneProtectEvaluationCallback>(
CallbackType.PingOneProtectEvaluationCallback,
);
nativeCallback?.setData(value);
return;
} catch (err) {
// Do nothing
}
// If we are here, we are dealing with Marketplace nodes
const cbs = step.getCallbacksOfType<HiddenValueCallback>(CallbackType.HiddenValueCallback);
if (!cbs.length) {
return;
}
const inputCb = cbs.find((hiddenValueCallback) => {
const output = hiddenValueCallback.getOutputByName<string>('id', '');
return output === 'pingone_risk_evaluation_signals';
});
if (!inputCb) {
return;
}
inputCb.setInputValue(value);
}
}