/
index.ts
107 lines (94 loc) · 3.08 KB
/
index.ts
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
/*
* @forgerock/javascript-sdk
*
* index.ts
*
* Copyright (c) 2020 ForgeRock. All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
import type { CallbackType } from '../../auth/enums';
import type { Callback, NameValue } from '../../auth/interfaces';
/**
* Base class for authentication tree callback wrappers.
*/
class FRCallback {
/**
* @param payload The raw payload returned by OpenAM
*/
constructor(public payload: Callback) {}
/**
* Gets the name of this callback type.
*/
public getType(): CallbackType {
return this.payload.type;
}
/**
* Gets the value of the specified input element, or the first element if `selector` is not
* provided.
*
* @param selector The index position or name of the desired element
*/
public getInputValue(selector: number | string = 0): unknown {
return this.getArrayElement(this.payload.input, selector).value;
}
/**
* Sets the value of the specified input element, or the first element if `selector` is not
* provided.
*
* @param selector The index position or name of the desired element
*/
public setInputValue(value: unknown, selector: number | string | RegExp = 0): void {
this.getArrayElement(this.payload.input, selector).value = value;
}
/**
* Gets the value of the specified output element, or the first element if `selector`
* is not provided.
*
* @param selector The index position or name of the desired element
*/
public getOutputValue(selector: number | string = 0): unknown {
return this.getArrayElement(this.payload.output, selector).value;
}
/**
* Gets the value of the first output element with the specified name or the
* specified default value.
*
* @param name The name of the desired element
*/
public getOutputByName<T>(name: string, defaultValue: T): T {
const output = this.payload.output.find((x) => x.name === name);
return output ? (output.value as T) : defaultValue;
}
private getArrayElement(
array: NameValue[] | undefined,
selector: number | string | RegExp = 0,
): NameValue {
if (array === undefined) {
throw new Error(`No NameValue array was provided to search (selector ${selector})`);
}
if (typeof selector === 'number') {
if (selector < 0 || selector > array.length - 1) {
throw new Error(`Selector index ${selector} is out of range`);
}
return array[selector];
}
if (typeof selector === 'string') {
const input = array.find((x) => x.name === selector);
if (!input) {
throw new Error(`Missing callback input entry "${selector}"`);
}
return input;
}
// Duck typing for RegEx
if (typeof selector === 'object' && selector.test && Boolean(selector.exec)) {
const input = array.find((x) => selector.test(x.name));
if (!input) {
throw new Error(`Missing callback input entry "${selector}"`);
}
return input;
}
throw new Error('Invalid selector value type');
}
}
export default FRCallback;