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
|
import { assert, assertEquals, assertExists } from 'https://deno.land/std@0.198.0/assert/mod.ts';
import { isoBase64URL, isoUint8Array } from '../helpers/iso/index.ts';
import { generateAuthenticationOptions } from './generateAuthenticationOptions.ts';
const challengeString = 'dG90YWxseXJhbmRvbXZhbHVl';
const challengeBuffer = isoBase64URL.toBuffer(challengeString);
const rpID = 'simplewebauthn.dev';
Deno.test('should generate credential request options suitable for sending via JSON', async () => {
const options = await generateAuthenticationOptions({
rpID,
allowCredentials: [
{
id: '1234',
transports: ['usb', 'nfc'],
},
{
id: '5678',
transports: ['internal'],
},
],
timeout: 1,
challenge: challengeBuffer,
});
assertEquals(options, {
rpId: 'simplewebauthn.dev',
// base64url-encoded
challenge: challengeString,
allowCredentials: [
{
id: '1234',
type: 'public-key',
transports: ['usb', 'nfc'],
},
{
id: '5678',
type: 'public-key',
transports: ['internal'],
},
],
timeout: 1,
userVerification: 'preferred',
extensions: undefined,
});
});
Deno.test('defaults to 60 seconds if no timeout is specified', async () => {
const options = await generateAuthenticationOptions({
rpID,
challenge: challengeBuffer,
allowCredentials: [
{ id: '1234' },
{ id: '5678' },
],
});
assertEquals(options.timeout, 60000);
});
Deno.test('should set userVerification to "preferred" if not specified', async () => {
const options = await generateAuthenticationOptions({
rpID,
challenge: challengeBuffer,
allowCredentials: [
{ id: '1234' },
{ id: '5678' },
],
});
assertEquals(options.userVerification, 'preferred');
});
Deno.test('should not set allowCredentials if not specified', async () => {
const options = await generateAuthenticationOptions({ rpID });
assertEquals(options.allowCredentials, undefined);
});
Deno.test('should generate without params', async () => {
const options = await generateAuthenticationOptions({ rpID });
const { challenge, ...otherFields } = options;
assertEquals(otherFields, {
allowCredentials: undefined,
extensions: undefined,
rpId: rpID,
timeout: 60000,
userVerification: 'preferred',
});
assertEquals(typeof challenge, 'string');
});
Deno.test('should set userVerification if specified', async () => {
const options = await generateAuthenticationOptions({
rpID,
challenge: challengeBuffer,
allowCredentials: [
{ id: '1234' },
{ id: '5678' },
],
userVerification: 'required',
});
assertEquals(options.userVerification, 'required');
});
Deno.test('should set extensions if specified', async () => {
const options = await generateAuthenticationOptions({
rpID,
challenge: challengeBuffer,
allowCredentials: [
{ id: '1234' },
{ id: '5678' },
],
extensions: { appid: 'simplewebauthn' },
});
assertEquals(options.extensions, { appid: 'simplewebauthn' });
});
Deno.test('should generate a challenge if one is not provided', async () => {
// @ts-ignore 2345
const options = await generateAuthenticationOptions({
rpID,
allowCredentials: [
{ id: '1234' },
{ id: '5678' },
],
});
// Assert basic properties of the challenge
assert(options.challenge.length >= 16);
assert(isoBase64URL.isBase64URL(options.challenge));
});
Deno.test('should treat string challenges as UTF-8 strings', async () => {
const options = await generateAuthenticationOptions({
rpID,
challenge: 'こんにちは',
});
assertEquals(
options.challenge,
'44GT44KT44Gr44Gh44Gv',
);
});
|