-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathwebauthn.server.ts
More file actions
66 lines (62 loc) · 1.61 KB
/
webauthn.server.ts
File metadata and controls
66 lines (62 loc) · 1.61 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
import { type RegistrationResponseJSON } from '@simplewebauthn/server'
import { createCookie } from 'react-router'
import { z } from 'zod'
import { getEnv } from './env.server.ts'
import { getDomainUrl } from './misc.ts'
export const passkeyCookie = createCookie('webauthn-challenge', {
path: '/',
sameSite: 'lax',
httpOnly: true,
maxAge: 60 * 60 * 2,
secure: getEnv().NODE_ENV === 'production',
secrets: [getEnv().SESSION_SECRET],
})
export const PasskeyCookieSchema = z.object({
challenge: z.string(),
userId: z.string(),
})
export const RegistrationResponseSchema = z.object({
id: z.string(),
rawId: z.string(),
response: z.object({
clientDataJSON: z.string(),
attestationObject: z.string(),
transports: z
.array(
z.enum([
'ble',
'cable',
'hybrid',
'internal',
'nfc',
'smart-card',
'usb',
]),
)
.optional(),
}),
authenticatorAttachment: z.enum(['cross-platform', 'platform']).optional(),
clientExtensionResults: z.object({
credProps: z
.object({
rk: z.boolean(),
})
.optional(),
}),
type: z.literal('public-key'),
}) satisfies z.ZodType<RegistrationResponseJSON>
export function getWebAuthnConfig(request: Request) {
const url = new URL(getDomainUrl(request))
return {
rpName: `KCD (${url.hostname})`,
rpID: url.hostname,
origin: url.origin,
// Common options for both registration and authentication
authenticatorSelection: {
// Required for discoverable credentials, which enables privacy-friendly
// passkey sign-in via form autofill / conditional UI.
residentKey: 'required',
userVerification: 'preferred',
},
} as const
}