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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
import { assertEquals, assertRejects } from 'https://deno.land/std@0.198.0/assert/mod.ts';
import { returnsNext, stub } from 'https://deno.land/std@0.198.0/testing/mock.ts';
import { generateRegistrationOptions } from './generateRegistrationOptions.ts';
import { _generateChallengeInternals } from '../helpers/generateChallenge.ts';
import { isoBase64URL, isoUint8Array } from '../helpers/index.ts';
Deno.test('should generate credential request options suitable for sending via JSON', async () => {
const rpName = 'SimpleWebAuthn';
const rpID = 'not.real';
const challenge = 'totallyrandomvalue';
const userID = isoUint8Array.fromUTF8String('1234');
const userName = 'usernameHere';
const timeout = 1;
const attestationType = 'indirect';
const userDisplayName = 'userDisplayName';
const options = await generateRegistrationOptions({
rpName,
rpID,
challenge,
userID,
userName,
userDisplayName,
timeout,
attestationType,
});
assertEquals(
options,
{
// Challenge, base64url-encoded
challenge: 'dG90YWxseXJhbmRvbXZhbHVl',
rp: {
name: rpName,
id: rpID,
},
user: {
id: isoBase64URL.fromBuffer(userID),
name: userName,
displayName: userDisplayName,
},
pubKeyCredParams: [
{ alg: -8, type: 'public-key' },
{ alg: -7, type: 'public-key' },
{ alg: -257, type: 'public-key' },
],
timeout,
attestation: attestationType,
excludeCredentials: [],
authenticatorSelection: {
requireResidentKey: false,
residentKey: 'preferred',
userVerification: 'preferred',
},
extensions: {
credProps: true,
},
},
);
});
Deno.test('should map excluded credential IDs if specified', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
challenge: 'totallyrandomvalue',
userName: 'usernameHere',
excludeCredentials: [
{
id: 'someIDhere',
transports: ['usb', 'ble', 'nfc', 'internal'],
},
],
});
assertEquals(
options.excludeCredentials,
[
{
id: 'someIDhere',
type: 'public-key',
transports: ['usb', 'ble', 'nfc', 'internal'],
},
],
);
});
Deno.test('defaults to 60 seconds if no timeout is specified', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
challenge: 'totallyrandomvalue',
userName: 'usernameHere',
});
assertEquals(options.timeout, 60000);
});
Deno.test('defaults to none attestation if no attestation type is specified', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
challenge: 'totallyrandomvalue',
userName: 'usernameHere',
});
assertEquals(options.attestation, 'none');
});
Deno.test('defaults to empty string for displayName if no userDisplayName is specified', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
challenge: 'totallyrandomvalue',
userName: 'usernameHere',
});
assertEquals(options.user.displayName, '');
});
Deno.test('should set authenticatorSelection if specified', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
challenge: 'totallyrandomvalue',
userName: 'usernameHere',
authenticatorSelection: {
authenticatorAttachment: 'cross-platform',
requireResidentKey: false,
userVerification: 'preferred',
},
});
assertEquals(
options.authenticatorSelection,
{
authenticatorAttachment: 'cross-platform',
requireResidentKey: false,
userVerification: 'preferred',
},
);
});
Deno.test('should set extensions if specified', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
challenge: 'totallyrandomvalue',
userName: 'usernameHere',
extensions: { appid: 'simplewebauthn' },
});
assertEquals(options.extensions?.appid, 'simplewebauthn');
});
Deno.test('should include credProps if extensions are not provided', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
userName: 'usernameHere',
});
assertEquals(options.extensions?.credProps, true);
});
Deno.test('should include credProps if extensions are provided', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
userName: 'usernameHere',
extensions: { appid: 'simplewebauthn' },
});
assertEquals(options.extensions?.credProps, true);
});
Deno.test('should generate a challenge if one is not provided', async () => {
const mockGenerateChallenge = stub(
_generateChallengeInternals,
'stubThis',
returnsNext([
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
]),
);
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
});
// base64url-encoded 16-byte buffer from mocked `generateChallenge()`
assertEquals(options.challenge, 'AQIDBAUGBwgJCgsMDQ4PEA');
mockGenerateChallenge.restore();
});
Deno.test('should treat string challenges as UTF-8 strings', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
challenge: 'こんにちは',
});
assertEquals(
options.challenge,
'44GT44KT44Gr44Gh44Gv',
);
});
Deno.test('should use custom supported algorithm IDs as-is when provided', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
supportedAlgorithmIDs: [-7, -8, -65535],
});
assertEquals(
options.pubKeyCredParams,
[
{ alg: -7, type: 'public-key' },
{ alg: -8, type: 'public-key' },
{ alg: -65535, type: 'public-key' },
],
);
});
Deno.test('should require resident key if residentKey option is absent but requireResidentKey is set to true', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
authenticatorSelection: {
requireResidentKey: true,
},
});
assertEquals(options.authenticatorSelection?.requireResidentKey, true);
assertEquals(options.authenticatorSelection?.residentKey, 'required');
});
Deno.test('should discourage resident key if residentKey option is absent but requireResidentKey is set to false', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
authenticatorSelection: {
requireResidentKey: false,
},
});
assertEquals(options.authenticatorSelection?.requireResidentKey, false);
assertEquals(options.authenticatorSelection?.residentKey, undefined);
});
Deno.test('should prefer resident key if both residentKey and requireResidentKey options are absent', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
});
assertEquals(options.authenticatorSelection?.requireResidentKey, false);
assertEquals(options.authenticatorSelection?.residentKey, 'preferred');
});
Deno.test('should set requireResidentKey to true if residentKey if set to required', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
authenticatorSelection: {
residentKey: 'required',
},
});
assertEquals(options.authenticatorSelection?.requireResidentKey, true);
assertEquals(options.authenticatorSelection?.residentKey, 'required');
});
Deno.test('should set requireResidentKey to false if residentKey if set to preferred', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
authenticatorSelection: {
residentKey: 'preferred',
},
});
assertEquals(options.authenticatorSelection?.requireResidentKey, false);
assertEquals(options.authenticatorSelection?.residentKey, 'preferred');
});
Deno.test('should set requireResidentKey to false if residentKey if set to discouraged', async () => {
const options = await generateRegistrationOptions({
rpID: 'not.real',
rpName: 'SimpleWebAuthn',
userName: 'usernameHere',
authenticatorSelection: {
residentKey: 'discouraged',
},
});
assertEquals(options.authenticatorSelection?.requireResidentKey, false);
assertEquals(options.authenticatorSelection?.residentKey, 'discouraged');
});
Deno.test('should prefer Ed25519 in pubKeyCredParams', async () => {
const options = await generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
challenge: 'totallyrandomvalue',
userName: 'usernameHere',
});
assertEquals(options.pubKeyCredParams[0].alg, -8);
});
Deno.test('should raise if string is specified for userID', async () => {
await assertRejects(
() =>
generateRegistrationOptions({
rpName: 'SimpleWebAuthn',
rpID: 'not.real',
userName: 'usernameHere',
// @ts-ignore: Pretending a dev missed a refactor between v9 and v10
userID: 'customUserID',
}),
Error,
'String values for `userID` are no longer supported. See https://simplewebauthn.dev/docs/advanced/server/custom-user-ids',
);
});
|