@kayjs/auth 1.104.3 → 1.106.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/llms/admin-and-organizations.md +8 -8
- package/llms/admin-pages.md +2 -2
- package/llms/grouped-methods.md +33 -17
- package/llms/hardening-and-testing.md +9 -9
- package/llms/oauth-passkeys-two-factor.md +14 -14
- package/llms/oauth-providers.md +1 -1
- package/llms/oauth-server-and-tokens.md +7 -7
- package/llms/passwords-and-mails.md +15 -15
- package/llms/sessions-and-guards.md +9 -1
- package/llms/setup.md +6 -6
- package/llms/signed-in-components.md +20 -11
- package/llms.txt +1 -1
- package/package.json +10 -6
- package/src/account.ts +6 -15
- package/src/admin.ts +9 -25
- package/src/auth.ts +9 -6
- package/src/authorize/clients.ts +1 -1
- package/src/authorize/grants.ts +2 -4
- package/src/authorize/request.ts +2 -4
- package/src/bearer.ts +5 -11
- package/src/captcha.ts +5 -3
- package/src/components/sign-in-link.kay +19 -0
- package/src/components/sign-out-button.kay +17 -0
- package/src/components/signed-in.kay +7 -2
- package/src/components/user-menu.kay +11 -8
- package/src/context.ts +1 -1
- package/src/deletion.ts +2 -4
- package/src/email.ts +16 -28
- package/src/erase.ts +5 -4
- package/src/export.ts +2 -4
- package/src/failure.ts +37 -9
- package/src/guard.ts +8 -1
- package/src/guards.ts +12 -5
- package/src/index.ts +14 -16
- package/src/limits.ts +97 -5
- package/src/mail.ts +5 -3
- package/src/memoryStore.ts +6 -6
- package/src/messages/fr.ts +1 -0
- package/src/methods/account.ts +30 -14
- package/src/methods/admin.ts +31 -14
- package/src/methods/captcha.ts +18 -0
- package/src/methods/email.ts +16 -7
- package/src/methods/magicLink.ts +5 -5
- package/src/methods/oauth.ts +16 -7
- package/src/methods/oauthServer.ts +32 -6
- package/src/methods/organizations.ts +51 -20
- package/src/methods/passkeys.ts +13 -7
- package/src/methods/password.ts +10 -10
- package/src/methods/proof.ts +22 -0
- package/src/methods/sessions.ts +3 -2
- package/src/methods/tokens.ts +17 -5
- package/src/methods/twoFactor.ts +10 -10
- package/src/oauth/flow.ts +2 -4
- package/src/oauth/providers.ts +1 -1
- package/src/oauthTest.ts +8 -4
- package/src/organizations.ts +12 -30
- package/src/organizationsCore.ts +1 -1
- package/src/own.ts +21 -19
- package/src/pages/components/frame.kay +3 -3
- package/src/pages/components/passkey-add.kay +2 -2
- package/src/pages/components/passkey-sign-in.kay +2 -2
- package/src/pages/components/passkey-two-factor.kay +2 -2
- package/src/pages/components/provider-buttons.kay +1 -1
- package/src/pages/forms.ts +6 -6
- package/src/pages/index.ts +9 -8
- package/src/pages/routes/account/clients.kay +2 -2
- package/src/pages/routes/account/connections.kay +4 -4
- package/src/pages/routes/account/data.kay +7 -7
- package/src/pages/routes/account/index.kay +15 -15
- package/src/pages/routes/account/organizations/[org].kay +15 -15
- package/src/pages/routes/account/organizations/index.kay +5 -5
- package/src/pages/routes/account/security.kay +15 -15
- package/src/pages/routes/account/tokens.kay +4 -4
- package/src/pages/routes/admin/_layout.kay +1 -1
- package/src/pages/routes/admin/audit.kay +1 -1
- package/src/pages/routes/admin/users/[id].kay +11 -11
- package/src/pages/routes/confirm-email.kay +3 -3
- package/src/pages/routes/invitation.kay +8 -8
- package/src/pages/routes/magic-link.kay +3 -3
- package/src/pages/routes/oauth/authorize.kay +2 -2
- package/src/pages/routes/register.kay +4 -4
- package/src/pages/routes/reset-password.kay +6 -6
- package/src/pages/routes/sign-in.kay +10 -10
- package/src/pages/routes/two-factor.kay +4 -4
- package/src/pages/routes/verify-email.kay +8 -8
- package/src/passkeys.ts +10 -21
- package/src/password.ts +8 -11
- package/src/pg.ts +3 -3
- package/src/profile.ts +1 -3
- package/src/proof.ts +3 -5
- package/src/request.ts +77 -46
- package/src/requiredTwoFactor.ts +2 -2
- package/src/routes.ts +2 -2
- package/src/sessions.ts +3 -7
- package/src/setup.ts +6 -4
- package/src/sqlite.ts +6 -0
- package/src/test.ts +15 -3
- package/src/testHelpers.ts +16 -7
- package/src/testHook.ts +31 -0
- package/src/testing.ts +5 -4
- package/src/twoFactor.ts +9 -18
- package/src/viewer.ts +26 -4
- package/src/webauthn.ts +2 -2
- package/types/account.d.ts +10 -23
- package/types/account.d.ts.map +1 -1
- package/types/admin.d.ts +21 -39
- package/types/admin.d.ts.map +1 -1
- package/types/auth.d.ts +18 -5
- package/types/auth.d.ts.map +1 -1
- package/types/authorize/grants.d.ts +2 -5
- package/types/authorize/grants.d.ts.map +1 -1
- package/types/authorize/request.d.ts +3 -5
- package/types/authorize/request.d.ts.map +1 -1
- package/types/bearer.d.ts +9 -17
- package/types/bearer.d.ts.map +1 -1
- package/types/captcha.d.ts +3 -1
- package/types/captcha.d.ts.map +1 -1
- package/types/context.d.ts +1 -1
- package/types/deletion.d.ts +2 -5
- package/types/deletion.d.ts.map +1 -1
- package/types/email.d.ts +23 -28
- package/types/email.d.ts.map +1 -1
- package/types/erase.d.ts.map +1 -1
- package/types/export.d.ts +3 -5
- package/types/export.d.ts.map +1 -1
- package/types/failure.d.ts +22 -3
- package/types/failure.d.ts.map +1 -1
- package/types/guard.d.ts +2 -0
- package/types/guard.d.ts.map +1 -1
- package/types/guards.d.ts +2 -1
- package/types/guards.d.ts.map +1 -1
- package/types/index.d.ts +14 -16
- package/types/index.d.ts.map +1 -1
- package/types/limits.d.ts +57 -1
- package/types/limits.d.ts.map +1 -1
- package/types/mail.d.ts +5 -0
- package/types/mail.d.ts.map +1 -1
- package/types/messages/fr.d.ts +1 -0
- package/types/methods/account.d.ts +27 -13
- package/types/methods/account.d.ts.map +1 -1
- package/types/methods/admin.d.ts +39 -15
- package/types/methods/admin.d.ts.map +1 -1
- package/types/methods/captcha.d.ts +9 -0
- package/types/methods/captcha.d.ts.map +1 -0
- package/types/methods/email.d.ts +21 -7
- package/types/methods/email.d.ts.map +1 -1
- package/types/methods/magicLink.d.ts +9 -4
- package/types/methods/magicLink.d.ts.map +1 -1
- package/types/methods/oauth.d.ts +12 -7
- package/types/methods/oauth.d.ts.map +1 -1
- package/types/methods/oauthServer.d.ts +13 -5
- package/types/methods/oauthServer.d.ts.map +1 -1
- package/types/methods/organizations.d.ts +40 -15
- package/types/methods/organizations.d.ts.map +1 -1
- package/types/methods/passkeys.d.ts +18 -7
- package/types/methods/passkeys.d.ts.map +1 -1
- package/types/methods/password.d.ts +23 -10
- package/types/methods/password.d.ts.map +1 -1
- package/types/methods/proof.d.ts +12 -0
- package/types/methods/proof.d.ts.map +1 -0
- package/types/methods/sessions.d.ts +3 -2
- package/types/methods/sessions.d.ts.map +1 -1
- package/types/methods/tokens.d.ts +12 -5
- package/types/methods/tokens.d.ts.map +1 -1
- package/types/methods/twoFactor.d.ts +22 -10
- package/types/methods/twoFactor.d.ts.map +1 -1
- package/types/oauth/flow.d.ts +4 -6
- package/types/oauth/flow.d.ts.map +1 -1
- package/types/oauthTest.d.ts +2 -0
- package/types/oauthTest.d.ts.map +1 -1
- package/types/organizations.d.ts +24 -48
- package/types/organizations.d.ts.map +1 -1
- package/types/own.d.ts +1 -1
- package/types/own.d.ts.map +1 -1
- package/types/pages/forms.d.ts +5 -3
- package/types/pages/forms.d.ts.map +1 -1
- package/types/pages/index.d.ts +5 -2
- package/types/pages/index.d.ts.map +1 -1
- package/types/passkeys.d.ts +20 -27
- package/types/passkeys.d.ts.map +1 -1
- package/types/password.d.ts +13 -20
- package/types/password.d.ts.map +1 -1
- package/types/profile.d.ts +1 -4
- package/types/profile.d.ts.map +1 -1
- package/types/proof.d.ts +1 -4
- package/types/proof.d.ts.map +1 -1
- package/types/request.d.ts +33 -7
- package/types/request.d.ts.map +1 -1
- package/types/requiredTwoFactor.d.ts +3 -1
- package/types/requiredTwoFactor.d.ts.map +1 -1
- package/types/routes.d.ts +3 -1
- package/types/routes.d.ts.map +1 -1
- package/types/sessions.d.ts +3 -10
- package/types/sessions.d.ts.map +1 -1
- package/types/setup.d.ts.map +1 -1
- package/types/sqlite.d.ts +3 -0
- package/types/sqlite.d.ts.map +1 -0
- package/types/test.d.ts +4 -1
- package/types/test.d.ts.map +1 -1
- package/types/testHelpers.d.ts +1 -0
- package/types/testHelpers.d.ts.map +1 -1
- package/types/testHook.d.ts +15 -0
- package/types/testHook.d.ts.map +1 -0
- package/types/testing.d.ts.map +1 -1
- package/types/twoFactor.d.ts +10 -21
- package/types/twoFactor.d.ts.map +1 -1
- package/types/viewer.d.ts +3 -1
- package/types/viewer.d.ts.map +1 -1
|
@@ -19,7 +19,7 @@ export const changePassword = action(schemas.changePassword, async (input, ctx)
|
|
|
19
19
|
const result = await ctx.locals.auth.password.change(proofOf(input), input.newPassword)
|
|
20
20
|
|
|
21
21
|
if (!result.ok)
|
|
22
|
-
return ctx.
|
|
22
|
+
return ctx.deny(400, { data: 'errors' in result ? { password: result.errors.password } : { error: result.error.code } })
|
|
23
23
|
|
|
24
24
|
return 'saved'
|
|
25
25
|
})
|
|
@@ -28,59 +28,59 @@ export const changePassword = action(schemas.changePassword, async (input, ctx)
|
|
|
28
28
|
export const setUp = action(schemas.proof, async (input, ctx) => {
|
|
29
29
|
const result = await ctx.locals.auth.twoFactor.setUp(proofOf(input))
|
|
30
30
|
|
|
31
|
-
return result.ok ? result.setup.uri : ctx.
|
|
31
|
+
return result.ok ? result.setup.uri : ctx.deny(400, { data: { error: result.error.code } })
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
// Posts the URI back with the code, so a wrong code shows the same QR code again.
|
|
35
35
|
export const enable = action(schemas.enableTwoFactor, async ({ code }, ctx) => {
|
|
36
36
|
const result = await ctx.locals.auth.twoFactor.enable(code)
|
|
37
37
|
|
|
38
|
-
return result.ok ? result.recoveryCodes : ctx.
|
|
38
|
+
return result.ok ? result.recoveryCodes : ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'code' : result.error.code } })
|
|
39
39
|
})
|
|
40
40
|
|
|
41
41
|
export const regenerate = action(schemas.proof, async (input, ctx) => {
|
|
42
42
|
const result = await ctx.locals.auth.twoFactor.regenerateRecoveryCodes(proofOf(input))
|
|
43
43
|
|
|
44
|
-
return result.ok ? result.recoveryCodes : ctx.
|
|
44
|
+
return result.ok ? result.recoveryCodes : ctx.deny(400, { data: { error: result.error.code } })
|
|
45
45
|
})
|
|
46
46
|
|
|
47
47
|
export const disable = action(schemas.proof, async (input, ctx) => {
|
|
48
48
|
const result = await ctx.locals.auth.twoFactor.disable(proofOf(input))
|
|
49
49
|
|
|
50
|
-
return result.ok ? 'disabled' : ctx.
|
|
50
|
+
return result.ok ? 'disabled' : ctx.deny(400, { data: { error: result.error.code } })
|
|
51
51
|
})
|
|
52
52
|
|
|
53
53
|
export const passkeyOptions = action(schemas.proof, async (input, ctx) => {
|
|
54
54
|
const result = await ctx.locals.auth.passkeys.registrationOptions(proofOf(input))
|
|
55
55
|
|
|
56
|
-
return result.ok ? result.options : ctx.
|
|
56
|
+
return result.ok ? result.options : ctx.deny(400, { data: { error: result.error.code } })
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
export const addPasskey = action(schemas.registerPasskey, async (input, ctx) => {
|
|
60
60
|
const result = await ctx.locals.auth.passkeys.register(input.response, input.name)
|
|
61
61
|
|
|
62
|
-
return result.ok ? result.passkey.id : ctx.
|
|
62
|
+
return result.ok ? result.passkey.id : ctx.deny(400, { data: { error: result.error.code } })
|
|
63
63
|
})
|
|
64
64
|
|
|
65
65
|
export const renamePasskey = action(schemas.renamePasskey, async ({ id, name }, ctx) => {
|
|
66
|
-
return (await ctx.locals.auth.passkeys.
|
|
66
|
+
return (await ctx.locals.auth.passkeys.update(id, name)).ok ? name : ctx.deny(404, { data: { error: 'not-found' } })
|
|
67
67
|
})
|
|
68
68
|
|
|
69
69
|
export const removePasskey = action(schemas.removePasskey, async (input, ctx) => {
|
|
70
70
|
const result = await ctx.locals.auth.passkeys.revoke(input.id, proofOf(input))
|
|
71
71
|
|
|
72
|
-
return result.ok ? 'removed' : ctx.
|
|
72
|
+
return result.ok ? 'removed' : ctx.deny(400, { data: { error: result.error.code, id: input.id } })
|
|
73
73
|
})
|
|
74
74
|
|
|
75
75
|
const auth = ctx.locals.auth
|
|
76
76
|
const user = await auth.requireUser()
|
|
77
77
|
const features = auth.features()
|
|
78
|
-
const passwordSaved = changePassword.
|
|
79
|
-
const passwordRefused = changePassword.failed()?.data
|
|
80
|
-
const setupUri = setUp.
|
|
78
|
+
const passwordSaved = changePassword.status() === 'ready'
|
|
79
|
+
const passwordRefused = changePassword.form.failed()?.data
|
|
80
|
+
const setupUri = setUp.data() ?? enable.form.failed()?.values.uri
|
|
81
81
|
const setupKey = setupUri && new URL(setupUri).searchParams.get('secret')
|
|
82
|
-
const twoFactorError = setUp.failed()?.data?.error ?? enable.failed()?.data?.error ?? regenerate.failed()?.data?.error ?? disable.failed()?.data?.error
|
|
83
|
-
const recoveryCodes = enable.
|
|
82
|
+
const twoFactorError = setUp.form.failed()?.data?.error ?? enable.form.failed()?.data?.error ?? regenerate.form.failed()?.data?.error ?? disable.form.failed()?.data?.error
|
|
83
|
+
const recoveryCodes = enable.data() ?? regenerate.data()
|
|
84
84
|
// Where a guard sent the user from to turn two-factor on: back there once it is on and its recovery codes were seen.
|
|
85
85
|
const back = localPath(ctx.url.searchParams.get('redirect'), '')
|
|
86
86
|
|
|
@@ -89,7 +89,7 @@ if (back !== '' && user.twoFactor && !recoveryCodes)
|
|
|
89
89
|
|
|
90
90
|
const codesLeft = user.twoFactor ? await auth.twoFactor.recoveryCodesLeft() : 0
|
|
91
91
|
const passkeys = features.passkeys ? await auth.passkeys.list() : []
|
|
92
|
-
const removeRefused = removePasskey.failed()?.data
|
|
92
|
+
const removeRefused = removePasskey.form.failed()?.data
|
|
93
93
|
const passkeyErrors = Object.fromEntries((['proof', 'stale', 'limited', 'impersonating', 'expired', 'invalid', 'signed-out'] as const).map(code => [code, failure(code)]))
|
|
94
94
|
---
|
|
95
95
|
if features.password
|
|
@@ -23,19 +23,19 @@ export const create = action(schemas.createToken, async (input, ctx) => {
|
|
|
23
23
|
// The app's scopes are its own type, which this page cannot name: tokens.create refuses any it does not declare.
|
|
24
24
|
const result = await ctx.locals.auth.tokens.create(proofOf(input), input.label, { scopes: input.scopes?.length ? input.scopes as never[] : undefined, maxAge: expiry(input.days) })
|
|
25
25
|
|
|
26
|
-
return result.ok ? result.token : ctx.
|
|
26
|
+
return result.ok ? result.token : ctx.deny(400, { data: { error: result.error.code } })
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
export const revoke = action(schemas.id, async ({ id }, ctx) => {
|
|
30
|
-
return (await ctx.locals.auth.tokens.revoke(id)).ok ? 'revoked' : ctx.
|
|
30
|
+
return (await ctx.locals.auth.tokens.revoke(id)).ok ? 'revoked' : ctx.deny(404, { data: { error: 'not-found' } })
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
const auth = ctx.locals.auth
|
|
34
34
|
const user = await auth.requireUser()
|
|
35
35
|
const features = auth.features()
|
|
36
36
|
const tokens = await auth.tokens.list()
|
|
37
|
-
const created = create.
|
|
38
|
-
const refused = create.failed()?.data?.error
|
|
37
|
+
const created = create.data()
|
|
38
|
+
const refused = create.form.failed()?.data?.error
|
|
39
39
|
const date = (value: Date) => formatDate(value, { dateStyle: 'medium' })
|
|
40
40
|
---
|
|
41
41
|
PageSection(title={wording('auth.account.tokens.title', 'Access tokens')})
|
|
@@ -25,7 +25,7 @@ function cursorOf(value: string | null): unknown {
|
|
|
25
25
|
const auth = ctx.locals.auth
|
|
26
26
|
const features = auth.features()
|
|
27
27
|
const before = cursorOf(ctx.url.searchParams.get('before'))
|
|
28
|
-
const log = await auth.admin.
|
|
28
|
+
const log = await auth.admin.listAuditLog({ ...before !== undefined && { before: before as never }, limit: PAGE_SIZE })
|
|
29
29
|
const entries = log.ok ? log.entries : []
|
|
30
30
|
const older = entries.length === PAGE_SIZE ? `${localize(`${features.pages?.prefix ?? features.prefix}/admin/audit`)}?before=${encodeURIComponent(JSON.stringify({ at: entries.at(-1)!.at, id: entries.at(-1)!.id }))}` : undefined
|
|
31
31
|
const dateTime = (value: Date) => formatDate(value, { dateStyle: 'medium', timeStyle: 'short' })
|
|
@@ -27,40 +27,40 @@ function endOf(day: string | undefined): Date | null | 'until' {
|
|
|
27
27
|
export const grant = action(schemas.adminRole, async ({ role }, ctx) => {
|
|
28
28
|
const result = await ctx.locals.auth.admin.grantRole(String(ctx.params.id), role as never)
|
|
29
29
|
|
|
30
|
-
return result.ok ? 'done' : ctx.
|
|
30
|
+
return result.ok ? 'done' : ctx.deny(400, { data: { error: result.error.code } })
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
export const revoke = action(schemas.adminRole, async ({ role }, ctx) => {
|
|
34
34
|
const result = await ctx.locals.auth.admin.revokeRole(String(ctx.params.id), role as never)
|
|
35
35
|
|
|
36
|
-
return result.ok ? 'done' : ctx.
|
|
36
|
+
return result.ok ? 'done' : ctx.deny(400, { data: { error: result.error.code } })
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
export const ban = action(schemas.adminBan, async ({ reason, until }, ctx) => {
|
|
40
40
|
const end = endOf(until)
|
|
41
41
|
|
|
42
42
|
if (end === 'until')
|
|
43
|
-
return ctx.
|
|
43
|
+
return ctx.deny(400, { data: { error: 'until' } })
|
|
44
44
|
|
|
45
45
|
const result = await ctx.locals.auth.admin.ban(String(ctx.params.id), { ...reason?.trim() && { reason: reason.trim() }, ...end && { until: end } })
|
|
46
46
|
|
|
47
|
-
return result.ok ? 'done' : ctx.
|
|
47
|
+
return result.ok ? 'done' : ctx.deny(400, { data: { error: result.error.code } })
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
export const unban = action(async (ctx) => {
|
|
51
51
|
const result = await ctx.locals.auth.admin.unban(String(ctx.params.id))
|
|
52
52
|
|
|
53
|
-
return result.ok ? 'done' : ctx.
|
|
53
|
+
return result.ok ? 'done' : ctx.deny(400, { data: { error: result.error.code } })
|
|
54
54
|
})
|
|
55
55
|
|
|
56
56
|
export const remove = action(schemas.adminDelete, async ({ confirm }, ctx) => {
|
|
57
57
|
if (confirm !== true)
|
|
58
|
-
return ctx.
|
|
58
|
+
return ctx.deny(400, { data: { error: 'form' } })
|
|
59
59
|
|
|
60
60
|
const result = await ctx.locals.auth.admin.deleteUser(String(ctx.params.id))
|
|
61
61
|
|
|
62
62
|
if (!result.ok)
|
|
63
|
-
return ctx.
|
|
63
|
+
return ctx.deny(400, { data: { error: result.error.code } })
|
|
64
64
|
|
|
65
65
|
const { pages, prefix } = ctx.locals.auth.features()
|
|
66
66
|
|
|
@@ -70,7 +70,7 @@ export const remove = action(schemas.adminDelete, async ({ confirm }, ctx) => {
|
|
|
70
70
|
export const impersonate = action(async (ctx) => {
|
|
71
71
|
const result = await ctx.locals.auth.admin.impersonate(String(ctx.params.id))
|
|
72
72
|
|
|
73
|
-
return result.ok ? ctx.redirect(localize(ctx.locals.auth.features().pages?.home ?? '/')) : ctx.
|
|
73
|
+
return result.ok ? ctx.redirect(localize(ctx.locals.auth.features().pages?.home ?? '/')) : ctx.deny(400, { data: { error: result.error.code } })
|
|
74
74
|
})
|
|
75
75
|
|
|
76
76
|
// An admin who signed in long ago signs in again and comes back to this user.
|
|
@@ -99,11 +99,11 @@ const user = found.user
|
|
|
99
99
|
const me = await auth.user()
|
|
100
100
|
const own = me?.id === user.id
|
|
101
101
|
const base = localize(`${features.pages?.prefix ?? features.prefix}/admin`)
|
|
102
|
-
const declared = auth.admin.
|
|
102
|
+
const declared = auth.admin.listRoles()
|
|
103
103
|
const grantable = declared.filter(role => !user.roles.includes(role))
|
|
104
|
-
const log = await auth.admin.
|
|
104
|
+
const log = await auth.admin.listAuditLog({ userId: user.id, limit: 20 })
|
|
105
105
|
const entries = log.ok ? log.entries : []
|
|
106
|
-
const refused = [grant, revoke, ban, unban, remove, impersonate].map(
|
|
106
|
+
const refused = [grant, revoke, ban, unban, remove, impersonate].map(action => action.form.failed()?.data?.error).find(error => error !== undefined)
|
|
107
107
|
const date = (value: Date) => formatDate(value, { dateStyle: 'medium' })
|
|
108
108
|
const dateTime = (value: Date) => formatDate(value, { dateStyle: 'medium', timeStyle: 'short' })
|
|
109
109
|
const locale = ctx.locale
|
|
@@ -16,13 +16,13 @@ export const confirm = action(schemas.token, async ({ token }, ctx) => {
|
|
|
16
16
|
const result = await ctx.locals.auth.email.confirmChange(token)
|
|
17
17
|
|
|
18
18
|
if (!result.ok)
|
|
19
|
-
return ctx.
|
|
19
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'link' : result.error.code } })
|
|
20
20
|
|
|
21
21
|
return ctx.redirect(localize(result.redirect))
|
|
22
22
|
})
|
|
23
23
|
|
|
24
|
-
const token = ctx.url.searchParams.get('token') ?? confirm.failed()?.values.token
|
|
25
|
-
const error = confirm.failed()?.data?.error
|
|
24
|
+
const token = ctx.url.searchParams.get('token') ?? confirm.form.failed()?.values.token
|
|
25
|
+
const error = confirm.form.failed()?.data?.error
|
|
26
26
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
27
27
|
---
|
|
28
28
|
head
|
|
@@ -16,12 +16,12 @@ export const accept = action(schemas.id, async ({ id }, ctx) => {
|
|
|
16
16
|
const auth = ctx.locals.auth
|
|
17
17
|
|
|
18
18
|
if (!auth.features().organizations)
|
|
19
|
-
return ctx.
|
|
19
|
+
return ctx.deny(404, { data: { error: 'not-found' } })
|
|
20
20
|
|
|
21
21
|
const result = await auth.organizations.acceptInvitation(id)
|
|
22
22
|
|
|
23
23
|
if (!result.ok)
|
|
24
|
-
return ctx.
|
|
24
|
+
return ctx.deny(400, { data: { error: result.error.code } })
|
|
25
25
|
|
|
26
26
|
const { organization } = result.membership
|
|
27
27
|
|
|
@@ -32,11 +32,11 @@ export const reject = action(schemas.id, async ({ id }, ctx) => {
|
|
|
32
32
|
const auth = ctx.locals.auth
|
|
33
33
|
|
|
34
34
|
if (!auth.features().organizations)
|
|
35
|
-
return ctx.
|
|
35
|
+
return ctx.deny(404, { data: { error: 'not-found' } })
|
|
36
36
|
|
|
37
37
|
const result = await auth.organizations.rejectInvitation(id)
|
|
38
38
|
|
|
39
|
-
return result.ok ? ctx.redirect(organizationsPage(auth.features())) : ctx.
|
|
39
|
+
return result.ok ? ctx.redirect(organizationsPage(auth.features())) : ctx.deny(400, { data: { error: result.error.code } })
|
|
40
40
|
})
|
|
41
41
|
|
|
42
42
|
// Signs the wrong account out and comes back to this link to sign in with the right one.
|
|
@@ -44,7 +44,7 @@ export const switchAccount = action(schemas.id, async ({ id }, ctx) => {
|
|
|
44
44
|
const auth = ctx.locals.auth
|
|
45
45
|
|
|
46
46
|
if (!auth.features().organizations)
|
|
47
|
-
return ctx.
|
|
47
|
+
return ctx.deny(404, { data: { error: 'not-found' } })
|
|
48
48
|
|
|
49
49
|
const { signInPage, pages, prefix } = auth.features()
|
|
50
50
|
const signIn = localize(signInPage)
|
|
@@ -61,15 +61,15 @@ if (!features.organizations)
|
|
|
61
61
|
ctx.notFound()
|
|
62
62
|
|
|
63
63
|
const prefix = features.pages?.prefix ?? features.prefix
|
|
64
|
-
const requested = ctx.url.searchParams.get('invitation') ?? accept.failed()?.values.id ?? reject.failed()?.values.id ?? ''
|
|
64
|
+
const requested = ctx.url.searchParams.get('invitation') ?? accept.form.failed()?.values.id ?? reject.form.failed()?.values.id ?? ''
|
|
65
65
|
const id = requested.length <= ID_MAX ? requested : ''
|
|
66
|
-
const invitation = id !== '' ? await auth.organizations.
|
|
66
|
+
const invitation = id !== '' ? await auth.organizations.getInvitation(id) : null
|
|
67
67
|
const user = invitation && await auth.via() === 'session' ? await auth.user() : null
|
|
68
68
|
const signedIn = user !== null && !user.guest
|
|
69
69
|
const mine = signedIn && user.email === invitation?.email
|
|
70
70
|
const unverified = mine && !user.emailVerified
|
|
71
71
|
const back = encodeURIComponent(`${prefix}/invitation?invitation=${encodeURIComponent(id)}`)
|
|
72
|
-
const refused = [accept, reject].map(
|
|
72
|
+
const refused = [accept, reject].map(action => action.form.failed()?.data?.error).find(error => error !== undefined)
|
|
73
73
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
74
74
|
---
|
|
75
75
|
head
|
|
@@ -16,7 +16,7 @@ export const spend = action(schemas.token, async ({ token }, ctx) => {
|
|
|
16
16
|
const result = await ctx.locals.auth.magicLink.signIn(token)
|
|
17
17
|
|
|
18
18
|
if (!result.ok)
|
|
19
|
-
return ctx.
|
|
19
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'link' : result.error.code } })
|
|
20
20
|
|
|
21
21
|
return ctx.redirect(localize(result.redirect))
|
|
22
22
|
})
|
|
@@ -24,8 +24,8 @@ export const spend = action(schemas.token, async ({ token }, ctx) => {
|
|
|
24
24
|
if (!ctx.locals.auth.features().magicLink)
|
|
25
25
|
ctx.notFound()
|
|
26
26
|
|
|
27
|
-
const token = ctx.url.searchParams.get('token') ?? spend.failed()?.values.token
|
|
28
|
-
const error = spend.failed()?.data?.error
|
|
27
|
+
const token = ctx.url.searchParams.get('token') ?? spend.form.failed()?.values.token
|
|
28
|
+
const error = spend.form.failed()?.data?.error
|
|
29
29
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
30
30
|
---
|
|
31
31
|
head
|
|
@@ -15,11 +15,11 @@ const FIELDS = ['client_id', 'redirect_uri', 'response_type', 'code_challenge',
|
|
|
15
15
|
export const decide = action(schemas.oauthConsent, async ({ decision, ...fields }, ctx) => {
|
|
16
16
|
const result = await ctx.locals.auth.oauthServer.decide({ ...fields, scope: fields.scope === '' ? undefined : fields.scope, state: fields.state === '' ? undefined : fields.state, resource: fields.resource === '' ? undefined : fields.resource }, decision === 'allow')
|
|
17
17
|
|
|
18
|
-
return result.ok ? ctx.redirect(result.redirect) : ctx.
|
|
18
|
+
return result.ok ? ctx.redirect(result.redirect) : ctx.deny(400, { data: { error: result.error.code } })
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
const auth = ctx.locals.auth
|
|
22
|
-
const refused = decide.failed()?.data?.error
|
|
22
|
+
const refused = decide.form.failed()?.data?.error
|
|
23
23
|
const request: Parameters<typeof auth.oauthServer.authorize>[0] = {}
|
|
24
24
|
|
|
25
25
|
for (const field of FIELDS) {
|
|
@@ -18,7 +18,7 @@ export const register = action(schemas.register, async (input, ctx) => {
|
|
|
18
18
|
const result = await ctx.locals.auth.password.register({ email: input.email, password: input.password, ...(input.username === undefined ? {} : { username: input.username }), locale: ctx.locale }, { remember: remembered(ctx.locals.auth.features(), input.remember), redirect: input.redirect, captcha: captchaOf(input) })
|
|
19
19
|
|
|
20
20
|
if (!result.ok)
|
|
21
|
-
return ctx.
|
|
21
|
+
return ctx.deny(400, { data: 'errors' in result ? { fields: result.errors } : { error: refusedFor(result.error.code, input) } })
|
|
22
22
|
|
|
23
23
|
if (!result.signedIn)
|
|
24
24
|
return 'verify'
|
|
@@ -41,10 +41,10 @@ if (user && !user.guest)
|
|
|
41
41
|
|
|
42
42
|
const { askRemember, providerLinks } = rememberOf(features, redirect, provider => wording('auth.signIn.provider', 'Continue with {provider}', { provider }), features.providers)
|
|
43
43
|
const captcha = await captchaFor(auth, features.captcha, 'register')
|
|
44
|
-
const refused = register.failed()?.data
|
|
44
|
+
const refused = register.form.failed()?.data
|
|
45
45
|
const fields = refused && 'fields' in refused ? refused.fields : {}
|
|
46
|
-
const error = refusal(register.failed(), features.captcha)
|
|
47
|
-
const verify = register.
|
|
46
|
+
const error = refusal(register.form.failed(), features.captcha)
|
|
47
|
+
const verify = register.status() === 'ready'
|
|
48
48
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
49
49
|
const privacy = features.pages?.privacy
|
|
50
50
|
const purpose = features.pages?.purpose
|
|
@@ -16,14 +16,14 @@ export const page = { guards: false, security: { headers: { 'referrer-policy': '
|
|
|
16
16
|
export const request = action(schemas.requestReset, async (input, ctx) => {
|
|
17
17
|
const result = await ctx.locals.auth.password.requestReset(input.email, { redirect: input.redirect, captcha: captchaOf(input) })
|
|
18
18
|
|
|
19
|
-
return result.ok ? 'sent' : ctx.
|
|
19
|
+
return result.ok ? 'sent' : ctx.deny(400, { data: { error: refusedFor(result.error.code, input) } })
|
|
20
20
|
}, guarded)
|
|
21
21
|
|
|
22
22
|
export const reset = action(schemas.reset, async ({ token, password }, ctx) => {
|
|
23
23
|
const result = await ctx.locals.auth.password.reset(token, password)
|
|
24
24
|
|
|
25
25
|
if (!result.ok)
|
|
26
|
-
return ctx.
|
|
26
|
+
return ctx.deny(400, { data: 'errors' in result ? { password: result.errors.password } : { error: result.error.code === 'invalid' ? 'link' : result.error.code } })
|
|
27
27
|
|
|
28
28
|
return ctx.redirect(localize(result.redirect))
|
|
29
29
|
})
|
|
@@ -35,12 +35,12 @@ if (!features.password)
|
|
|
35
35
|
ctx.notFound()
|
|
36
36
|
|
|
37
37
|
const redirect = localPath(ctx.url.searchParams.get('redirect'), localize(features.pages?.home ?? '/'))
|
|
38
|
-
const token = ctx.url.searchParams.get('token') ?? reset.failed()?.values.token
|
|
38
|
+
const token = ctx.url.searchParams.get('token') ?? reset.form.failed()?.values.token
|
|
39
39
|
// Only the request form asks for a captcha, so a visit with a token never reads the client's attempts.
|
|
40
40
|
const captcha = token ? { shown: false, challenge: null } : await captchaFor(auth, features.captcha, 'reset')
|
|
41
|
-
const requestError = refusal(request.failed(), features.captcha)
|
|
42
|
-
const refused = reset.failed()?.data
|
|
43
|
-
const sent = request.
|
|
41
|
+
const requestError = refusal(request.form.failed(), features.captcha)
|
|
42
|
+
const refused = reset.form.failed()?.data
|
|
43
|
+
const sent = request.status() === 'ready'
|
|
44
44
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
45
45
|
---
|
|
46
46
|
head
|
|
@@ -19,7 +19,7 @@ export const password = action(schemas.signIn, async (input, ctx) => {
|
|
|
19
19
|
const result = await ctx.locals.auth.password.signIn({ identifier: input.identifier, password: input.password }, { remember: remembered(ctx.locals.auth.features(), input.remember), redirect: input.redirect, captcha: captchaOf(input) })
|
|
20
20
|
|
|
21
21
|
if (!result.ok)
|
|
22
|
-
return ctx.
|
|
22
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'credentials' : refusedFor(result.error.code, input) } })
|
|
23
23
|
|
|
24
24
|
return ctx.redirect(localize(result.redirect))
|
|
25
25
|
}, guarded)
|
|
@@ -27,24 +27,24 @@ export const password = action(schemas.signIn, async (input, ctx) => {
|
|
|
27
27
|
export const magicLink = action(schemas.magicLink, async (input, ctx) => {
|
|
28
28
|
const result = await ctx.locals.auth.magicLink.request(input.email, { redirect: input.redirect, captcha: captchaOf(input), remember: remembered(ctx.locals.auth.features(), input.remember) })
|
|
29
29
|
|
|
30
|
-
return result.ok ? input.email : ctx.
|
|
30
|
+
return result.ok ? input.email : ctx.deny(400, { data: { error: refusedFor(result.error.code, input) } })
|
|
31
31
|
}, guarded)
|
|
32
32
|
|
|
33
33
|
export const emailCode = action(schemas.emailCode, async ({ code }, ctx) => {
|
|
34
|
-
const result = await ctx.locals.auth.
|
|
34
|
+
const result = await ctx.locals.auth.email.signInWithCode(code)
|
|
35
35
|
|
|
36
36
|
if (!result.ok)
|
|
37
|
-
return ctx.
|
|
37
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'code' : result.error.code } })
|
|
38
38
|
|
|
39
39
|
return ctx.redirect(localize(result.redirect))
|
|
40
40
|
})
|
|
41
41
|
|
|
42
|
-
export const passkeyOptions = action(async ctx => ctx.locals.auth.features().passkeys ? ctx.locals.auth.passkeys.signInOptions() : ctx.
|
|
42
|
+
export const passkeyOptions = action(async ctx => ctx.locals.auth.features().passkeys ? ctx.locals.auth.passkeys.signInOptions() : ctx.deny(404, { data: { error: 'not-found' } }))
|
|
43
43
|
|
|
44
44
|
export const passkey = action(schemas.passkey, async (input, ctx) => {
|
|
45
45
|
const result = await ctx.locals.auth.passkeys.signIn(input.response, { redirect: input.redirect, remember: remembered(ctx.locals.auth.features(), input.remember) })
|
|
46
46
|
|
|
47
|
-
return !result.ok ? ctx.
|
|
47
|
+
return !result.ok ? ctx.deny(400, { data: { error: result.error.code } }) : ctx.redirect(localize(result.redirect))
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
const auth = ctx.locals.auth
|
|
@@ -60,10 +60,10 @@ if (user && !user.guest)
|
|
|
60
60
|
const [captchaSignIn, captchaMagic] = await Promise.all([captchaFor(auth, features.captcha, 'sign-in'), captchaFor(auth, features.captcha, 'magic-link')])
|
|
61
61
|
const pending = await auth.email.pendingCode()
|
|
62
62
|
const oauthError = oauthFailure(ctx.url.searchParams.get('error'))
|
|
63
|
-
const signInError = refusal(password.failed(), features.captcha)
|
|
64
|
-
const mailError = refusal(magicLink.failed(), features.captcha)
|
|
65
|
-
const codeError = emailCode.failed()?.data?.error
|
|
66
|
-
const mailedTo = magicLink.
|
|
63
|
+
const signInError = refusal(password.form.failed(), features.captcha)
|
|
64
|
+
const mailError = refusal(magicLink.form.failed(), features.captcha)
|
|
65
|
+
const codeError = emailCode.form.failed()?.data?.error
|
|
66
|
+
const mailedTo = magicLink.data()
|
|
67
67
|
const { askRemember, providerLinks } = rememberOf(features, redirect, provider => wording('auth.signIn.provider', 'Continue with {provider}', { provider }), features.providers)
|
|
68
68
|
const codes = features.codes?.['magic-link'] ?? 'link'
|
|
69
69
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
@@ -16,7 +16,7 @@ export const verify = action(schemas.twoFactor, async ({ code }, ctx) => {
|
|
|
16
16
|
const result = await ctx.locals.auth.twoFactor.verify(code)
|
|
17
17
|
|
|
18
18
|
if (!result.ok)
|
|
19
|
-
return ctx.
|
|
19
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'code' : result.error.code } })
|
|
20
20
|
|
|
21
21
|
return ctx.redirect(localize(result.redirect))
|
|
22
22
|
})
|
|
@@ -24,14 +24,14 @@ export const verify = action(schemas.twoFactor, async ({ code }, ctx) => {
|
|
|
24
24
|
export const passkeyOptions = action(async (ctx) => {
|
|
25
25
|
const result = await ctx.locals.auth.twoFactor.passkeyOptions()
|
|
26
26
|
|
|
27
|
-
return result.ok ? result.options : ctx.
|
|
27
|
+
return result.ok ? result.options : ctx.deny(400, { data: { error: result.error.code } })
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
export const passkey = action(schemas.passkey, async (input, ctx) => {
|
|
31
31
|
const result = await ctx.locals.auth.twoFactor.verifyPasskey(input.response)
|
|
32
32
|
|
|
33
33
|
if (!result.ok)
|
|
34
|
-
return ctx.
|
|
34
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'code' : result.error.code } })
|
|
35
35
|
|
|
36
36
|
return ctx.redirect(localize(result.redirect))
|
|
37
37
|
})
|
|
@@ -40,7 +40,7 @@ const auth = ctx.locals.auth
|
|
|
40
40
|
const features = auth.features()
|
|
41
41
|
const pending = await auth.twoFactor.pending()
|
|
42
42
|
const redirect = localPath(ctx.url.searchParams.get('redirect'), localize(features.pages?.home ?? '/'))
|
|
43
|
-
const error = verify.failed()?.data?.error
|
|
43
|
+
const error = verify.form.failed()?.data?.error
|
|
44
44
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
45
45
|
---
|
|
46
46
|
head
|
|
@@ -17,7 +17,7 @@ export const verify = action(schemas.verify, async ({ token, password }, ctx) =>
|
|
|
17
17
|
const result = await ctx.locals.auth.email.verify(token, password)
|
|
18
18
|
|
|
19
19
|
if (!result.ok)
|
|
20
|
-
return ctx.
|
|
20
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'link' : result.error.code } })
|
|
21
21
|
|
|
22
22
|
return ctx.redirect(localize(result.redirect))
|
|
23
23
|
})
|
|
@@ -26,7 +26,7 @@ export const code = action(schemas.emailCode, async ({ code }, ctx) => {
|
|
|
26
26
|
const result = await ctx.locals.auth.email.verifyCode(code)
|
|
27
27
|
|
|
28
28
|
if (!result.ok)
|
|
29
|
-
return ctx.
|
|
29
|
+
return ctx.deny(400, { data: { error: result.error.code === 'invalid' ? 'code' : result.error.code } })
|
|
30
30
|
|
|
31
31
|
return ctx.redirect(localize(result.redirect))
|
|
32
32
|
})
|
|
@@ -34,16 +34,16 @@ export const code = action(schemas.emailCode, async ({ code }, ctx) => {
|
|
|
34
34
|
export const resend = action(async (ctx) => {
|
|
35
35
|
const result = await ctx.locals.auth.email.sendVerification()
|
|
36
36
|
|
|
37
|
-
return result.ok ? 'sent' : ctx.
|
|
37
|
+
return result.ok ? 'sent' : ctx.deny(400, { data: { error: result.error.code } })
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
const auth = ctx.locals.auth
|
|
41
|
-
const token = ctx.url.searchParams.get('token') ?? verify.failed()?.values.token
|
|
41
|
+
const token = ctx.url.searchParams.get('token') ?? verify.form.failed()?.values.token
|
|
42
42
|
const [user, pending] = await Promise.all([auth.user(), auth.email.pendingCode()])
|
|
43
|
-
const refused = verify.failed()?.data?.error
|
|
44
|
-
const codeError = code.failed()?.data?.error
|
|
45
|
-
const resendError = resend.failed()?.data?.error
|
|
46
|
-
const resent = resend.
|
|
43
|
+
const refused = verify.form.failed()?.data?.error
|
|
44
|
+
const codeError = code.form.failed()?.data?.error
|
|
45
|
+
const resendError = resend.form.failed()?.data?.error
|
|
46
|
+
const resent = resend.status() === 'ready'
|
|
47
47
|
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
48
48
|
---
|
|
49
49
|
head
|
package/src/passkeys.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { Core } from './core'
|
|
2
|
-
import type {
|
|
2
|
+
import type { Result } from './failure'
|
|
3
3
|
import type { Proof, ProofFailure } from './proof'
|
|
4
4
|
import type { RequestState } from './state'
|
|
5
5
|
import type { PasskeyRecord, UserRecord } from './store'
|
|
6
|
-
import type { TwoFactorResult } from './twoFactor'
|
|
7
6
|
import type { AuthenticationResponseJSON, PublicKeyCredentialCreationOptionsJSON, PublicKeyCredentialRequestOptionsJSON, RegistrationResponseJSON } from './webauthn'
|
|
8
7
|
import { Buffer } from 'node:buffer'
|
|
9
8
|
import { localPath } from 'kay'
|
|
@@ -58,15 +57,9 @@ export interface Passkey {
|
|
|
58
57
|
lastUsedAt: Date | null
|
|
59
58
|
}
|
|
60
59
|
|
|
61
|
-
export type PasskeyOptionsResult<Options, Failure extends string = 'signed-out' | 'none' | 'expired' | 'off'> = { ok: true, options: Options } | AuthFailed<Failure>
|
|
62
|
-
|
|
63
|
-
export type RegisterPasskeyResult = { ok: true, passkey: Passkey } | AuthFailed<'signed-out' | 'expired' | 'invalid'>
|
|
64
|
-
|
|
65
|
-
export type PasskeySignInResult = { ok: true, redirect: string } | AuthFailed<'expired' | 'invalid' | 'limited' | 'banned'>
|
|
66
|
-
|
|
67
60
|
// The options for the browser's `navigator.credentials.create`, through `makePasskey` of `@kayjs/auth/browser`, once `proof` re-proves the user.
|
|
68
61
|
// A passkey is a way in that skips the second factor and outlives every sign-out, so a stolen session alone may not add one, and the second factor proves it when it is on.
|
|
69
|
-
export async function passkeyRegistrationOptions(state: RequestState, proof: Proof): Promise<
|
|
62
|
+
export async function passkeyRegistrationOptions(state: RequestState, proof: Proof): Promise<Result<{ options: PublicKeyCredentialCreationOptionsJSON }, ProofFailure>> {
|
|
70
63
|
const relying = relyingParty(state.core)
|
|
71
64
|
|
|
72
65
|
if (!(await state.current()) || state.via() !== 'session')
|
|
@@ -93,7 +86,7 @@ export async function passkeyRegistrationOptions(state: RequestState, proof: Pro
|
|
|
93
86
|
}
|
|
94
87
|
|
|
95
88
|
// Keeps the passkey the browser made from `passkeys.registrationOptions`, under `name` or the device it was made on.
|
|
96
|
-
export async function registerPasskey(state: RequestState, response: RegistrationResponseJSON, name?: string): Promise<
|
|
89
|
+
export async function registerPasskey(state: RequestState, response: RegistrationResponseJSON, name?: string): Promise<Result<{ passkey: Passkey }, 'signed-out' | 'expired' | 'invalid'>> {
|
|
97
90
|
const { core } = state
|
|
98
91
|
const relying = relyingParty(core)
|
|
99
92
|
const record = await state.current()
|
|
@@ -137,9 +130,7 @@ export async function listPasskeys(state: RequestState): Promise<Passkey[]> {
|
|
|
137
130
|
return record ? (await state.core.store.passkeys.listByUser(record.id)).map(passkey => publicPasskey(passkey)) : []
|
|
138
131
|
}
|
|
139
132
|
|
|
140
|
-
export
|
|
141
|
-
|
|
142
|
-
export async function renamePasskey(state: RequestState, id: string, name: string): Promise<RenamePasskeyResult> {
|
|
133
|
+
export async function renamePasskey(state: RequestState, id: string, name: string): Promise<Result<object, 'signed-out' | 'not-found' | 'invalid-name'>> {
|
|
143
134
|
const record = await state.current()
|
|
144
135
|
|
|
145
136
|
if (!record || state.via() !== 'session')
|
|
@@ -159,10 +150,8 @@ export async function renamePasskey(state: RequestState, id: string, name: strin
|
|
|
159
150
|
return { ok: true }
|
|
160
151
|
}
|
|
161
152
|
|
|
162
|
-
export type RevokePasskeyResult = { ok: true } | AuthFailed<ProofFailure | 'not-found' | 'last-way-in'>
|
|
163
|
-
|
|
164
153
|
// Removes one of the signed-in user's passkeys once `proof` re-proves them, unless it is their last way in.
|
|
165
|
-
export async function removePasskey(state: RequestState, id: string, proof: Proof): Promise<
|
|
154
|
+
export async function removePasskey(state: RequestState, id: string, proof: Proof): Promise<Result<object, ProofFailure | 'not-found' | 'last-way-in'>> {
|
|
166
155
|
const { core } = state
|
|
167
156
|
const proved = await reprove(state, proof)
|
|
168
157
|
|
|
@@ -206,7 +195,7 @@ export async function passkeySignInOptions(state: RequestState): Promise<PublicK
|
|
|
206
195
|
}
|
|
207
196
|
|
|
208
197
|
// Signs in the user whose passkey answered `passkeys.signInOptions`. A passkey with user verification is two factors in one, so no second factor is asked after it.
|
|
209
|
-
export async function signInWithPasskey(state: RequestState, response: AuthenticationResponseJSON, redirect: string | undefined, signIn: (record: UserRecord, method: string) => Promise<void>): Promise<
|
|
198
|
+
export async function signInWithPasskey(state: RequestState, response: AuthenticationResponseJSON, redirect: string | undefined, signIn: (record: UserRecord, method: string) => Promise<void>): Promise<Result<{ redirect: string }, 'expired' | 'invalid' | 'limited' | 'banned'>> {
|
|
210
199
|
const { core } = state
|
|
211
200
|
|
|
212
201
|
if (await spend(core.rates, `passkey:${state.client}`, core.limits.signIn))
|
|
@@ -239,7 +228,7 @@ export async function signInWithPasskey(state: RequestState, response: Authentic
|
|
|
239
228
|
}
|
|
240
229
|
|
|
241
230
|
// The options for answering a pending sign-in's second factor with one of that user's passkeys.
|
|
242
|
-
export async function twoFactorPasskeyOptions(state: RequestState): Promise<
|
|
231
|
+
export async function twoFactorPasskeyOptions(state: RequestState): Promise<Result<{ options: PublicKeyCredentialRequestOptionsJSON }, 'signed-out' | 'none' | 'expired' | 'off'>> {
|
|
243
232
|
const pending = await state.session.get('authChallenge')
|
|
244
233
|
const record = pending && pending.expires > Date.now() ? await state.core.store.users.get(pending.user) : undefined
|
|
245
234
|
|
|
@@ -247,7 +236,7 @@ export async function twoFactorPasskeyOptions(state: RequestState): Promise<Pass
|
|
|
247
236
|
}
|
|
248
237
|
|
|
249
238
|
// Answers a pending sign-in's second factor with a passkey, in place of an authenticator code.
|
|
250
|
-
export async function verifyTwoFactorPasskey(state: RequestState, response: AuthenticationResponseJSON, signIn: (record: UserRecord, method: string) => Promise<void>): Promise<
|
|
239
|
+
export async function verifyTwoFactorPasskey(state: RequestState, response: AuthenticationResponseJSON, signIn: (record: UserRecord, method: string) => Promise<void>): Promise<Result<{ redirect: string }, 'invalid' | 'expired' | 'limited'>> {
|
|
251
240
|
const { store } = state.core
|
|
252
241
|
const pending = await state.session.get('authChallenge')
|
|
253
242
|
const record = pending && pending.expires > Date.now() ? await store.users.get(pending.user) : undefined
|
|
@@ -279,7 +268,7 @@ export async function verifyTwoFactorPasskey(state: RequestState, response: Auth
|
|
|
279
268
|
}
|
|
280
269
|
|
|
281
270
|
// The options for re-proving the signed-in user with one of their passkeys, sent back as `{ passkey }` in a proof.
|
|
282
|
-
export async function passkeyProofOptions(state: RequestState): Promise<
|
|
271
|
+
export async function passkeyProofOptions(state: RequestState): Promise<Result<{ options: PublicKeyCredentialRequestOptionsJSON }, 'signed-out' | 'none' | 'expired' | 'off'>> {
|
|
283
272
|
const record = await state.current()
|
|
284
273
|
|
|
285
274
|
return !record || state.via() !== 'session' ? failed('signed-out') : userOptions(state, record, 'reprove')
|
|
@@ -305,7 +294,7 @@ export function relyingParty(core: Core): Relying {
|
|
|
305
294
|
return core.passkeys
|
|
306
295
|
}
|
|
307
296
|
|
|
308
|
-
async function userOptions(state: RequestState, record: UserRecord, purpose: 'second-factor' | 'reprove'): Promise<
|
|
297
|
+
async function userOptions(state: RequestState, record: UserRecord, purpose: 'second-factor' | 'reprove'): Promise<Result<{ options: PublicKeyCredentialRequestOptionsJSON }, 'signed-out' | 'none' | 'expired' | 'off'>> {
|
|
309
298
|
const passkeys = await state.core.store.passkeys.listByUser(record.id)
|
|
310
299
|
|
|
311
300
|
if (passkeys.length === 0)
|