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
|
import {
AssertionCredential,
PublicKeyCredentialRequestOptionsJSON,
} from '@simplewebauthn/typescript-types';
import supportsWebauthn from '../helpers/supportsWebauthn';
import toUint8Array from '../helpers/toUint8Array';
import bufferToBase64URLString from '../helpers/bufferToBase64URLString';
import startAssertion from './startAssertion';
jest.mock('../helpers/supportsWebauthn');
const mockNavigatorGet = window.navigator.credentials.get as jest.Mock;
const mockSupportsWebauthn = supportsWebauthn as jest.Mock;
const mockAuthenticatorData = 'mockAuthenticatorData';
const mockClientDataJSON = 'mockClientDataJSON';
const mockSignature = 'mockSignature';
const mockUserHandle = 'mockUserHandle';
// With ASCII challenge
const goodOpts1: PublicKeyCredentialRequestOptionsJSON = {
challenge: bufferToBase64URLString(toUint8Array('fizz')),
allowCredentials: [
{
id: 'C0VGlvYFratUdAV1iCw-ULpUW8E-exHPXQChBfyVeJZCMfjMFcwDmOFgoMUz39LoMtCJUBW8WPlLkGT6q8qTCg',
type: 'public-key',
transports: ['nfc'],
},
],
timeout: 1,
};
// With UTF-8 challenge
const goodOpts2UTF8: PublicKeyCredentialRequestOptionsJSON = {
challenge: bufferToBase64URLString(toUint8Array('やれやれだぜ')),
allowCredentials: [],
timeout: 1,
};
// Without allow credentials
const goodOpts3: PublicKeyCredentialRequestOptionsJSON = {
challenge: bufferToBase64URLString(toUint8Array('fizz')),
timeout: 1,
};
beforeEach(() => {
mockNavigatorGet.mockReset();
mockSupportsWebauthn.mockReset();
});
test('should convert options before passing to navigator.credentials.get(...)', async done => {
mockSupportsWebauthn.mockReturnValue(true);
// Stub out a response so the method won't throw
mockNavigatorGet.mockImplementation(
(): Promise<any> => {
return new Promise(resolve => {
resolve({
response: {},
getClientExtensionResults: () => ({}),
});
});
},
);
const checkWithOpts = async (opts: PublicKeyCredentialRequestOptionsJSON) => {
await startAssertion(opts);
const argsPublicKey = mockNavigatorGet.mock.calls[0][0].publicKey;
const credId = argsPublicKey.allowCredentials[0].id;
expect(new Uint8Array(argsPublicKey.challenge)).toEqual(new Uint8Array([102, 105, 122, 122]));
// Make sure the credential ID is an ArrayBuffer with a length of 64
expect(credId instanceof ArrayBuffer).toEqual(true);
expect(credId.byteLength).toEqual(64);
};
await checkWithOpts(goodOpts1);
await checkWithOpts(goodOpts3);
done();
});
test('should return base64url-encoded response values', async done => {
mockSupportsWebauthn.mockReturnValue(true);
mockNavigatorGet.mockImplementation(
(): Promise<AssertionCredential> => {
return new Promise(resolve => {
resolve({
id: 'foobar',
rawId: Buffer.from('foobar', 'ascii'),
response: {
authenticatorData: Buffer.from(mockAuthenticatorData, 'ascii'),
clientDataJSON: Buffer.from(mockClientDataJSON, 'ascii'),
signature: Buffer.from(mockSignature, 'ascii'),
userHandle: Buffer.from(mockUserHandle, 'ascii'),
},
getClientExtensionResults: () => ({}),
type: 'webauthn.get',
});
});
},
);
const response = await startAssertion(goodOpts1);
expect(response.rawId).toEqual('Zm9vYmFy');
expect(response.response.authenticatorData).toEqual('bW9ja0F1dGhlbnRpY2F0b3JEYXRh');
expect(response.response.clientDataJSON).toEqual('bW9ja0NsaWVudERhdGFKU09O');
expect(response.response.signature).toEqual('bW9ja1NpZ25hdHVyZQ');
expect(response.response.userHandle).toEqual('bW9ja1VzZXJIYW5kbGU');
done();
});
test("should throw error if WebAuthn isn't supported", async done => {
mockSupportsWebauthn.mockReturnValue(false);
await expect(startAssertion(goodOpts1)).rejects.toThrow(
'WebAuthn is not supported in this browser',
);
done();
});
test('should throw error if assertion is cancelled for some reason', async done => {
mockSupportsWebauthn.mockReturnValue(true);
mockNavigatorGet.mockImplementation(
(): Promise<null> => {
return new Promise(resolve => {
resolve(null);
});
},
);
await expect(startAssertion(goodOpts1)).rejects.toThrow('Assertion was not completed');
done();
});
test('should handle UTF-8 challenges', async done => {
mockSupportsWebauthn.mockReturnValue(true);
// Stub out a response so the method won't throw
mockNavigatorGet.mockImplementation(
(): Promise<any> => {
return new Promise(resolve => {
resolve({
response: {},
getClientExtensionResults: () => ({}),
});
});
},
);
await startAssertion(goodOpts2UTF8);
const argsPublicKey = mockNavigatorGet.mock.calls[0][0].publicKey;
expect(new Uint8Array(argsPublicKey.challenge)).toEqual(
new Uint8Array([
227,
130,
132,
227,
130,
140,
227,
130,
132,
227,
130,
140,
227,
129,
160,
227,
129,
156,
]),
);
done();
});
|