-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwebauthn.ts
104 lines (83 loc) · 2.79 KB
/
webauthn.ts
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
import type {
AuthenticationResponseJSON,
PublicKeyCredentialRequestOptionsJSON,
} from '@simplewebauthn/types';
import { browserSupportsWebAuthnAutofill, startAuthentication } from '@simplewebauthn/browser';
import { goto } from '$app/navigation';
import type { VerificationResponse } from '../assertion/verify/+server';
export async function ceremony(fetch: Fetch) {
if (!(await browserSupportsWebAuthnAutofill())) {
throw new Error(
'Passkeys are not available in this browser. Please sign in using a one-time code instead.',
);
}
const assertionOptions = await requestAssertion(fetch);
let assertionResponse: AuthenticationResponseJSON;
try {
assertionResponse = await startAuthentication(assertionOptions, true);
} catch (cause) {
if (!(cause instanceof Error)) {
throw cause;
}
console.error(`Failed to start authentication: ${cause.message}`, {
assertionOptions,
error: cause,
});
throw new Error(
'Uh-oh, Something went wrong while signing you in. ' +
'Please try again or continue with your email address.',
{ cause },
);
}
let destination: string;
try {
destination = await verifyAssertion(fetch, assertionResponse);
} catch (cause) {
if (!(cause instanceof Error)) {
throw cause;
}
console.error(`Failed to verify assertion: ${cause.message}`, {
assertionOptions,
assertionResponse,
error: cause,
});
throw new Error(
'Uh-oh, Something went wrong while signing you in. ' +
'Please try again or continue with your email address.',
{ cause },
);
}
return goto(destination, { invalidateAll: true });
}
async function requestAssertion(fetch: Fetch) {
let assertionOptionsResponse: Response;
let assertionOptions: PublicKeyCredentialRequestOptionsJSON;
try {
assertionOptionsResponse = await fetch('/auth/assertion/generate', {
headers: { accept: 'application/json' },
});
assertionOptions = await assertionOptionsResponse.json();
} catch (error) {
if (!(error instanceof Error)) {
throw error;
}
throw new Error(`Failed to generate assertion options: ${error.message}`);
}
return assertionOptions;
}
async function verifyAssertion(fetch: Fetch, assertion: AuthenticationResponseJSON) {
const verificationResponse = await fetch('/auth/assertion/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(assertion),
});
const data: VerificationResponse = await verificationResponse.json();
if (!data) {
throw new Error('Verification failed: Unexpected payload: No verification data');
}
if (!data.verified) {
throw new Error('Verification failed: Unexpected state');
}
return data.destination;
}
type Fetch = typeof fetch;