forked from ForgeRock/forgerock-javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfr-webauthn.test.ts
More file actions
307 lines (273 loc) · 11.5 KB
/
fr-webauthn.test.ts
File metadata and controls
307 lines (273 loc) · 11.5 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* @forgerock/javascript-sdk
*
* fr-webauthn.test.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.
*/
import { WebAuthnOutcome, WebAuthnStepType } from './enums';
import FRWebAuthn from './index';
import {
webAuthnRegJSCallback653,
webAuthnAuthJSCallback653,
webAuthnRegJSCallback70,
webAuthnAuthJSCallback70,
webAuthnRegMetaCallback70,
webAuthnAuthMetaCallback70,
webAuthnRegJSCallback70StoredUsername,
webAuthnAuthJSCallback70StoredUsername,
webAuthnRegMetaCallback70StoredUsername,
webAuthnAuthMetaCallback70StoredUsername,
webAuthnAuthConditionalMetaCallback,
} from './fr-webauthn.mock.data';
import { CallbackType } from '../auth/enums';
import FRStep from '../fr-auth/fr-step';
import Config from '../config';
describe('Test FRWebAuthn class with 6.5.3 "Passwordless"', () => {
it('should return Registration type with register text-output callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnRegJSCallback653 as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Registration);
});
it('should return Authentication type with authenticate text-output callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnAuthJSCallback653 as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Authentication);
});
// it('should return Registration type with register metadata callbacks', () => {
// // eslint-disable-next-line
// const step = new FRStep(webAuthnRegMetaCallback653 as any);
// const stepType = FRWebAuthn.getWebAuthnStepType(step);
// expect(stepType).toBe(WebAuthnStepType.Registration);
// });
// it('should return Authentication type with authenticate metadata callbacks', () => {
// // eslint-disable-next-line
// const step = new FRStep(webAuthnAuthMetaCallback653 as any);
// const stepType = FRWebAuthn.getWebAuthnStepType(step);
// expect(stepType).toBe(WebAuthnStepType.Authentication);
// });
});
describe('Test FRWebAuthn class with 7.0 "Passwordless"', () => {
it('should return Registration type with register text-output callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnRegJSCallback70 as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Registration);
});
it('should return Authentication type with authenticate text-output callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnAuthJSCallback70 as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Authentication);
});
it('should return Registration type with register metadata callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnRegMetaCallback70 as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Registration);
});
it('should return Authentication type with authenticate metadata callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnAuthMetaCallback70 as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Authentication);
});
});
describe('Test FRWebAuthn class with 7.0 "Usernameless"', () => {
it('should return Registration type with register text-output callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnRegJSCallback70StoredUsername as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Registration);
});
it('should return Authentication type with authenticate text-output callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnAuthJSCallback70StoredUsername as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Authentication);
});
it('should return Registration type with register metadata callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnRegMetaCallback70StoredUsername as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Registration);
});
it('should return Authentication type with authenticate metadata callbacks', () => {
// eslint-disable-next-line
const step = new FRStep(webAuthnAuthMetaCallback70StoredUsername as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Authentication);
});
});
describe('Test FRWebAuthn class with Conditional UI', () => {
beforeEach(() => {
// Mock navigator.credentials and window.PublicKeyCredential
Object.defineProperty(global.navigator, 'credentials', {
value: {
get: vi.fn().mockResolvedValue(null),
create: vi.fn(),
},
writable: true,
});
Object.defineProperty(window, 'PublicKeyCredential', {
value: {
isConditionalMediationAvailable: vi.fn(),
},
writable: true,
});
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should detect if conditional UI is supported', async () => {
vi.spyOn(window.PublicKeyCredential, 'isConditionalMediationAvailable').mockResolvedValue(true);
const isSupported = await FRWebAuthn.isConditionalMediationSupported();
expect(isSupported).toBe(true);
});
it('should return Authentication type with conditional UI metadata callback', () => {
const step = new FRStep(webAuthnAuthConditionalMetaCallback as any);
const stepType = FRWebAuthn.getWebAuthnStepType(step);
expect(stepType).toBe(WebAuthnStepType.Authentication);
});
it('should create authentication public key with empty allowCredentials for conditional UI', () => {
const metadata: any = {
_action: 'webauthn_authentication',
challenge: 'JEisuqkVMhI490jM0/iEgrRz+j94OoGc7gdY4gYicSk=',
allowCredentials: '',
_allowCredentials: [],
timeout: 60000,
userVerification: 'preferred',
mediation: 'conditional',
relyingPartyId: '',
_relyingPartyId: 'example.com',
extensions: {},
supportsJsonResponse: true,
};
const publicKey = FRWebAuthn.createAuthenticationPublicKey(metadata);
expect(publicKey.challenge).toBeDefined();
expect(publicKey.timeout).toBe(60000);
expect(publicKey.userVerification).toBe('preferred');
expect(publicKey.rpId).toBe('example.com');
// allowCredentials should not be present for conditional UI with empty credentials
expect(publicKey.allowCredentials).toBeUndefined();
});
it('should warn and return false if conditional UI is requested but not supported', async () => {
Config.set({
serverConfig: {
baseUrl: 'http://localhost:8080',
},
clientId: 'test',
realmPath: 'alpha',
logLevel: 'warn',
});
// Mock browser support for conditional UI to be false
vi.spyOn(window.PublicKeyCredential, 'isConditionalMediationAvailable').mockResolvedValue(
false,
);
const getSpy = vi.spyOn(navigator.credentials, 'get');
// Attempt to authenticate with conditional UI requested
await FRWebAuthn.getAuthenticationCredential({});
// Expect the call to navigator.credentials.get to NOT have the mediation property
expect(getSpy).toHaveBeenCalledWith(
expect.not.objectContaining({
mediation: 'conditional',
}),
);
});
it('should set mediation to conditional if supported', async () => {
// Mock browser support for conditional UI to be true
vi.spyOn(window.PublicKeyCredential, 'isConditionalMediationAvailable').mockResolvedValue(true);
const getSpy = vi.spyOn(navigator.credentials, 'get');
// Attempt to authenticate with conditional UI requested
await FRWebAuthn.getAuthenticationCredential({
mediation: 'conditional',
});
// Expect the call to navigator.credentials.get to have the mediation property
expect(getSpy).toHaveBeenCalledWith(
expect.objectContaining({
mediation: 'conditional',
}),
);
});
it('should throw NotSupportedError if WebAuthn is not supported', async () => {
// Mock WebAuthn not supported
const spy = vi.spyOn(FRWebAuthn, 'isWebAuthnSupported').mockReturnValue(false);
await expect(FRWebAuthn.getRegistrationCredential({} as any)).rejects.toThrow(
'PublicKeyCredential not supported by this browser',
);
spy.mockRestore();
});
it('should correctly convert _allowCredentials id to Int8Array', () => {
const metadata: any = {
_action: 'webauthn_authentication',
challenge: 'JEisuqkVMhI490jM0/iEgrRz+j94OoGc7gdY4gYicSk=',
relyingPartyId: '',
_allowCredentials: [
{
type: 'public-key',
id: [1, 2, 3, 4],
transports: ['usb'],
},
],
timeout: 60000,
};
const publicKey = FRWebAuthn.createAuthenticationPublicKey(metadata);
expect(publicKey.allowCredentials).toBeDefined();
expect(publicKey.allowCredentials![0].id).toBeInstanceOf(Int8Array);
const idArray = publicKey.allowCredentials![0].id as Int8Array;
expect(Array.from(idArray)).toEqual([1, 2, 3, 4]);
});
});
describe('Test FRWebAuthn class with cancellation error handling', () => {
beforeEach(() => {
Object.defineProperty(global.navigator, 'credentials', {
value: {
get: vi.fn(),
create: vi.fn(),
},
writable: true,
});
Object.defineProperty(window, 'PublicKeyCredential', {
value: {
// Mocked as supported so conditional mediation checks pass through to the credential call
isConditionalMediationAvailable: vi.fn().mockResolvedValue(true),
},
writable: true,
});
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should write NotAllowedError to HiddenValueCallback when user cancels conditional authentication', async () => {
const cancelError = new Error('The operation either timed out or was not allowed.');
cancelError.name = 'NotAllowedError';
vi.spyOn(navigator.credentials, 'get').mockRejectedValue(cancelError);
const step = new FRStep(webAuthnAuthConditionalMetaCallback as any);
await expect(FRWebAuthn.authenticate(step)).rejects.toMatchObject({
name: 'NotAllowedError',
});
const hiddenCallback = step.getCallbacksOfType(CallbackType.HiddenValueCallback)[0];
expect(hiddenCallback).toBeDefined();
expect(hiddenCallback.getInputValue()).toBe(
`${WebAuthnOutcome.Error}::NotAllowedError:The operation either timed out or was not allowed.`,
);
});
it('should write NotAllowedError to HiddenValueCallback when user cancels standard authentication', async () => {
const cancelError = new Error('The operation either timed out or was not allowed.');
cancelError.name = 'NotAllowedError';
vi.spyOn(navigator.credentials, 'get').mockRejectedValue(cancelError);
const step = new FRStep(webAuthnAuthMetaCallback70 as any);
await expect(FRWebAuthn.authenticate(step)).rejects.toMatchObject({
name: 'NotAllowedError',
});
const hiddenCallback = step.getCallbacksOfType(CallbackType.HiddenValueCallback)[0];
expect(hiddenCallback).toBeDefined();
expect(hiddenCallback.getInputValue()).toBe(
`${WebAuthnOutcome.Error}::NotAllowedError:The operation either timed out or was not allowed.`,
);
});
});