@kayjs/auth 0.0.0-stage → 1.104.2
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/LICENSE +21 -0
- package/README.md +78 -2
- package/llms/admin-and-organizations.md +70 -0
- package/llms/admin-pages.md +30 -0
- package/llms/grouped-methods.md +106 -0
- package/llms/hardening-and-testing.md +108 -0
- package/llms/oauth-passkeys-two-factor.md +111 -0
- package/llms/oauth-providers.md +28 -0
- package/llms/oauth-server-and-tokens.md +76 -0
- package/llms/organization-pages.md +20 -0
- package/llms/passwords-and-mails.md +129 -0
- package/llms/sessions-and-guards.md +51 -0
- package/llms/setup.md +122 -0
- package/llms/signed-in-components.md +59 -0
- package/llms.txt +28 -0
- package/package.json +85 -4
- package/src/access.ts +31 -0
- package/src/account.ts +198 -0
- package/src/add.ts +70 -0
- package/src/address.ts +51 -0
- package/src/admin.ts +361 -0
- package/src/auditWording.ts +153 -0
- package/src/auth.ts +219 -0
- package/src/authorize/clients.ts +160 -0
- package/src/authorize/grants.ts +44 -0
- package/src/authorize/metadata.ts +72 -0
- package/src/authorize/register.ts +38 -0
- package/src/authorize/reply.ts +9 -0
- package/src/authorize/request.ts +253 -0
- package/src/authorize/resource.ts +34 -0
- package/src/authorize/revoke.ts +36 -0
- package/src/authorize/routes.ts +87 -0
- package/src/authorize/token.ts +213 -0
- package/src/authorize/uris.ts +76 -0
- package/src/bearer.ts +208 -0
- package/src/bearerHeader.ts +11 -0
- package/src/breached.ts +47 -0
- package/src/browser.ts +59 -0
- package/src/captcha.ts +196 -0
- package/src/cbor.ts +129 -0
- package/src/components/deletion-banner.kay +20 -0
- package/src/components/impersonation-banner.kay +21 -0
- package/src/components/signed-in.kay +8 -0
- package/src/components/signed-out.kay +6 -0
- package/src/components/user-menu.kay +21 -0
- package/src/context.ts +5 -0
- package/src/core.ts +308 -0
- package/src/createUserCommand.ts +217 -0
- package/src/decide.ts +45 -0
- package/src/deletion.ts +80 -0
- package/src/devTool.ts +104 -0
- package/src/device.ts +37 -0
- package/src/email.ts +594 -0
- package/src/erase.ts +237 -0
- package/src/events.ts +297 -0
- package/src/export.ts +101 -0
- package/src/failure.ts +63 -0
- package/src/forwardAuth.ts +55 -0
- package/src/forwardRoutes.ts +68 -0
- package/src/gate.ts +104 -0
- package/src/guard.ts +22 -0
- package/src/guards.ts +72 -0
- package/src/guesses.ts +99 -0
- package/src/index.ts +46 -0
- package/src/keys.ts +51 -0
- package/src/limits.ts +182 -0
- package/src/load.ts +18 -0
- package/src/locale.ts +15 -0
- package/src/log.ts +4 -0
- package/src/mail.ts +225 -0
- package/src/memoryStore.ts +599 -0
- package/src/messages/fr.ts +357 -0
- package/src/methods/account.ts +80 -0
- package/src/methods/admin.ts +63 -0
- package/src/methods/audit.ts +15 -0
- package/src/methods/email.ts +39 -0
- package/src/methods/magicLink.ts +32 -0
- package/src/methods/oauth.ts +35 -0
- package/src/methods/oauthServer.ts +31 -0
- package/src/methods/organizations.ts +96 -0
- package/src/methods/passkeys.ts +61 -0
- package/src/methods/password.ts +53 -0
- package/src/methods/sessions.ts +18 -0
- package/src/methods/tokens.ts +24 -0
- package/src/methods/twoFactor.ts +62 -0
- package/src/oauth/client.ts +324 -0
- package/src/oauth/flow.ts +350 -0
- package/src/oauth/index.ts +5 -0
- package/src/oauth/pkce.ts +20 -0
- package/src/oauth/providers.ts +490 -0
- package/src/oauth/refresher.ts +177 -0
- package/src/oauth/timeout.ts +2 -0
- package/src/oauth/types.ts +78 -0
- package/src/oauthTest.ts +112 -0
- package/src/organizations.ts +675 -0
- package/src/organizationsCore.ts +60 -0
- package/src/own.ts +313 -0
- package/src/pages/components/captcha.kay +16 -0
- package/src/pages/components/frame.kay +11 -0
- package/src/pages/components/message.kay +8 -0
- package/src/pages/components/panel.kay +12 -0
- package/src/pages/components/passkey-add.kay +48 -0
- package/src/pages/components/passkey-sign-in.kay +34 -0
- package/src/pages/components/passkey-two-factor.kay +33 -0
- package/src/pages/components/proof.kay +15 -0
- package/src/pages/components/provider-buttons.kay +25 -0
- package/src/pages/components/recovery-codes.kay +13 -0
- package/src/pages/components/separator.kay +4 -0
- package/src/pages/english.ts +82 -0
- package/src/pages/forms.ts +185 -0
- package/src/pages/index.ts +361 -0
- package/src/pages/organizationPage.ts +12 -0
- package/src/pages/routes/account/_guard.ts +15 -0
- package/src/pages/routes/account/_layout.kay +37 -0
- package/src/pages/routes/account/clients.kay +33 -0
- package/src/pages/routes/account/connections.kay +57 -0
- package/src/pages/routes/account/data.kay +106 -0
- package/src/pages/routes/account/index.kay +109 -0
- package/src/pages/routes/account/organizations/[org].kay +188 -0
- package/src/pages/routes/account/organizations/_guard.ts +8 -0
- package/src/pages/routes/account/organizations/index.kay +86 -0
- package/src/pages/routes/account/security.kay +164 -0
- package/src/pages/routes/account/sessions.kay +51 -0
- package/src/pages/routes/account/tokens.kay +84 -0
- package/src/pages/routes/admin/_layout.kay +43 -0
- package/src/pages/routes/admin/audit.kay +43 -0
- package/src/pages/routes/admin/index.kay +60 -0
- package/src/pages/routes/admin/users/[id].kay +181 -0
- package/src/pages/routes/confirm-email.kay +39 -0
- package/src/pages/routes/invitation.kay +104 -0
- package/src/pages/routes/magic-link.kay +42 -0
- package/src/pages/routes/oauth/authorize.kay +80 -0
- package/src/pages/routes/register.kay +83 -0
- package/src/pages/routes/reset-password.kay +73 -0
- package/src/pages/routes/sign-in.kay +128 -0
- package/src/pages/routes/two-factor.kay +61 -0
- package/src/pages/routes/verify-email.kay +83 -0
- package/src/passkeys.ts +373 -0
- package/src/password.ts +316 -0
- package/src/payloads.ts +22 -0
- package/src/pg.ts +705 -0
- package/src/profile.ts +43 -0
- package/src/proof.ts +110 -0
- package/src/prune.ts +123 -0
- package/src/rates.ts +82 -0
- package/src/request.ts +712 -0
- package/src/requiredTwoFactor.ts +71 -0
- package/src/requirement.ts +23 -0
- package/src/roles.ts +18 -0
- package/src/routes.ts +202 -0
- package/src/rules-check.ts +37 -0
- package/src/rules.ts +108 -0
- package/src/securityNotice.ts +45 -0
- package/src/sessions.ts +184 -0
- package/src/setup.ts +417 -0
- package/src/signInOptions.ts +9 -0
- package/src/state.ts +34 -0
- package/src/store.ts +410 -0
- package/src/storeHelpers.ts +23 -0
- package/src/test.ts +46 -0
- package/src/testCookies.ts +9 -0
- package/src/testHelpers.ts +58 -0
- package/src/testing.ts +113 -0
- package/src/text.ts +6 -0
- package/src/totp.ts +120 -0
- package/src/twoFactor.ts +200 -0
- package/src/types.ts +23 -0
- package/src/userData.ts +47 -0
- package/src/viewer-layout/_layout.kay +11 -0
- package/src/viewer.ts +116 -0
- package/src/wayIn.ts +40 -0
- package/src/webauthn.ts +375 -0
- package/src/wsAuth.ts +53 -0
- package/types/access.d.ts +12 -0
- package/types/access.d.ts.map +1 -0
- package/types/account.d.ts +32 -0
- package/types/account.d.ts.map +1 -0
- package/types/add.d.ts +29 -0
- package/types/add.d.ts.map +1 -0
- package/types/address.d.ts +2 -0
- package/types/address.d.ts.map +1 -0
- package/types/admin.d.ts +84 -0
- package/types/admin.d.ts.map +1 -0
- package/types/auditWording.d.ts +9 -0
- package/types/auditWording.d.ts.map +1 -0
- package/types/auth.d.ts +3475 -0
- package/types/auth.d.ts.map +1 -0
- package/types/authorize/clients.d.ts +15 -0
- package/types/authorize/clients.d.ts.map +1 -0
- package/types/authorize/grants.d.ts +15 -0
- package/types/authorize/grants.d.ts.map +1 -0
- package/types/authorize/metadata.d.ts +20 -0
- package/types/authorize/metadata.d.ts.map +1 -0
- package/types/authorize/register.d.ts +4 -0
- package/types/authorize/register.d.ts.map +1 -0
- package/types/authorize/reply.d.ts +6 -0
- package/types/authorize/reply.d.ts.map +1 -0
- package/types/authorize/request.d.ts +48 -0
- package/types/authorize/request.d.ts.map +1 -0
- package/types/authorize/resource.d.ts +4 -0
- package/types/authorize/resource.d.ts.map +1 -0
- package/types/authorize/revoke.d.ts +4 -0
- package/types/authorize/revoke.d.ts.map +1 -0
- package/types/authorize/routes.d.ts +5 -0
- package/types/authorize/routes.d.ts.map +1 -0
- package/types/authorize/token.d.ts +7 -0
- package/types/authorize/token.d.ts.map +1 -0
- package/types/authorize/uris.d.ts +5 -0
- package/types/authorize/uris.d.ts.map +1 -0
- package/types/bearer.d.ts +45 -0
- package/types/bearer.d.ts.map +1 -0
- package/types/bearerHeader.d.ts +3 -0
- package/types/bearerHeader.d.ts.map +1 -0
- package/types/breached.d.ts +5 -0
- package/types/breached.d.ts.map +1 -0
- package/types/browser.d.ts +11 -0
- package/types/browser.d.ts.map +1 -0
- package/types/captcha.d.ts +42 -0
- package/types/captcha.d.ts.map +1 -0
- package/types/cbor.d.ts +4 -0
- package/types/cbor.d.ts.map +1 -0
- package/types/context.d.ts +3 -0
- package/types/context.d.ts.map +1 -0
- package/types/core.d.ts +180 -0
- package/types/core.d.ts.map +1 -0
- package/types/createUserCommand.d.ts +11 -0
- package/types/createUserCommand.d.ts.map +1 -0
- package/types/decide.d.ts +12 -0
- package/types/decide.d.ts.map +1 -0
- package/types/deletion.d.ts +17 -0
- package/types/deletion.d.ts.map +1 -0
- package/types/devTool.d.ts +3 -0
- package/types/devTool.d.ts.map +1 -0
- package/types/device.d.ts +7 -0
- package/types/device.d.ts.map +1 -0
- package/types/email.d.ts +73 -0
- package/types/email.d.ts.map +1 -0
- package/types/erase.d.ts +31 -0
- package/types/erase.d.ts.map +1 -0
- package/types/events.d.ts +139 -0
- package/types/events.d.ts.map +1 -0
- package/types/export.d.ts +54 -0
- package/types/export.d.ts.map +1 -0
- package/types/failure.d.ts +19 -0
- package/types/failure.d.ts.map +1 -0
- package/types/forwardAuth.d.ts +12 -0
- package/types/forwardAuth.d.ts.map +1 -0
- package/types/forwardRoutes.d.ts +11 -0
- package/types/forwardRoutes.d.ts.map +1 -0
- package/types/gate.d.ts +31 -0
- package/types/gate.d.ts.map +1 -0
- package/types/guard.d.ts +10 -0
- package/types/guard.d.ts.map +1 -0
- package/types/guards.d.ts +37 -0
- package/types/guards.d.ts.map +1 -0
- package/types/guesses.d.ts +18 -0
- package/types/guesses.d.ts.map +1 -0
- package/types/index.d.ts +47 -0
- package/types/index.d.ts.map +1 -0
- package/types/keys.d.ts +11 -0
- package/types/keys.d.ts.map +1 -0
- package/types/limits.d.ts +133 -0
- package/types/limits.d.ts.map +1 -0
- package/types/load.d.ts +18 -0
- package/types/load.d.ts.map +1 -0
- package/types/locale.d.ts +4 -0
- package/types/locale.d.ts.map +1 -0
- package/types/log.d.ts +2 -0
- package/types/log.d.ts.map +1 -0
- package/types/mail.d.ts +51 -0
- package/types/mail.d.ts.map +1 -0
- package/types/memoryStore.d.ts +3 -0
- package/types/memoryStore.d.ts.map +1 -0
- package/types/messages/fr.d.ts +409 -0
- package/types/messages/fr.d.ts.map +1 -0
- package/types/methods/account.d.ts +24 -0
- package/types/methods/account.d.ts.map +1 -0
- package/types/methods/admin.d.ts +30 -0
- package/types/methods/admin.d.ts.map +1 -0
- package/types/methods/audit.d.ts +11 -0
- package/types/methods/audit.d.ts.map +1 -0
- package/types/methods/email.d.ts +14 -0
- package/types/methods/email.d.ts.map +1 -0
- package/types/methods/magicLink.d.ts +12 -0
- package/types/methods/magicLink.d.ts.map +1 -0
- package/types/methods/oauth.d.ts +16 -0
- package/types/methods/oauth.d.ts.map +1 -0
- package/types/methods/oauthServer.d.ts +13 -0
- package/types/methods/oauthServer.d.ts.map +1 -0
- package/types/methods/organizations.d.ts +30 -0
- package/types/methods/organizations.d.ts.map +1 -0
- package/types/methods/passkeys.d.ts +19 -0
- package/types/methods/passkeys.d.ts.map +1 -0
- package/types/methods/password.d.ts +17 -0
- package/types/methods/password.d.ts.map +1 -0
- package/types/methods/sessions.d.ts +9 -0
- package/types/methods/sessions.d.ts.map +1 -0
- package/types/methods/tokens.d.ts +11 -0
- package/types/methods/tokens.d.ts.map +1 -0
- package/types/methods/twoFactor.d.ts +20 -0
- package/types/methods/twoFactor.d.ts.map +1 -0
- package/types/oauth/client.d.ts +39 -0
- package/types/oauth/client.d.ts.map +1 -0
- package/types/oauth/flow.d.ts +38 -0
- package/types/oauth/flow.d.ts.map +1 -0
- package/types/oauth/index.d.ts +6 -0
- package/types/oauth/index.d.ts.map +1 -0
- package/types/oauth/pkce.d.ts +10 -0
- package/types/oauth/pkce.d.ts.map +1 -0
- package/types/oauth/providers.d.ts +100 -0
- package/types/oauth/providers.d.ts.map +1 -0
- package/types/oauth/refresher.d.ts +55 -0
- package/types/oauth/refresher.d.ts.map +1 -0
- package/types/oauth/timeout.d.ts +2 -0
- package/types/oauth/timeout.d.ts.map +1 -0
- package/types/oauth/types.d.ts +75 -0
- package/types/oauth/types.d.ts.map +1 -0
- package/types/oauthTest.d.ts +23 -0
- package/types/oauthTest.d.ts.map +1 -0
- package/types/organizations.d.ts +111 -0
- package/types/organizations.d.ts.map +1 -0
- package/types/organizationsCore.d.ts +29 -0
- package/types/organizationsCore.d.ts.map +1 -0
- package/types/own.d.ts +55 -0
- package/types/own.d.ts.map +1 -0
- package/types/pages/english.d.ts +80 -0
- package/types/pages/english.d.ts.map +1 -0
- package/types/pages/forms.d.ts +204 -0
- package/types/pages/forms.d.ts.map +1 -0
- package/types/pages/index.d.ts +166 -0
- package/types/pages/index.d.ts.map +1 -0
- package/types/pages/organizationPage.d.ts +7 -0
- package/types/pages/organizationPage.d.ts.map +1 -0
- package/types/pages/routes/account/_guard.d.ts +3 -0
- package/types/pages/routes/account/_guard.d.ts.map +1 -0
- package/types/pages/routes/account/organizations/_guard.d.ts +3 -0
- package/types/pages/routes/account/organizations/_guard.d.ts.map +1 -0
- package/types/passkeys.d.ts +61 -0
- package/types/passkeys.d.ts.map +1 -0
- package/types/password.d.ts +49 -0
- package/types/password.d.ts.map +1 -0
- package/types/payloads.d.ts +4 -0
- package/types/payloads.d.ts.map +1 -0
- package/types/pg.d.ts +303 -0
- package/types/pg.d.ts.map +1 -0
- package/types/profile.d.ts +11 -0
- package/types/profile.d.ts.map +1 -0
- package/types/proof.d.ts +33 -0
- package/types/proof.d.ts.map +1 -0
- package/types/prune.d.ts +5 -0
- package/types/prune.d.ts.map +1 -0
- package/types/rates.d.ts +15 -0
- package/types/rates.d.ts.map +1 -0
- package/types/request.d.ts +119 -0
- package/types/request.d.ts.map +1 -0
- package/types/requiredTwoFactor.d.ts +16 -0
- package/types/requiredTwoFactor.d.ts.map +1 -0
- package/types/requirement.d.ts +4 -0
- package/types/requirement.d.ts.map +1 -0
- package/types/roles.d.ts +9 -0
- package/types/roles.d.ts.map +1 -0
- package/types/routes.d.ts +31 -0
- package/types/routes.d.ts.map +1 -0
- package/types/rules-check.d.ts +3 -0
- package/types/rules-check.d.ts.map +1 -0
- package/types/rules.d.ts +38 -0
- package/types/rules.d.ts.map +1 -0
- package/types/securityNotice.d.ts +7 -0
- package/types/securityNotice.d.ts.map +1 -0
- package/types/sessions.d.ts +32 -0
- package/types/sessions.d.ts.map +1 -0
- package/types/setup.d.ts +18 -0
- package/types/setup.d.ts.map +1 -0
- package/types/signInOptions.d.ts +6 -0
- package/types/signInOptions.d.ts.map +1 -0
- package/types/state.d.ts +25 -0
- package/types/state.d.ts.map +1 -0
- package/types/store.d.ts +295 -0
- package/types/store.d.ts.map +1 -0
- package/types/storeHelpers.d.ts +4 -0
- package/types/storeHelpers.d.ts.map +1 -0
- package/types/test.d.ts +21 -0
- package/types/test.d.ts.map +1 -0
- package/types/testCookies.d.ts +3 -0
- package/types/testCookies.d.ts.map +1 -0
- package/types/testHelpers.d.ts +34 -0
- package/types/testHelpers.d.ts.map +1 -0
- package/types/testing.d.ts +12 -0
- package/types/testing.d.ts.map +1 -0
- package/types/text.d.ts +2 -0
- package/types/text.d.ts.map +1 -0
- package/types/totp.d.ts +12 -0
- package/types/totp.d.ts.map +1 -0
- package/types/twoFactor.d.ts +41 -0
- package/types/twoFactor.d.ts.map +1 -0
- package/types/types.d.ts +17 -0
- package/types/types.d.ts.map +1 -0
- package/types/userData.d.ts +9 -0
- package/types/userData.d.ts.map +1 -0
- package/types/viewer.d.ts +23 -0
- package/types/viewer.d.ts.map +1 -0
- package/types/wayIn.d.ts +12 -0
- package/types/wayIn.d.ts.map +1 -0
- package/types/webauthn.d.ts +111 -0
- package/types/webauthn.d.ts.map +1 -0
- package/types/wsAuth.d.ts +14 -0
- package/types/wsAuth.d.ts.map +1 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
--- server
|
|
2
|
+
import { action } from 'kay'
|
|
3
|
+
import { localize } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import Message from '../components/message.kay'
|
|
6
|
+
import Panel from '../components/panel.kay'
|
|
7
|
+
import { failure, schemas } from '../forms'
|
|
8
|
+
import Button from '@kayjs/ui/button.kay'
|
|
9
|
+
import FormField from '@kayjs/ui/form-field.kay'
|
|
10
|
+
|
|
11
|
+
// Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep everyone out.
|
|
12
|
+
// The link's token sits in the address, which no Referer header may carry to another site; `same-origin` still lets a form post carry its Origin.
|
|
13
|
+
export const page = { guards: false, security: { headers: { 'referrer-policy': 'same-origin' } } }
|
|
14
|
+
|
|
15
|
+
// The link only shows this button, so a mail scanner opening it spends nothing.
|
|
16
|
+
export const verify = action(schemas.verify, async ({ token, password }, ctx) => {
|
|
17
|
+
const result = await ctx.locals.auth.email.verify(token, password)
|
|
18
|
+
|
|
19
|
+
if (!result.ok)
|
|
20
|
+
return ctx.fail(400, { error: result.error.code === 'invalid' ? 'link' : result.error.code })
|
|
21
|
+
|
|
22
|
+
return ctx.redirect(localize(result.redirect))
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export const code = action(schemas.emailCode, async ({ code }, ctx) => {
|
|
26
|
+
const result = await ctx.locals.auth.email.verifyCode(code)
|
|
27
|
+
|
|
28
|
+
if (!result.ok)
|
|
29
|
+
return ctx.fail(400, { error: result.error.code === 'invalid' ? 'code' : result.error.code })
|
|
30
|
+
|
|
31
|
+
return ctx.redirect(localize(result.redirect))
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
export const resend = action(async (ctx) => {
|
|
35
|
+
const result = await ctx.locals.auth.email.sendVerification()
|
|
36
|
+
|
|
37
|
+
return result.ok ? 'sent' : ctx.fail(400, { error: result.error.code })
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const auth = ctx.locals.auth
|
|
41
|
+
const token = ctx.url.searchParams.get('token') ?? verify.failed()?.values.token
|
|
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.succeeded() !== undefined
|
|
47
|
+
const landmark = ctx.locals.auth.features().pages?.main !== false
|
|
48
|
+
---
|
|
49
|
+
head
|
|
50
|
+
title {wording('auth.verify.title', 'Verify your address')}
|
|
51
|
+
Panel(landmark={landmark} title={wording('auth.verify.title', 'Verify your address')})
|
|
52
|
+
if token
|
|
53
|
+
form.flex.flex-col.gap-3(action={verify})
|
|
54
|
+
input(type="hidden" name="token" value={token})
|
|
55
|
+
if refused === 'confirm'
|
|
56
|
+
p {wording('auth.verify.confirm', 'This link was opened in another browser: type your password to confirm it is you.')}
|
|
57
|
+
FormField(label={wording('auth.password', 'Password')} required)
|
|
58
|
+
input(name="password" type="password" autocomplete="current-password" required)
|
|
59
|
+
else
|
|
60
|
+
p {wording('auth.verify.intro', 'Press the button to verify your address.')}
|
|
61
|
+
if refused
|
|
62
|
+
Message(text={failure(refused)})
|
|
63
|
+
Button(type="submit" tone="accent" label={wording('auth.verify.submit', 'Verify my address')})
|
|
64
|
+
else if pending?.type === 'verify'
|
|
65
|
+
form.flex.flex-col.gap-3(action={code})
|
|
66
|
+
p {wording('auth.verify.codeIntro', 'Type the code mailed to {email}.', { email: pending.email })}
|
|
67
|
+
FormField(label={wording('auth.code', 'Code')} required)
|
|
68
|
+
input(name="code" inputmode="numeric" autocomplete="one-time-code" required)
|
|
69
|
+
if codeError
|
|
70
|
+
Message(text={failure(codeError)})
|
|
71
|
+
Button(type="submit" tone="accent" label={wording('auth.verify.submit', 'Verify my address')})
|
|
72
|
+
else if user?.email && !user.emailVerified
|
|
73
|
+
form.flex.flex-col.gap-3(action={resend})
|
|
74
|
+
p {wording('auth.verify.unverified', 'Your address {email} is not verified yet.', { email: user.email })}
|
|
75
|
+
if resent
|
|
76
|
+
Message(tone="success" text={wording('auth.verify.resent', 'A new link is on its way.')})
|
|
77
|
+
if resendError
|
|
78
|
+
Message(text={failure(resendError)})
|
|
79
|
+
Button(type="submit" label={wording('auth.verify.resend', 'Mail me a new link')})
|
|
80
|
+
else if user?.emailVerified
|
|
81
|
+
Message(tone="success" text={wording('auth.verify.done', 'Your address is verified.')})
|
|
82
|
+
else
|
|
83
|
+
Message(text={failure('link')})
|
package/src/passkeys.ts
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import type { Core } from './core'
|
|
2
|
+
import type { AuthFailed } from './failure'
|
|
3
|
+
import type { Proof, ProofFailure } from './proof'
|
|
4
|
+
import type { RequestState } from './state'
|
|
5
|
+
import type { PasskeyRecord, UserRecord } from './store'
|
|
6
|
+
import type { TwoFactorResult } from './twoFactor'
|
|
7
|
+
import type { AuthenticationResponseJSON, PublicKeyCredentialCreationOptionsJSON, PublicKeyCredentialRequestOptionsJSON, RegistrationResponseJSON } from './webauthn'
|
|
8
|
+
import { Buffer } from 'node:buffer'
|
|
9
|
+
import { localPath } from 'kay'
|
|
10
|
+
import { configError } from 'kay/library'
|
|
11
|
+
import { banned, homeOf } from './core'
|
|
12
|
+
import { readDevice } from './device'
|
|
13
|
+
import { report } from './events'
|
|
14
|
+
import { failed } from './failure'
|
|
15
|
+
import { secondFactorLimited } from './guesses'
|
|
16
|
+
import { forget, spend, windowOf } from './limits'
|
|
17
|
+
import { reprove } from './proof'
|
|
18
|
+
import { securityNotice } from './securityNotice'
|
|
19
|
+
import { endOtherSessions } from './sessions'
|
|
20
|
+
import { plainName } from './text'
|
|
21
|
+
import { hasWayIn, putBack } from './wayIn'
|
|
22
|
+
import { authenticationOptions, registrationOptions, verifyAuthentication, verifyRegistration } from './webauthn'
|
|
23
|
+
|
|
24
|
+
const CHALLENGE_MAX_AGE_MS = 5 * 60 * 1000
|
|
25
|
+
const NAME_MAX_LENGTH = 64
|
|
26
|
+
|
|
27
|
+
// Who passkeys are made for: the site's host as the WebAuthn relying party, and the origins its pages are served from.
|
|
28
|
+
export interface Relying {
|
|
29
|
+
id: string
|
|
30
|
+
name: string
|
|
31
|
+
origins: string[]
|
|
32
|
+
// How hard registering a passkey and answering a second factor with one ask for the user to be verified, by a PIN or a biometric.
|
|
33
|
+
userVerification: 'required' | 'preferred' | 'discouraged'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// What a passkey ceremony asks for: a new passkey, a sign-in, the second factor of one, or a re-proof.
|
|
37
|
+
export type PasskeyPurpose = 'register' | 'sign-in' | 'second-factor' | 'reprove'
|
|
38
|
+
|
|
39
|
+
// The challenge a ceremony in progress waits to see signed, kept in the session and taken once.
|
|
40
|
+
export interface PasskeyChallenge {
|
|
41
|
+
purpose: PasskeyPurpose
|
|
42
|
+
challenge: string
|
|
43
|
+
// The user it was made for; null for a sign-in, where the passkey names them.
|
|
44
|
+
user: string | null
|
|
45
|
+
expires: number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// A passkey as the app lists it, its key left out.
|
|
49
|
+
export interface Passkey {
|
|
50
|
+
id: string
|
|
51
|
+
name: string
|
|
52
|
+
// Whether it syncs between devices, as one in a password manager does.
|
|
53
|
+
multiDevice: boolean
|
|
54
|
+
backedUp: boolean
|
|
55
|
+
// Whether it has proved user verification, as signing in requires. One that has not, such as one made before passkeys always asked for it, is no way in: replace it by adding a new passkey and removing this one.
|
|
56
|
+
userVerified: boolean
|
|
57
|
+
createdAt: Date
|
|
58
|
+
lastUsedAt: Date | null
|
|
59
|
+
}
|
|
60
|
+
|
|
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
|
+
// The options for the browser's `navigator.credentials.create`, through `makePasskey` of `@kayjs/auth/browser`, once `proof` re-proves the user.
|
|
68
|
+
// 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<PasskeyOptionsResult<PublicKeyCredentialCreationOptionsJSON, ProofFailure>> {
|
|
70
|
+
const relying = relyingParty(state.core)
|
|
71
|
+
|
|
72
|
+
if (!(await state.current()) || state.via() !== 'session')
|
|
73
|
+
return failed('signed-out')
|
|
74
|
+
|
|
75
|
+
const proved = await reprove(state, proof, { secondFactor: true })
|
|
76
|
+
|
|
77
|
+
if (!proved.ok)
|
|
78
|
+
return proved
|
|
79
|
+
|
|
80
|
+
const { record } = proved
|
|
81
|
+
|
|
82
|
+
const options = registrationOptions({
|
|
83
|
+
rpName: relying.name,
|
|
84
|
+
rpId: relying.id,
|
|
85
|
+
userName: record.email ?? record.username ?? record.id,
|
|
86
|
+
userId: record.id,
|
|
87
|
+
exclude: (await state.core.store.passkeys.listByUser(record.id)).map(passkey => ({ id: passkey.id, transports: passkey.transports })),
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
await park(state, 'register', options.challenge, record.id)
|
|
91
|
+
|
|
92
|
+
return { ok: true, options }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 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<RegisterPasskeyResult> {
|
|
97
|
+
const { core } = state
|
|
98
|
+
const relying = relyingParty(core)
|
|
99
|
+
const record = await state.current()
|
|
100
|
+
|
|
101
|
+
if (!record || state.via() !== 'session')
|
|
102
|
+
return failed('signed-out')
|
|
103
|
+
|
|
104
|
+
const challenge = await take(state, 'register', record.id)
|
|
105
|
+
|
|
106
|
+
if (challenge === undefined)
|
|
107
|
+
return failed('expired')
|
|
108
|
+
|
|
109
|
+
const credential = await verifyRegistration(response, { challenge, origins: relying.origins, rpId: relying.id, requireUserVerification: true })
|
|
110
|
+
|
|
111
|
+
if (!credential || await core.store.passkeys.get(credential.id))
|
|
112
|
+
return failed('invalid')
|
|
113
|
+
|
|
114
|
+
const passkey: PasskeyRecord = {
|
|
115
|
+
id: credential.id,
|
|
116
|
+
userId: record.id,
|
|
117
|
+
name: cleanName(name) ?? deviceName(state.userAgent),
|
|
118
|
+
publicKey: credential.publicKey,
|
|
119
|
+
counter: credential.counter,
|
|
120
|
+
transports: credential.transports,
|
|
121
|
+
multiDevice: credential.multiDevice,
|
|
122
|
+
backedUp: credential.backedUp,
|
|
123
|
+
userVerified: true,
|
|
124
|
+
createdAt: new Date(),
|
|
125
|
+
lastUsedAt: null,
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await core.store.passkeys.create(passkey)
|
|
129
|
+
await securityNotice(state, record, 'passkey-added', passkey.name)
|
|
130
|
+
|
|
131
|
+
return { ok: true, passkey: publicPasskey(passkey) }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function listPasskeys(state: RequestState): Promise<Passkey[]> {
|
|
135
|
+
const record = await state.current()
|
|
136
|
+
|
|
137
|
+
return record ? (await state.core.store.passkeys.listByUser(record.id)).map(passkey => publicPasskey(passkey)) : []
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export type RenamePasskeyResult = { ok: true } | AuthFailed<'signed-out' | 'not-found' | 'invalid-name'>
|
|
141
|
+
|
|
142
|
+
export async function renamePasskey(state: RequestState, id: string, name: string): Promise<RenamePasskeyResult> {
|
|
143
|
+
const record = await state.current()
|
|
144
|
+
|
|
145
|
+
if (!record || state.via() !== 'session')
|
|
146
|
+
return failed('signed-out')
|
|
147
|
+
|
|
148
|
+
const passkey = await state.core.store.passkeys.get(id)
|
|
149
|
+
const cleaned = cleanName(name)
|
|
150
|
+
|
|
151
|
+
if (passkey?.userId !== record.id)
|
|
152
|
+
return failed('not-found')
|
|
153
|
+
|
|
154
|
+
if (cleaned === undefined)
|
|
155
|
+
return failed('invalid-name')
|
|
156
|
+
|
|
157
|
+
await state.core.store.passkeys.update(id, { name: cleaned })
|
|
158
|
+
|
|
159
|
+
return { ok: true }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export type RevokePasskeyResult = { ok: true } | AuthFailed<ProofFailure | 'not-found' | 'last-way-in'>
|
|
163
|
+
|
|
164
|
+
// 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<RevokePasskeyResult> {
|
|
166
|
+
const { core } = state
|
|
167
|
+
const proved = await reprove(state, proof)
|
|
168
|
+
|
|
169
|
+
if (!proved.ok)
|
|
170
|
+
return proved
|
|
171
|
+
|
|
172
|
+
const { record } = proved
|
|
173
|
+
const passkeys = await core.store.passkeys.listByUser(record.id)
|
|
174
|
+
const passkey = passkeys.find(entry => entry.id === id)
|
|
175
|
+
|
|
176
|
+
if (!passkey)
|
|
177
|
+
return failed('not-found')
|
|
178
|
+
|
|
179
|
+
const removed = { passkey }
|
|
180
|
+
|
|
181
|
+
if (!await hasWayIn(core, record.id, removed))
|
|
182
|
+
return failed('last-way-in')
|
|
183
|
+
|
|
184
|
+
await core.store.passkeys.delete(record.id, id)
|
|
185
|
+
|
|
186
|
+
// Two requests can each see the other way in still there: when none is left, the passkey goes back.
|
|
187
|
+
if (!await hasWayIn(core, record.id)) {
|
|
188
|
+
await putBack(core, record.id, async () => core.store.passkeys.create(passkey))
|
|
189
|
+
|
|
190
|
+
return failed('last-way-in')
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
await endOtherSessions(state, record)
|
|
194
|
+
await securityNotice(state, record, 'passkey-removed', passkey.name)
|
|
195
|
+
|
|
196
|
+
return { ok: true }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// The options for `navigator.credentials.get` naming no account, so the browser offers every passkey it holds for the site, in its autofill too with `useBrowserAutofill`.
|
|
200
|
+
export async function passkeySignInOptions(state: RequestState): Promise<PublicKeyCredentialRequestOptionsJSON> {
|
|
201
|
+
const options = authenticationOptions({ rpId: relyingParty(state.core).id, userVerification: 'required' })
|
|
202
|
+
|
|
203
|
+
await park(state, 'sign-in', options.challenge, null)
|
|
204
|
+
|
|
205
|
+
return options
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 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<PasskeySignInResult> {
|
|
210
|
+
const { core } = state
|
|
211
|
+
|
|
212
|
+
if (await spend(core.rates, `passkey:${state.client}`, core.limits.signIn))
|
|
213
|
+
return failed('limited')
|
|
214
|
+
|
|
215
|
+
const challenge = await take(state, 'sign-in', null)
|
|
216
|
+
|
|
217
|
+
if (challenge === undefined)
|
|
218
|
+
return failed('expired')
|
|
219
|
+
|
|
220
|
+
const passkey = await core.store.passkeys.get(response.id)
|
|
221
|
+
const record = passkey && await core.store.users.get(passkey.userId)
|
|
222
|
+
|
|
223
|
+
// The authenticator names the account it made the passkey for, which must be the one holding it.
|
|
224
|
+
if (!passkey || !record || (response.response.userHandle !== undefined && response.response.userHandle !== userHandle(record.id)))
|
|
225
|
+
return failed('invalid')
|
|
226
|
+
|
|
227
|
+
if (!(await check(core, passkey, response, challenge, true))) {
|
|
228
|
+
await report(state, record, 'sign-in-failed', 'passkey')
|
|
229
|
+
|
|
230
|
+
return failed('invalid')
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (banned(record))
|
|
234
|
+
return failed('banned')
|
|
235
|
+
|
|
236
|
+
await signIn(record, 'passkey')
|
|
237
|
+
|
|
238
|
+
return { ok: true, redirect: localPath(redirect, homeOf(core)) }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 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<PasskeyOptionsResult<PublicKeyCredentialRequestOptionsJSON>> {
|
|
243
|
+
const pending = await state.session.get('authChallenge')
|
|
244
|
+
const record = pending && pending.expires > Date.now() ? await state.core.store.users.get(pending.user) : undefined
|
|
245
|
+
|
|
246
|
+
return !record ? failed('expired') : userOptions(state, record, 'second-factor')
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// 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<TwoFactorResult> {
|
|
251
|
+
const { store } = state.core
|
|
252
|
+
const pending = await state.session.get('authChallenge')
|
|
253
|
+
const record = pending && pending.expires > Date.now() ? await store.users.get(pending.user) : undefined
|
|
254
|
+
|
|
255
|
+
if (!pending || !record || record.sessionVersion !== pending.version)
|
|
256
|
+
return failed('expired')
|
|
257
|
+
|
|
258
|
+
if (await secondFactorLimited(state, record))
|
|
259
|
+
return failed('limited')
|
|
260
|
+
|
|
261
|
+
const challenge = await take(state, 'second-factor', record.id)
|
|
262
|
+
|
|
263
|
+
if (challenge === undefined)
|
|
264
|
+
return failed('expired')
|
|
265
|
+
|
|
266
|
+
const passkey = await store.passkeys.get(response.id)
|
|
267
|
+
|
|
268
|
+
if (passkey?.userId !== record.id || !(await check(state.core, passkey, response, challenge, relyingParty(state.core).userVerification === 'required'))) {
|
|
269
|
+
await report(state, record, 'sign-in-failed', 'two-factor')
|
|
270
|
+
|
|
271
|
+
return failed('invalid')
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
await forget(state.core.rates, `two-factor:${record.id}`, state.core.limits.twoFactorAccount)
|
|
275
|
+
await state.session.unset('authChallenge')
|
|
276
|
+
await signIn(record, pending.method ?? 'unknown')
|
|
277
|
+
|
|
278
|
+
return { ok: true, redirect: pending.redirect }
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// 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<PasskeyOptionsResult<PublicKeyCredentialRequestOptionsJSON>> {
|
|
283
|
+
const record = await state.current()
|
|
284
|
+
|
|
285
|
+
return !record || state.via() !== 'session' ? failed('signed-out') : userOptions(state, record, 'reprove')
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Whether `response` re-proves `record` with one of their passkeys, against the challenge `passkeys.proofOptions` parked.
|
|
289
|
+
export async function provePasskey(state: RequestState, record: UserRecord, response: AuthenticationResponseJSON): Promise<boolean> {
|
|
290
|
+
const challenge = await take(state, 'reprove', record.id)
|
|
291
|
+
const passkey = challenge === undefined ? undefined : await state.core.store.passkeys.get(response.id)
|
|
292
|
+
|
|
293
|
+
return passkey?.userId === record.id && await check(state.core, passkey, response, challenge!, true)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// The WebAuthn user handle a passkey of `userId` carries, base64url.
|
|
297
|
+
export function userHandle(userId: string): string {
|
|
298
|
+
return Buffer.from(userId).toString('base64url')
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function relyingParty(core: Core): Relying {
|
|
302
|
+
if (!core.passkeys)
|
|
303
|
+
throw configError('auth', 'passkeys', 'passkeys need `url`, the site\'s public origin, which names who they are made for')
|
|
304
|
+
|
|
305
|
+
return core.passkeys
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async function userOptions(state: RequestState, record: UserRecord, purpose: 'second-factor' | 'reprove'): Promise<PasskeyOptionsResult<PublicKeyCredentialRequestOptionsJSON>> {
|
|
309
|
+
const passkeys = await state.core.store.passkeys.listByUser(record.id)
|
|
310
|
+
|
|
311
|
+
if (passkeys.length === 0)
|
|
312
|
+
return failed('none')
|
|
313
|
+
|
|
314
|
+
const options = authenticationOptions({
|
|
315
|
+
rpId: relyingParty(state.core).id,
|
|
316
|
+
allow: passkeys.map(passkey => ({ id: passkey.id, transports: passkey.transports })),
|
|
317
|
+
userVerification: purpose === 'reprove' ? 'required' : relyingParty(state.core).userVerification,
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
await park(state, purpose, options.challenge, record.id)
|
|
321
|
+
|
|
322
|
+
return { ok: true, options }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async function park(state: RequestState, purpose: PasskeyPurpose, challenge: string, user: string | null): Promise<void> {
|
|
326
|
+
await state.session.set('authPasskey', { purpose, challenge, user, expires: Date.now() + CHALLENGE_MAX_AGE_MS })
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// The session's challenge for `purpose` and `user`, spent whatever happens next. The store counts its uses too, so two requests racing on one copy of the session cannot both spend it.
|
|
330
|
+
async function take(state: RequestState, purpose: PasskeyPurpose, user: string | null): Promise<string | undefined> {
|
|
331
|
+
const parked = await state.session.get('authPasskey')
|
|
332
|
+
|
|
333
|
+
if (parked)
|
|
334
|
+
await state.session.unset('authPasskey')
|
|
335
|
+
|
|
336
|
+
if (!parked || parked.purpose !== purpose || parked.user !== user || parked.expires < Date.now())
|
|
337
|
+
return undefined
|
|
338
|
+
|
|
339
|
+
return (await state.core.rates(windowOf(CHALLENGE_MAX_AGE_MS)).hit(`passkey-challenge:${parked.challenge}`)).count === 1 ? parked.challenge : undefined
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Whether `response` is `passkey` signing `challenge`; its counter and last use are recorded. A counter that did not move past the stored one, the sign of a cloned authenticator, is refused.
|
|
343
|
+
async function check(core: Core, passkey: PasskeyRecord, response: AuthenticationResponseJSON, challenge: string, requireUserVerification: boolean): Promise<boolean> {
|
|
344
|
+
const relying = relyingParty(core)
|
|
345
|
+
const asserted = await verifyAuthentication(response, { challenge, origins: relying.origins, rpId: relying.id, requireUserVerification }, passkey)
|
|
346
|
+
|
|
347
|
+
if (!asserted)
|
|
348
|
+
return false
|
|
349
|
+
|
|
350
|
+
await core.store.passkeys.update(passkey.id, { counter: asserted.counter, backedUp: asserted.backedUp, ...asserted.userVerified && !passkey.userVerified && { userVerified: true }, lastUsedAt: new Date() })
|
|
351
|
+
|
|
352
|
+
return true
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function cleanName(name: string | undefined): string | undefined {
|
|
356
|
+
const cleaned = typeof name === 'string' ? plainName(name).slice(0, NAME_MAX_LENGTH) : undefined
|
|
357
|
+
|
|
358
|
+
return cleaned === undefined || cleaned === '' ? undefined : cleaned
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// `Firefox on Windows`, or `Passkey` for a browser it cannot read.
|
|
362
|
+
function deviceName(userAgent: string): string {
|
|
363
|
+
const device = readDevice(userAgent)
|
|
364
|
+
|
|
365
|
+
if (device.browser === null)
|
|
366
|
+
return device.os ?? 'Passkey'
|
|
367
|
+
|
|
368
|
+
return device.os === null ? device.browser : `${device.browser} on ${device.os}`
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function publicPasskey(passkey: PasskeyRecord): Passkey {
|
|
372
|
+
return { id: passkey.id, name: passkey.name, multiDevice: passkey.multiDevice, backedUp: passkey.backedUp, userVerified: passkey.userVerified, createdAt: passkey.createdAt, lastUsedAt: passkey.lastUsedAt }
|
|
373
|
+
}
|