-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathutils.server.ts
More file actions
90 lines (83 loc) · 2.02 KB
/
utils.server.ts
File metadata and controls
90 lines (83 loc) · 2.02 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
import {
type AuthenticationResponseJSON,
type RegistrationResponseJSON,
} from '@simplewebauthn/server'
import { createCookie } from 'react-router'
import { ENV } from 'varlock/env'
import { z } from 'zod'
import { getDomainUrl } from '#app/utils/misc.tsx'
export const passkeyCookie = createCookie('webauthn-challenge', {
path: '/',
sameSite: 'lax',
httpOnly: true,
maxAge: 60 * 60 * 2,
secure: ENV.NODE_ENV === 'production',
secrets: [ENV.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>
const AuthenticationResponseSchema = z.object({
id: z.string(),
rawId: z.string(),
response: z.object({
clientDataJSON: z.string(),
authenticatorData: z.string(),
signature: z.string(),
userHandle: z.string().optional(),
}),
type: z.literal('public-key'),
clientExtensionResults: z.object({
appid: z.boolean().optional(),
credProps: z
.object({
rk: z.boolean().optional(),
})
.optional(),
hmacCreateSecret: z.boolean().optional(),
}),
}) satisfies z.ZodType<AuthenticationResponseJSON>
export const PasskeyLoginBodySchema = z.object({
authResponse: AuthenticationResponseSchema,
remember: z.boolean().default(false),
redirectTo: z.string().nullable(),
})
export function getWebAuthnConfig(request: Request) {
const url = new URL(getDomainUrl(request))
return {
rpName: url.hostname,
rpID: url.hostname,
origin: url.origin,
} as const
}