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
|
import base64js from 'base64-js';
import {
AttestationCredential,
PublicKeyCredentialCreationOptionsJSON,
} from '@webauthntine/typescript-types';
import toUint8Array from '../helpers/toUint8Array';
import supportsWebauthn from '../helpers/supportsWebauthn';
import startAttestation from './startAttestation';
jest.mock('../helpers/supportsWebauthn');
const mockNavigatorCreate = window.navigator.credentials.create as jest.Mock;
const mockSupportsWebauthn = supportsWebauthn as jest.Mock;
const mockAttestationObject = 'mockAtte';
const mockClientDataJSON = 'mockClie';
const goodOpts1: PublicKeyCredentialCreationOptionsJSON = {
publicKey: {
challenge: 'fizz',
attestation: 'direct',
pubKeyCredParams: [
{
alg: -7,
type: 'public-key',
},
],
rp: {
id: '1234',
name: 'webauthntine',
},
user: {
id: '5678',
displayName: 'username',
name: 'username',
},
timeout: 1,
excludeCredentials: [{
id: 'authIdentifier',
type: 'public-key',
transports: ['internal'],
}],
},
};
beforeEach(() => {
mockNavigatorCreate.mockReset();
mockSupportsWebauthn.mockReset();
});
test('should convert options before passing to navigator.credentials.create(...)', async done => {
mockSupportsWebauthn.mockReturnValue(true);
// Stub out a response so the method won't throw
mockNavigatorCreate.mockImplementation(
(): Promise<any> => {
return new Promise(resolve => {
resolve({ response: {} });
});
},
);
await startAttestation(goodOpts1);
const argsPublicKey = mockNavigatorCreate.mock.calls[0][0].publicKey;
expect(argsPublicKey.challenge).toEqual(toUint8Array(goodOpts1.publicKey.challenge));
expect(argsPublicKey.user.id).toEqual(toUint8Array(goodOpts1.publicKey.user.id));
expect(argsPublicKey.excludeCredentials).toEqual([{
id: base64js.toByteArray('authIdentifier=='),
type: 'public-key',
transports: ['internal'],
}])
done();
});
test('should return base64-encoded response values', async done => {
mockSupportsWebauthn.mockReturnValue(true);
mockNavigatorCreate.mockImplementation(
(): Promise<AttestationCredential> => {
return new Promise(resolve => {
resolve({
id: 'foobar',
rawId: toUint8Array('foobar'),
response: {
attestationObject: base64js.toByteArray(mockAttestationObject),
clientDataJSON: base64js.toByteArray(mockClientDataJSON),
},
getClientExtensionResults: () => ({}),
type: 'webauthn.create',
});
});
},
);
const response = await startAttestation(goodOpts1);
expect(response).toEqual({
base64AttestationObject: mockAttestationObject,
base64ClientDataJSON: mockClientDataJSON,
});
done();
});
test("should throw error if WebAuthn isn't supported", async done => {
mockSupportsWebauthn.mockReturnValue(false);
await expect(startAttestation(goodOpts1)).rejects.toThrow(
'WebAuthn is not supported in this browser',
);
done();
});
test('should throw error if attestation is cancelled for some reason', async done => {
mockSupportsWebauthn.mockReturnValue(true);
mockNavigatorCreate.mockImplementation(
(): Promise<null> => {
return new Promise(resolve => {
resolve(null);
});
},
);
await expect(startAttestation(goodOpts1)).rejects.toThrow('Attestation was not completed');
done();
});
|