-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathpasskeys.test.ts
More file actions
144 lines (125 loc) · 4.87 KB
/
passkeys.test.ts
File metadata and controls
144 lines (125 loc) · 4.87 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
import { describe, expect, it } from 'vitest';
import type {
PublicKeyCredentialWithAuthenticatorAssertionResponse,
PublicKeyCredentialWithAuthenticatorAttestationResponse,
} from '../../../types';
import {
bufferToBase64Url,
convertJSONToPublicKeyCreateOptions,
convertJSONToPublicKeyRequestOptions,
serializePublicKeyCredential,
serializePublicKeyCredentialAssertion,
} from '../passkeys';
describe('Passkey utils', () => {
describe('serialization', () => {
it('convertJSONToPublicKeyCreateOptions()', () => {
const pkCreateOptions = {
rp: {
name: 'clerk.com',
id: 'clerk.com',
},
user: {
name: 'clerkUser',
displayName: 'Clerk User',
id: 'dXNlcl8xMjM', // user_123 encoded as base64url
},
excludeCredentials: [
{
type: 'public-key' as const,
id: 'cmFuZG9tX2lk',
},
],
authenticatorSelection: {
requireResidentKey: true,
residentKey: 'required' as const,
userVerification: 'required' as const,
},
attestation: 'none' as const,
pubKeyCredParams: [
{
type: 'public-key' as const,
alg: -7,
},
],
timeout: 10000,
challenge: 'Y2hhbGxlbmdlXzEyMw', // challenge_123 encoded as base64url
};
const result = convertJSONToPublicKeyCreateOptions(pkCreateOptions);
expect(result.rp).toEqual({
name: 'clerk.com',
id: 'clerk.com',
});
expect(result.attestation).toEqual('none');
expect(result.authenticatorSelection).toEqual({
requireResidentKey: true,
residentKey: 'required',
userVerification: 'required',
});
expect(bufferToBase64Url(result.user.id as ArrayBuffer)).toEqual(pkCreateOptions.user.id);
expect(bufferToBase64Url(result.excludeCredentials[0].id as ArrayBuffer)).toEqual(
pkCreateOptions.excludeCredentials[0].id,
);
});
it('convertJSONToPublicKeyCreateOptions()', () => {
const pkCreateOptions = {
rpId: 'clerk.com',
allowCredentials: [
{
type: 'public-key' as const,
id: 'cmFuZG9tX2lk',
},
],
userVerification: 'required' as const,
timeout: 10000,
challenge: 'Y2hhbGxlbmdlXzEyMw', // challenge_123 encoded as base64url
};
const result = convertJSONToPublicKeyRequestOptions(pkCreateOptions);
expect(result.rpId).toEqual('clerk.com');
expect(result.userVerification).toEqual('required');
expect(bufferToBase64Url(result.allowCredentials[0].id as ArrayBuffer)).toEqual(
pkCreateOptions.allowCredentials[0].id,
);
});
it('serializePublicKeyCredential()', () => {
const publicKeyCredential = {
type: 'public-key' as const,
id: 'credentialId_123',
rawId: new Uint8Array([99, 114, 101, 100, 101, 110, 116, 105, 97, 108, 73, 100, 95, 49, 50, 51]),
authenticatorAttachment: 'cross-platform' as AuthenticatorAttachment,
response: {
clientDataJSON: new Uint8Array([110, 116, 105, 97]),
attestationObject: new Uint8Array([108, 73, 100, 95, 49]),
getTransports: () => ['usb'] as AuthenticatorTransport[],
},
} as any as PublicKeyCredentialWithAuthenticatorAttestationResponse;
const result = serializePublicKeyCredential(publicKeyCredential);
expect(result.type).toEqual('public-key');
expect(result.id).toEqual('credentialId_123');
expect(result.rawId).toEqual('Y3JlZGVudGlhbElkXzEyMw');
expect(result.response.clientDataJSON).toEqual('bnRpYQ');
expect(result.response.attestationObject).toEqual('bElkXzE');
expect(result.response.transports).toEqual(['usb']);
});
it('serializePublicKeyCredentialAssertion()', () => {
const publicKeyCredential = {
type: 'public-key' as const,
id: 'credentialId_123',
rawId: new Uint8Array([99, 114, 101, 100, 101, 110, 116, 105, 97, 108, 73, 100, 95, 49, 50, 51]),
authenticatorAttachment: 'cross-platform' as AuthenticatorAttachment,
response: {
clientDataJSON: new Uint8Array([110, 116, 105, 97]),
signature: new Uint8Array([108, 73, 100, 95, 49]),
authenticatorData: new Uint8Array([108, 73, 100, 95, 49]),
userHandle: null,
},
} as any as PublicKeyCredentialWithAuthenticatorAssertionResponse;
const result = serializePublicKeyCredentialAssertion(publicKeyCredential);
expect(result.type).toEqual('public-key');
expect(result.id).toEqual('credentialId_123');
expect(result.rawId).toEqual('Y3JlZGVudGlhbElkXzEyMw');
expect(result.response.clientDataJSON).toEqual('bnRpYQ');
expect(result.response.signature).toEqual('bElkXzE');
expect(result.response.userHandle).toEqual(null);
});
});
});