@kayjs/auth 0.0.0-stage → 1.104.1
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,82 @@
|
|
|
1
|
+
// The English of every code the pages word, which `failure`, `issue` and `oauthFailure` read through `wording` as `auth.errors.<code>`, `auth.oauth.<code>` and `auth.fields.<field>.<problem>`. A code a flow answers with no entry here is a type error.
|
|
2
|
+
export const ERRORS = {
|
|
3
|
+
'form': 'Check the fields and try again.',
|
|
4
|
+
'credentials': 'Wrong email, username or password.',
|
|
5
|
+
'limited': 'Too many attempts. Wait a while, then try again.',
|
|
6
|
+
'captcha': 'Complete the check that you are not a robot.',
|
|
7
|
+
'captchaAgain': 'The check in your browser did not go through: send the form again.',
|
|
8
|
+
'captchaScript': 'This form runs a check in your browser, which needs JavaScript: turn it on, then send it again.',
|
|
9
|
+
'early': 'Wait a moment, then send it again.',
|
|
10
|
+
'unverified': 'Verify your address first: open the link mailed to you.',
|
|
11
|
+
'banned': 'This account is suspended.',
|
|
12
|
+
'unavailable': 'Mail is not set up on this site.',
|
|
13
|
+
'link': 'This link is invalid or was already used.',
|
|
14
|
+
'code': 'Wrong code.',
|
|
15
|
+
'expired': 'This took too long: start again.',
|
|
16
|
+
'signed-out': 'Sign in first.',
|
|
17
|
+
'verified': 'Your address is already verified.',
|
|
18
|
+
'proof': 'Wrong password or code.',
|
|
19
|
+
'stale': 'For your safety, sign in again before doing this.',
|
|
20
|
+
'impersonating': 'Not while acting as another user.',
|
|
21
|
+
'enabled': 'Two-step verification is already on.',
|
|
22
|
+
'not-set-up': 'Set up two-step verification first.',
|
|
23
|
+
'not-enabled': 'Two-step verification is off.',
|
|
24
|
+
'off': 'This is switched off on this site.',
|
|
25
|
+
'not-linked': 'This account is not connected.',
|
|
26
|
+
'last-way-in': 'This is your only way to sign in: add another first.',
|
|
27
|
+
'self': 'You cannot do this to your own account.',
|
|
28
|
+
'last-admin': 'You are the only admin: make someone else an admin first.',
|
|
29
|
+
'last-owner': 'You are the only owner of an organization with other members: make another member an owner first.',
|
|
30
|
+
'refused': 'This account cannot be deleted right now.',
|
|
31
|
+
'not-found': 'Not found.',
|
|
32
|
+
'label': 'Give the token a name.',
|
|
33
|
+
'scope': 'Choose what the token is allowed to do.',
|
|
34
|
+
'expiry': 'Choose when the token expires.',
|
|
35
|
+
'invalid': 'That did not work.',
|
|
36
|
+
'none': 'No passkey to use.',
|
|
37
|
+
'forbidden': 'You may not do this.',
|
|
38
|
+
'two-factor-required': 'Turn on two-step verification in your account first.',
|
|
39
|
+
'unknown-role': 'This role does not exist.',
|
|
40
|
+
'admin': 'An admin cannot be changed this way: take the admin role away first.',
|
|
41
|
+
'until': 'Choose a date in the future.',
|
|
42
|
+
'invalid-name': 'Use 1 to 100 characters for the name, with no line breaks.',
|
|
43
|
+
'invalid-slug': 'Use lowercase letters, digits and dashes for the address.',
|
|
44
|
+
'slug-taken': 'This address is already used by another organization.',
|
|
45
|
+
'invalid-email': 'Type a valid address.',
|
|
46
|
+
'member': 'This person is already a member.',
|
|
47
|
+
'wrong-account': 'This invitation is for another address: sign in with the address it was sent to.',
|
|
48
|
+
} as const
|
|
49
|
+
|
|
50
|
+
export const OAUTH_ERRORS = {
|
|
51
|
+
'oauth': 'The sign-in with that provider failed. Try again.',
|
|
52
|
+
'cancelled': 'The sign-in was cancelled.',
|
|
53
|
+
'refused': 'This account may not sign in here.',
|
|
54
|
+
'taken': 'Another account already uses that address: sign in to it, then connect this provider.',
|
|
55
|
+
'unavailable': 'Signing in with that provider is not available.',
|
|
56
|
+
'signed-out': 'Sign in first.',
|
|
57
|
+
'banned': 'This account is suspended.',
|
|
58
|
+
} as const
|
|
59
|
+
|
|
60
|
+
export const FIELD_ERRORS = {
|
|
61
|
+
email: {
|
|
62
|
+
invalid: 'Type a valid address.',
|
|
63
|
+
taken: 'An account already uses this address.',
|
|
64
|
+
unchanged: 'This is your current address.',
|
|
65
|
+
},
|
|
66
|
+
password: {
|
|
67
|
+
'too-short': 'Use at least {min} characters.',
|
|
68
|
+
'too-long': 'Use at most {max} characters.',
|
|
69
|
+
'breached': 'This password appeared in a data breach: choose another.',
|
|
70
|
+
},
|
|
71
|
+
username: {
|
|
72
|
+
'length': 'Use {min} to {max} characters.',
|
|
73
|
+
'characters': 'Use letters, digits, spaces, dots, dashes and underscores.',
|
|
74
|
+
'mixed-scripts': 'Use one alphabet only.',
|
|
75
|
+
'taken': 'This username is taken.',
|
|
76
|
+
'unchanged': 'This is your current username.',
|
|
77
|
+
},
|
|
78
|
+
profile: {
|
|
79
|
+
'required': 'Fill this in.',
|
|
80
|
+
'too-long': 'Use at most {max} characters.',
|
|
81
|
+
},
|
|
82
|
+
} as const
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { MinTimeRefusal } from 'kay'
|
|
2
|
+
import type { Auth } from '../auth'
|
|
3
|
+
import type { CaptchaAction } from '../captcha'
|
|
4
|
+
import type { Proof } from '../proof'
|
|
5
|
+
import type { AuthFeatures, ProfileProblem } from './index'
|
|
6
|
+
import { t } from 'elysia'
|
|
7
|
+
import { wording } from 'kay/words'
|
|
8
|
+
import { passkeyAssertion, passkeyAttestation } from '../payloads'
|
|
9
|
+
import { PROFILE_MAX_LENGTH } from '../rules'
|
|
10
|
+
import { ERRORS, FIELD_ERRORS, OAUTH_ERRORS } from './english'
|
|
11
|
+
|
|
12
|
+
// The codes the pages word, as the English table holds them: a code a flow answers with no message is a type error here.
|
|
13
|
+
export type FailureCode = keyof typeof ERRORS
|
|
14
|
+
export type OAuthFailureCode = keyof typeof OAUTH_ERRORS
|
|
15
|
+
type Fields = typeof FIELD_ERRORS
|
|
16
|
+
type Problem<Field extends keyof Fields> = keyof Fields[Field]
|
|
17
|
+
|
|
18
|
+
// A scope as the page shows it: the label the app words under `auth.scopes.<scope>` in its catalogs, else the scope itself.
|
|
19
|
+
export function scopeLabel(scope: string): string {
|
|
20
|
+
return wording(`auth.scopes.${scope}`, scope)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Stands for a `minTime` refusal's reason, which kay words before the request's locale is known: `refusal` puts it in the page's language.
|
|
24
|
+
const MIN_TIME = 'kauth-min-time:'
|
|
25
|
+
const PASSWORD_PROBLEMS: ReadonlySet<string> = new Set<Problem<'password'>>(['too-short', 'too-long', 'breached'])
|
|
26
|
+
const OAUTH_FAILURES: ReadonlySet<string> = new Set<OAuthFailureCode>(['oauth', 'cancelled', 'refused', 'taken', 'unavailable', 'signed-out', 'banned'])
|
|
27
|
+
|
|
28
|
+
// What the prebuilt pages' actions take. A field longer than kayjs-auth would ever accept is refused here rather than read.
|
|
29
|
+
const email = t.String({ maxLength: 254 })
|
|
30
|
+
const password = t.String({ maxLength: 1024 })
|
|
31
|
+
const code = t.String({ maxLength: 32 })
|
|
32
|
+
const token = t.String({ maxLength: 512 })
|
|
33
|
+
const redirect = t.Optional(t.String({ maxLength: 2048 }))
|
|
34
|
+
const remember = t.Optional(t.Boolean())
|
|
35
|
+
|
|
36
|
+
// The token a CAPTCHA widget posts, under its provider's name, or the solution to kay's proof of work.
|
|
37
|
+
const captcha = {
|
|
38
|
+
'cf-turnstile-response': t.Optional(t.String({ maxLength: 4096 })),
|
|
39
|
+
'h-captcha-response': t.Optional(t.String({ maxLength: 4096 })),
|
|
40
|
+
'kayjs-captcha': t.Optional(t.String({ maxLength: 512 })),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// What kay checks of a post from the pages open to visitors without JavaScript, which sends no proof of work: a field bots fill in, and a least time to fill the form in.
|
|
44
|
+
export const guarded = {
|
|
45
|
+
protect: { honeypot: 'website', minTime: { after: '2s', plain: true } },
|
|
46
|
+
messages: { minTime: (reason: MinTimeRefusal) => MIN_TIME + reason },
|
|
47
|
+
} as const
|
|
48
|
+
|
|
49
|
+
// The current password, or a code for an account without one or a change asking for the second factor.
|
|
50
|
+
const proof = { password: t.Optional(password), code: t.Optional(code) }
|
|
51
|
+
|
|
52
|
+
// Whether the sign-in a form asks for lasts `remember.maxAge`: the box when the pages show one, else what `pages.remember` fixes, the same by every way of signing in.
|
|
53
|
+
export function remembered(features: Pick<AuthFeatures, 'pages'>, chosen: boolean | undefined): boolean {
|
|
54
|
+
const mode = features.pages?.remember ?? 'checkbox'
|
|
55
|
+
|
|
56
|
+
return mode === 'always' || (mode === 'checkbox' && chosen === true)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// What the sign-in and register pages share about the box: whether to show it, and the provider links carrying the choice a click without script leaves.
|
|
60
|
+
export function rememberOf(features: Pick<AuthFeatures, 'pages' | 'prefix'>, redirect: string, label: (provider: string) => string, providers: AuthFeatures['providers']) {
|
|
61
|
+
const mode = features.pages?.remember ?? 'checkbox'
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
askRemember: mode === 'checkbox',
|
|
65
|
+
providerLinks: providers.map(provider => ({ name: provider.name, label: label(provider.label), href: `${features.prefix}/${provider.name}?redirect=${encodeURIComponent(redirect)}&remember=${mode === 'always' ? 1 : 0}` })),
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// The longest id an invitation or another record id field accepts.
|
|
70
|
+
export const ID_MAX = 128
|
|
71
|
+
|
|
72
|
+
export const schemas = {
|
|
73
|
+
signIn: t.Object({ identifier: email, password, remember, redirect, ...captcha }),
|
|
74
|
+
magicLink: t.Object({ email, remember, redirect, ...captcha }),
|
|
75
|
+
emailCode: t.Object({ code }),
|
|
76
|
+
passkey: t.Object({ response: passkeyAssertion, redirect, remember }),
|
|
77
|
+
registerPasskey: t.Object({ response: passkeyAttestation, name: t.Optional(t.String({ maxLength: 64 })) }),
|
|
78
|
+
register: t.Object({ email, password, username: t.Optional(t.String({ maxLength: 128 })), remember, redirect, ...captcha }),
|
|
79
|
+
twoFactor: t.Object({ code }),
|
|
80
|
+
requestReset: t.Object({ email, redirect, ...captcha }),
|
|
81
|
+
reset: t.Object({ token, password }),
|
|
82
|
+
token: t.Object({ token }),
|
|
83
|
+
verify: t.Object({ token, password: t.Optional(password) }),
|
|
84
|
+
proof: t.Object(proof),
|
|
85
|
+
// Whichever fields the app's profile has: `saveProfile` reads those it knows.
|
|
86
|
+
profile: t.Object({}, { additionalProperties: true }),
|
|
87
|
+
changePassword: t.Object({ ...proof, newPassword: password }),
|
|
88
|
+
changeEmail: t.Object({ ...proof, email }),
|
|
89
|
+
changeUsername: t.Object({ username: t.String({ maxLength: 128 }) }),
|
|
90
|
+
enableTwoFactor: t.Object({ code, uri: t.String({ maxLength: 1024 }) }),
|
|
91
|
+
id: t.Object({ id: t.String({ maxLength: ID_MAX }) }),
|
|
92
|
+
adminRole: t.Object({ role: t.String({ maxLength: 100 }) }),
|
|
93
|
+
adminDelete: t.Object({ confirm: t.Optional(t.Boolean()) }),
|
|
94
|
+
adminBan: t.Object({ reason: t.Optional(t.String({ maxLength: 500 })), until: t.Optional(t.String({ maxLength: 10 })) }),
|
|
95
|
+
organization: t.Object({ name: t.String({ maxLength: 200 }), slug: t.Optional(t.String({ maxLength: 100 })) }),
|
|
96
|
+
memberRoles: t.Object({ userId: t.String({ maxLength: 128 }), roles: t.Optional(t.Array(t.String({ maxLength: 100 }), { maxItems: 100 })) }),
|
|
97
|
+
member: t.Object({ userId: t.String({ maxLength: 128 }) }),
|
|
98
|
+
invite: t.Object({ email, roles: t.Optional(t.Array(t.String({ maxLength: 100 }), { maxItems: 100 })) }),
|
|
99
|
+
oauthClient: t.Object({ id: t.String({ maxLength: 2048 }) }),
|
|
100
|
+
// The authorization request the consent page was shown for, which the answer is checked against again.
|
|
101
|
+
oauthConsent: t.Object({ client_id: t.String({ maxLength: 2048 }), redirect_uri: t.String({ maxLength: 2048 }), code_challenge: t.String({ maxLength: 128 }), scope: t.String({ maxLength: 2048 }), state: t.String({ maxLength: 2048 }), resource: t.String({ maxLength: 2048 }), decision: t.Union([t.Literal('allow'), t.Literal('deny')]) }),
|
|
102
|
+
unlink: t.Object({ ...proof, provider: t.String({ maxLength: 64 }) }),
|
|
103
|
+
removePasskey: t.Object({ ...proof, id: t.String({ maxLength: 512 }) }),
|
|
104
|
+
renamePasskey: t.Object({ id: t.String({ maxLength: 512 }), name: t.String({ minLength: 1, maxLength: 64 }) }),
|
|
105
|
+
createToken: t.Object({ ...proof, label: t.String({ maxLength: 100 }), scopes: t.Optional(t.Array(t.String({ maxLength: 100 }), { maxItems: 100 })), days: t.Optional(t.Integer({ minimum: 0, maximum: 3650 })) }),
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function captchaOf(input: { 'cf-turnstile-response'?: string | undefined, 'h-captcha-response'?: string | undefined, 'kayjs-captcha'?: string | undefined }): string | undefined {
|
|
109
|
+
return input['cf-turnstile-response'] ?? input['h-captcha-response'] ?? input['kayjs-captcha']
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// An action's failure code, `captchaScript` for a CAPTCHA refused with no token posted, as a browser without JavaScript sends none: `refusal` tells it apart from a stale solution.
|
|
113
|
+
export function refusedFor<Code extends string>(error: Code, input: Parameters<typeof captchaOf>[0]): Code | 'captchaScript' {
|
|
114
|
+
return error === 'captcha' && (captchaOf(input) ?? '') === '' ? 'captchaScript' : error
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Whether the page shows its CAPTCHA for `action`, with the proof of work's challenge, reading the client's attempts once.
|
|
118
|
+
export async function captchaFor(auth: Pick<Auth, 'captchaRequired' | 'captchaChallenge'>, captcha: AuthFeatures['captcha'], action: CaptchaAction): Promise<{ shown: boolean, challenge: string | null }> {
|
|
119
|
+
if (captcha !== 'proof-of-work')
|
|
120
|
+
return { shown: captcha !== null && await auth.captchaRequired(action), challenge: null }
|
|
121
|
+
|
|
122
|
+
const challenge = await auth.captchaChallenge(action)
|
|
123
|
+
|
|
124
|
+
return { shown: challenge !== null, challenge }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function proofOf(input: { password?: string | undefined, code?: string | undefined }): Proof {
|
|
128
|
+
return { ...input.password !== undefined && { password: input.password }, ...input.code !== undefined && { code: input.code } }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// What a page shows for a failure code, in the request's language.
|
|
132
|
+
export function failure(error: FailureCode): string {
|
|
133
|
+
return wording(`auth.errors.${error}`, ERRORS[error])
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function captchaFailure(captcha: AuthFeatures['captcha'], error: string): FailureCode {
|
|
137
|
+
if (captcha !== 'proof-of-work')
|
|
138
|
+
return 'captcha'
|
|
139
|
+
|
|
140
|
+
return error === 'captcha' ? 'captchaAgain' : 'captchaScript'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// What kay refused a post for before the action: a reason `guarded` worded as `MIN_TIME` followed by it.
|
|
144
|
+
function minTimeReason(refused: string): FailureCode | undefined {
|
|
145
|
+
const reason = refused.slice(MIN_TIME.length)
|
|
146
|
+
|
|
147
|
+
return reason === 'early' || reason === 'expired' ? reason : undefined
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// What a page open to visitors shows for a refused post: what kay refused it for before the action, or its failure code, a refused proof of work worded by whether the browser ran it.
|
|
151
|
+
export function refusal(failed: { errors: Partial<Record<string, string>>, data?: { error?: FailureCode } | undefined } | undefined, captcha: AuthFeatures['captcha']): string | undefined {
|
|
152
|
+
const error = failed?.data?.error
|
|
153
|
+
|
|
154
|
+
if (error === undefined) {
|
|
155
|
+
const refused = failed?.errors['']
|
|
156
|
+
|
|
157
|
+
return refused?.startsWith(MIN_TIME) ? failure(minTimeReason(refused) ?? 'form') : refused
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// A proof of work refused with a solution is a stale one, which the page now holds a fresh challenge for; a widget keeps its one message.
|
|
161
|
+
return error === 'captcha' || error === 'captchaScript' ? failure(captchaFailure(captcha, error)) : failure(error)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// What a page shows under a field refused for `problem`, with the limits the app's rules hold it to. A password problem the app's own `check` returned that has no message of the page's shows as it is.
|
|
165
|
+
export function issue(...[field, problem, rules]: [field: 'email', problem: Problem<'email'>, rules?: undefined] | [field: 'password', problem: Problem<'password'> | (string & {}), rules: AuthFeatures['rules']] | [field: 'username', problem: Problem<'username'>, rules: AuthFeatures['rules']]): string {
|
|
166
|
+
switch (field) {
|
|
167
|
+
case 'email': return wording(`auth.fields.email.${problem}`, FIELD_ERRORS.email[problem])
|
|
168
|
+
case 'password': return PASSWORD_PROBLEMS.has(problem) ? wording(`auth.fields.password.${problem as Problem<'password'>}`, FIELD_ERRORS.password[problem as Problem<'password'>], { min: rules.password.min, max: rules.password.max }) : problem
|
|
169
|
+
case 'username': return wording(`auth.fields.username.${problem}`, FIELD_ERRORS.username[problem], { min: rules.username.min, max: rules.username.max })
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// What a page shows under a profile field the app asked for, `max` being the field's own limit when it set one.
|
|
174
|
+
export function profileIssue(problem: ProfileProblem, max?: number): string {
|
|
175
|
+
return typeof problem === 'object' ? problem.message : wording(`auth.fields.profile.${problem}`, FIELD_ERRORS.profile[problem], { max: max ?? PROFILE_MAX_LENGTH })
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// The message for the `?error=` a failed provider sign-in comes back with; undefined for none, or one kayjs-auth never sends.
|
|
179
|
+
export function oauthFailure(error: string | null): string | undefined {
|
|
180
|
+
return error !== null && isOAuthFailure(error) ? wording(`auth.oauth.${error}`, OAUTH_ERRORS[error]) : undefined
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function isOAuthFailure(error: string): error is OAuthFailureCode {
|
|
184
|
+
return OAUTH_FAILURES.has(error)
|
|
185
|
+
}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import type { ExtensionApp, KayExtension, PluginInput } from 'kay/server'
|
|
2
|
+
import type { CaptchaWidget } from '../captcha'
|
|
3
|
+
import type { BareUser, Core } from '../core'
|
|
4
|
+
import type { CodedLink, CodeMode } from '../email'
|
|
5
|
+
import type { OrganizationsCore } from '../organizationsCore'
|
|
6
|
+
import type { OwnOptions, OwnPlugin } from '../own'
|
|
7
|
+
import type { CreateUserInput, CreateUserResult } from '../password'
|
|
8
|
+
import type { LinkType } from '../store'
|
|
9
|
+
import { resolve } from 'node:path'
|
|
10
|
+
import { appModules, checkOptionKeys, configError } from 'kay/library'
|
|
11
|
+
import { pickLocale } from '../locale'
|
|
12
|
+
import { buildOwn, MAIL_EXTENSION, ownEnv } from '../own'
|
|
13
|
+
|
|
14
|
+
export interface AuthPagesOptions {
|
|
15
|
+
// Where the pages are served, `/auth` by default, beside kayjs-auth's own routes: `/auth/sign-in`. `authCore` reads it when `auth()` loads it with `module`.
|
|
16
|
+
prefix?: string | undefined
|
|
17
|
+
// Where a sign-in goes when nothing asked for another page, and a sign-out always: `/` by default.
|
|
18
|
+
home?: string | undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type RememberMode = 'checkbox' | 'always' | 'never'
|
|
22
|
+
|
|
23
|
+
// One input of the profile form the account page shows beside the username.
|
|
24
|
+
export interface ProfileField {
|
|
25
|
+
// A string, or the label in each locale of the site by tag (`{ en: 'Bio', fr: 'Biographie' }`): the page shows the one matching its locale, else the first.
|
|
26
|
+
label: string | Record<string, string>
|
|
27
|
+
// An `input` of this type, or a `textarea`; `text` by default.
|
|
28
|
+
type?: 'text' | 'textarea' | 'tel' | 'url' | 'email' | undefined
|
|
29
|
+
autocomplete?: string | undefined
|
|
30
|
+
required?: boolean | undefined
|
|
31
|
+
// The longest value, 500 characters by default.
|
|
32
|
+
maxLength?: number | undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// A field of the profile form with what the user holds in it.
|
|
36
|
+
export interface ProfileFieldView extends Omit<ProfileField, 'label'> {
|
|
37
|
+
name: string
|
|
38
|
+
// The label in the locale asked for.
|
|
39
|
+
label: string
|
|
40
|
+
// The limit in force, 500 characters when the field gave none.
|
|
41
|
+
maxLength: number
|
|
42
|
+
value: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Why a field was refused: left empty, too long, or in the words of the app's `save`.
|
|
46
|
+
export type ProfileProblem = 'required' | 'too-long' | { message: string }
|
|
47
|
+
|
|
48
|
+
// The profile data an app keeps beside kayjs-auth's users, edited on the account page's profile form.
|
|
49
|
+
export interface ProfileOptions<Field extends string = string> {
|
|
50
|
+
fields: Record<Field, ProfileField>
|
|
51
|
+
// What each field holds now for this user, read when the form renders.
|
|
52
|
+
load: (user: BareUser) => Partial<Record<Field, string | null | undefined>> | Promise<Partial<Record<Field, string | null | undefined>>>
|
|
53
|
+
// Stores values already trimmed and checked against `required` and `maxLength`; returns `{ errors }` naming what it refused, by field.
|
|
54
|
+
save: (user: BareUser, values: Record<Field, string>) => void | { errors: Partial<Record<Field, string>> } | Promise<void | { errors: Partial<Record<Field, string>> }>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// What `authCore`'s `pages` option takes: `auth()`'s own options and the profile data of the app.
|
|
58
|
+
export interface AuthPagesConfig extends AuthPagesOptions {
|
|
59
|
+
// The "keep me signed in" choice of the sign-in and register pages: `'checkbox'` shows a box that decides (unchecked is the same short session by every way of signing in, OAuth and passkeys included), `'always'` hides it and keeps every sign-in `remember.maxAge`, `'never'` hides it and ends every session with the browser. `'checkbox'` by default.
|
|
60
|
+
remember?: RememberMode | undefined
|
|
61
|
+
// Whether the prebuilt pages write their content in a `main` landmark, `true` by default. `false` for an app whose layout already wraps its pages in one, so a page holds a single `main`.
|
|
62
|
+
main?: boolean | undefined
|
|
63
|
+
// Where the privacy policy is, such as `/privacy`: the register page links it under the form, after the purpose line. A site collecting email addresses states what for and links its policy; without it a production start warns.
|
|
64
|
+
privacy?: string | undefined
|
|
65
|
+
// What the register page tells a visitor their address and name are used for, drawn under the form; `privacy` alone draws the link after a stock line, worded as `auth.register.purpose`.
|
|
66
|
+
purpose?: string | undefined
|
|
67
|
+
// Extra fields on the account page's profile form, with where they are read and stored.
|
|
68
|
+
profile?: ProfileOptions | undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface AuthPages<Server extends PluginInput = PluginInput> extends KayExtension<Server> {
|
|
72
|
+
prefix: string
|
|
73
|
+
home: string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// What the config enables, for a page to show only what works.
|
|
77
|
+
export interface AuthFeatures {
|
|
78
|
+
// The OAuth providers in the order configured, by the name their routes take, with what a button calls them.
|
|
79
|
+
providers: { name: string, label: string }[]
|
|
80
|
+
// Whether mail is set up: magic links, password resets, verification and email changes.
|
|
81
|
+
email: boolean
|
|
82
|
+
// What sign-in and verification mails carry, a link, a code or both; null without mail.
|
|
83
|
+
codes: Record<CodedLink, CodeMode> | null
|
|
84
|
+
// Whether visitors make their own accounts: registering, and signing in with a provider account no user has linked.
|
|
85
|
+
signUp: boolean
|
|
86
|
+
// Whether password sign-in waits for a verified address, the default with mail.
|
|
87
|
+
requireVerified: boolean
|
|
88
|
+
// Whether passkeys work: switched on and the site has an origin.
|
|
89
|
+
passkeys: boolean
|
|
90
|
+
// Whether password sign-in, registering, resets and changes are on, `password: false` switching them off.
|
|
91
|
+
password: boolean
|
|
92
|
+
// Whether a sign-in link or code can be mailed: mail is set up and `magicLink` is not false.
|
|
93
|
+
magicLink: boolean
|
|
94
|
+
// Whether users can turn on two-step verification, `twoFactor: false` switching that off.
|
|
95
|
+
twoFactor: boolean
|
|
96
|
+
// The CAPTCHA widget to show where `captchaRequired` says one is needed, `'proof-of-work'` for kay's, whose field carries `captchaChallenge`, or null without one.
|
|
97
|
+
captcha: CaptchaWidget | 'proof-of-work' | null
|
|
98
|
+
// What a bearer token may be limited to; any token may do anything without them.
|
|
99
|
+
scopes: string[]
|
|
100
|
+
// Where kayjs-auth's own routes live: `${prefix}/github` starts a GitHub sign-in.
|
|
101
|
+
prefix: string
|
|
102
|
+
// Whether MCP clients sign in to this app through its own OAuth server, which the account page then lists the approved ones of.
|
|
103
|
+
oauthServer: boolean
|
|
104
|
+
// Whether the `admin` option names who manages users. The prebuilt admin pages answer 404 without it.
|
|
105
|
+
admin: boolean
|
|
106
|
+
// The `organizations` option as the prebuilt organization pages need it: every role in declaration order, who owns an organization, who manages it and, per role, the roles holding it grants through the hierarchy (itself included); null without the option, when the pages answer 404.
|
|
107
|
+
organizations: { roles: string[], owner: string, manage: string, grants: Record<string, string[]> } | null
|
|
108
|
+
// Whether the `audit` option keeps the audit log.
|
|
109
|
+
audit: boolean
|
|
110
|
+
// The seconds `account.delete` leaves an account pending before deleting it (`account.deleteAfter`); null when it deletes at once.
|
|
111
|
+
deleteAfter: number | null
|
|
112
|
+
// Where a sign-in that failed goes, and where a guard sends an anonymous visitor once the prebuilt pages are on.
|
|
113
|
+
signInPage: string
|
|
114
|
+
// The prebuilt pages' prefix and home; null without them.
|
|
115
|
+
pages: { prefix: string, home: string, remember: RememberMode, main: boolean, privacy?: string | undefined, purpose?: string | undefined } | null
|
|
116
|
+
// The password and username limits, for a page or an island to show and check with: `passwordProblem(value, features.rules)`. The password rule's `check` stays on the server and in the app's own `rules`.
|
|
117
|
+
rules: { password: { min: number, max: number }, username: { min: number, max: number } }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export const DEFAULT_PREFIX = '/auth'
|
|
121
|
+
|
|
122
|
+
// The files an `auth()` with no module reads by convention: kay imports, bakes and reloads them.
|
|
123
|
+
const APP_FILES = { hooks: 'src/auth.ts', mail: 'src/mail.ts' }
|
|
124
|
+
|
|
125
|
+
// The dev tool's Auth tab, which kay loads on dev pages.
|
|
126
|
+
const DEV_TOOL = { module: `${import.meta.dir}/../devTool.ts` }
|
|
127
|
+
|
|
128
|
+
function captchaFeature(captcha: Core['captcha']): AuthFeatures['captcha'] {
|
|
129
|
+
if (!captcha)
|
|
130
|
+
return null
|
|
131
|
+
|
|
132
|
+
return 'proofOfWork' in captcha ? 'proof-of-work' : captcha.widget ?? null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function grantsOf(organizations: OrganizationsCore): Record<string, string[]> {
|
|
136
|
+
return Object.fromEntries([...organizations.roles].map(role => [role, [...organizations.roles].filter(granted => organizations.isGranted(granted, [role]))]))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function featuresOf(core: Core): AuthFeatures {
|
|
140
|
+
return {
|
|
141
|
+
providers: [...core.oauth].map(([name, { app }]) => ({ name, label: app.label ?? app.provider.label ?? name })),
|
|
142
|
+
email: core.email !== undefined,
|
|
143
|
+
codes: core.email?.codes ?? null,
|
|
144
|
+
signUp: core.signUp,
|
|
145
|
+
requireVerified: core.email?.requireVerified ?? false,
|
|
146
|
+
passkeys: core.switches.passkeys && core.passkeys !== undefined,
|
|
147
|
+
password: core.switches.password,
|
|
148
|
+
magicLink: core.switches.magicLink && core.email !== undefined,
|
|
149
|
+
twoFactor: core.switches.twoFactor,
|
|
150
|
+
captcha: captchaFeature(core.captcha),
|
|
151
|
+
scopes: [...core.scopes],
|
|
152
|
+
prefix: core.prefix,
|
|
153
|
+
oauthServer: core.oauthServer !== undefined,
|
|
154
|
+
signInPage: core.signInPage,
|
|
155
|
+
admin: core.admin !== undefined,
|
|
156
|
+
organizations: core.organizations ? { roles: [...core.organizations.roles], owner: core.organizations.owner, manage: core.organizations.manage, grants: grantsOf(core.organizations) } : null,
|
|
157
|
+
audit: core.audit !== undefined,
|
|
158
|
+
deleteAfter: core.deletion?.after ?? null,
|
|
159
|
+
rules: { password: { min: core.rules.password.min, max: core.rules.password.max }, username: { ...core.rules.username } },
|
|
160
|
+
pages: core.pages ? { prefix: core.pages.prefix, home: core.pages.home, remember: core.pages.remember, main: core.pages.main, privacy: core.pages.privacy, purpose: core.pages.purpose } : null,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The pages `auth()` serves for `authCore({ pages })` to send users to, each under the prefix.
|
|
165
|
+
export function pagePaths(prefix: string) {
|
|
166
|
+
return {
|
|
167
|
+
signIn: `${prefix}/sign-in`,
|
|
168
|
+
twoFactor: `${prefix}/two-factor`,
|
|
169
|
+
sessions: `${prefix}/account/sessions`,
|
|
170
|
+
security: `${prefix}/account/security`,
|
|
171
|
+
data: `${prefix}/account/data`,
|
|
172
|
+
links: {
|
|
173
|
+
'verify-email': `${prefix}/verify-email`,
|
|
174
|
+
'reset-password': `${prefix}/reset-password`,
|
|
175
|
+
'magic-link': `${prefix}/magic-link`,
|
|
176
|
+
'change-email': `${prefix}/confirm-email`,
|
|
177
|
+
} satisfies Record<LinkType, string>,
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The module `src/auth.ts` is: the `authCore` result as its `auth` export or its default one.
|
|
182
|
+
export type AuthModule = { auth: { plugin: PluginInput } } | { default: { plugin: PluginInput } }
|
|
183
|
+
|
|
184
|
+
// The plugin of an `AuthModule`, for `kay check` to type `ctx.locals.auth` off.
|
|
185
|
+
export type PluginOf<Module> = Module extends { auth: { plugin: infer Plugin extends PluginInput } } ? Plugin : Module extends { default: { plugin: infer Plugin extends PluginInput } } ? Plugin : never
|
|
186
|
+
|
|
187
|
+
// What `authCore` hands `auth()` to settle the prefix and home between them, whichever of the two was made first.
|
|
188
|
+
export const PAGES_OF = Symbol('kayjs-auth.pages')
|
|
189
|
+
|
|
190
|
+
export interface PagesHandle {
|
|
191
|
+
// Takes the prefix and home `auth()` was given where `authCore({ pages })` set none of its own, and throws when both name another prefix.
|
|
192
|
+
adopt: (given: { prefix?: string | undefined, home?: string | undefined }) => void
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Set on the plugin of a `authCore` result, so what has the plugin alone finds the `auth` it came from.
|
|
196
|
+
export const AUTH_OF = Symbol('kayjs-auth.auth')
|
|
197
|
+
|
|
198
|
+
// Set on the extensions kayjs-auth gives, answering the app's `auth` for the commands, given the project root.
|
|
199
|
+
export const LOAD_AUTH = Symbol('kayjs-auth.load')
|
|
200
|
+
|
|
201
|
+
// Set on the `authCore` result, answering what `createUser` would refuse a login for.
|
|
202
|
+
export const CHECK_USER = Symbol('kayjs-auth.checkUser')
|
|
203
|
+
|
|
204
|
+
// What `kay auth create-user` needs of the app's `auth`.
|
|
205
|
+
export interface UserMaker {
|
|
206
|
+
// What `createUser` would refuse the login for, asked before the password is.
|
|
207
|
+
[CHECK_USER]: (input: Omit<CreateUserInput, 'roles'> & { roles?: string[] | undefined }) => Promise<Partial<Record<'email' | 'username' | 'password', string>>>
|
|
208
|
+
admin: { createUser: (input: Omit<CreateUserInput, 'roles'> & { roles?: string[] | undefined }) => Promise<CreateUserResult> }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// The commands both extensions add.
|
|
212
|
+
const COMMANDS = { 'auth create-user': `${import.meta.dir}/../createUserCommand.ts` }
|
|
213
|
+
|
|
214
|
+
// The `auth` of a module: the `authCore` result as its `auth` export or its default one.
|
|
215
|
+
function authOfModule(module: Partial<Record<'auth' | 'default', { plugin: PluginInput, [PAGES_OF]?: PagesHandle | undefined }>>) {
|
|
216
|
+
const auth = module.auth ?? module.default
|
|
217
|
+
|
|
218
|
+
if (!auth)
|
|
219
|
+
throw configError('auth', 'module', 'the auth module exports no `auth`, the `authCore` result')
|
|
220
|
+
|
|
221
|
+
return auth
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// The `auth` behind a plugin handed as `server`.
|
|
225
|
+
async function authOfServer(server: (() => PluginInput | Promise<PluginInput>) | undefined): Promise<UserMaker> {
|
|
226
|
+
const plugin = await server?.() as { [AUTH_OF]?: UserMaker } | undefined
|
|
227
|
+
const auth = plugin?.[AUTH_OF]
|
|
228
|
+
|
|
229
|
+
if (!auth)
|
|
230
|
+
throw configError('auth', 'module', 'the `server` of auth() is not the `plugin` of a `authCore` result, so its commands cannot reach the app\'s auth; give `module: \'./src/auth\'` instead')
|
|
231
|
+
|
|
232
|
+
return auth
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const VIEWER_LAYOUT = `${import.meta.dir}/../viewer-layout/_layout.kay`
|
|
236
|
+
const CONTEXT = `${import.meta.dir}/../context.ts`
|
|
237
|
+
|
|
238
|
+
// `ctx.user`, `ctx.can` and `ctx.impersonator` for the templates, read ahead of the pages reaching one; `viewer: true` also reads the user ahead of every page, for an app whose own components call `currentUser`.
|
|
239
|
+
function viewerExtension(viewer: boolean | undefined): { context: string, layout?: string } {
|
|
240
|
+
return { context: CONTEXT, ...viewer === true && { layout: VIEWER_LAYOUT } }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export interface AuthExtensionOptions extends AuthPagesOptions {
|
|
244
|
+
// The file exporting the app's `auth`, the `authCore` result, as a path from the project root: `'./src/auth'`. kay imports it as the server starts, never as the config is read, so the config stays off the app's database, and `kay check` types `ctx.locals.auth` and links to kayjs-auth's routes off it.
|
|
245
|
+
module?: string | undefined
|
|
246
|
+
// Any other plugin carrying an `authCore` result, as a function, in place of `module`. `false` builds no plugin, for an app mounting it by hand, `new Elysia().use(auth.plugin).use(await pages())`: the extension then serves the pages alone, as `mail({ server: false })` serves nothing.
|
|
247
|
+
server?: (() => PluginInput | Promise<PluginInput>) | false | undefined
|
|
248
|
+
// `false` leaves out the prebuilt pages and serves the plugin alone, for an app writing every page itself. `true` by default.
|
|
249
|
+
pages?: boolean | undefined
|
|
250
|
+
// Templates read `ctx.user`, `ctx.can(role)` and `ctx.impersonator` with no option, as `SignedIn`, `SignedOut`, `UserMenu` and `ImpersonationBanner` do, the session being read before the render of each page reaching one of them and of no other. `true` also reads the user ahead of every page, for an app whose own components call `currentUser`.
|
|
251
|
+
viewer?: boolean | undefined
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// The `auth` an app's module exports, for `prepare` to hand it the prefix and home `auth()` was given.
|
|
255
|
+
function authOfExport(exported: unknown, module: string): { [PAGES_OF]?: PagesHandle | undefined } {
|
|
256
|
+
if (typeof exported !== 'object' || exported === null || !('plugin' in exported))
|
|
257
|
+
throw new Error(`kayjs-auth: ${module} exports no \`auth\`: export the \`authCore\` result as \`export const auth = authCore(...)\``)
|
|
258
|
+
|
|
259
|
+
return exported as { [PAGES_OF]?: PagesHandle | undefined }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const KNOWN_OPTIONS: Record<keyof OwnOptions | keyof AuthExtensionOptions, true> = { module: true, server: true, prefix: true, pages: true, cookie: true, oauth: true, store: true, email: true, hooks: true, viewer: true, secret: true, site: true, name: true, signInPage: true, failurePage: true, twoFactorPage: true, allow: true, signUp: true, onSignUp: true, user: true, profileFields: true, beforeDelete: true, guests: true, exportData: true, onEvent: true, audit: true, scopes: true, roles: true, organizations: true, admin: true, defaultRoles: true, remember: true, rules: true, sessionsPage: true, securityPage: true, locate: true, rateLimit: true, passkeys: true, password: true, magicLink: true, twoFactor: true, captcha: true, breachedPasswords: true, proxy: true, oauthServer: true, forwardAuth: true, authorize: true, home: true, account: true }
|
|
263
|
+
|
|
264
|
+
// kayjs-auth for `extensions` in kay.config.ts, as the whole declaration: `auth({ module: './src/auth' })`, the module exporting the `auth` made with `authCore`. Its plugin is used before the pages, so the app mounts nothing else and declares no `Register`, and `prefix` and `home` reach `authCore({ pages: true })`; the prebuilt pages come with it unless `pages: false`.
|
|
265
|
+
export function auth<Role extends string = string, Prefix extends string = '/auth', const Scope extends string = never, OrgRole extends string = never, UserData extends object = Record<string, never>>(options?: OwnOptions<Role, Prefix, Scope, OrgRole, UserData>): AuthPages<OwnPlugin<Role, Prefix, Scope, OrgRole, UserData>>
|
|
266
|
+
export function auth(options: AuthExtensionOptions): AuthPages
|
|
267
|
+
export function auth(options: object = {}): AuthPages {
|
|
268
|
+
checkOptionKeys('auth()', options, KNOWN_OPTIONS)
|
|
269
|
+
|
|
270
|
+
const given = options as AuthExtensionOptions
|
|
271
|
+
|
|
272
|
+
return given.module === undefined && given.server === undefined ? ownAuth(options) : moduleAuth(given)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// `auth()` with no module: kayjs-auth builds the whole `authCore` as the server starts, so kay.config.ts declares everything and no file of the app is needed.
|
|
276
|
+
function ownAuth(options: OwnOptions): AuthPages {
|
|
277
|
+
const prefix = options.prefix ?? DEFAULT_PREFIX
|
|
278
|
+
// Built again only when the hooks, the mail module or kayjs-mail's registration are not the ones it was built from, so a reload under `kay dev` that left them alone keeps the store it opened; a test site always builds its own, so two sites of one project do not share a store.
|
|
279
|
+
let current: { root: string, database: string | undefined, hooks: unknown, mail: unknown, mailed: boolean, built: Promise<UserMaker & { plugin: PluginInput }> } | undefined
|
|
280
|
+
|
|
281
|
+
const build = async (app: ExtensionApp) => {
|
|
282
|
+
if (app.testCookies)
|
|
283
|
+
return buildOwn(options, app)
|
|
284
|
+
|
|
285
|
+
if (current?.root !== app.root || current.database !== app.database || current.hooks !== app.modules.hooks || current.mail !== app.modules.mail || current.mailed !== app.extensions.includes(MAIL_EXTENSION))
|
|
286
|
+
current = { root: app.root, database: app.database, hooks: app.modules.hooks, mail: app.modules.mail, mailed: app.extensions.includes(MAIL_EXTENSION), built: buildOwn(options, app) }
|
|
287
|
+
|
|
288
|
+
return current.built
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const extension: AuthPages = {
|
|
292
|
+
name: '@kayjs/auth',
|
|
293
|
+
appFiles: APP_FILES,
|
|
294
|
+
server: async app => (await build(app)).plugin,
|
|
295
|
+
...options.pages !== false && { routes: { dir: `${import.meta.dir}/routes`, prefix } },
|
|
296
|
+
...viewerExtension(options.viewer),
|
|
297
|
+
env: ownEnv(options),
|
|
298
|
+
devTool: DEV_TOOL,
|
|
299
|
+
commands: COMMANDS,
|
|
300
|
+
prefix,
|
|
301
|
+
home: options.home ?? '/',
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return Object.assign(extension, { [LOAD_AUTH]: async (root: string, extensions: string[], database?: string, dryRun?: boolean) => await build({ root, modules: await appModules(root, extension), extensions, database, ...dryRun && { exporting: true } }) as UserMaker })
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function moduleAuth(options: AuthExtensionOptions): AuthPages {
|
|
308
|
+
const { module, pages = true, viewer, prefix = DEFAULT_PREFIX, home } = options
|
|
309
|
+
const server = options.server === false ? undefined : options.server
|
|
310
|
+
|
|
311
|
+
if (module !== undefined && options.server !== undefined)
|
|
312
|
+
throw new Error('kayjs-auth: auth() takes `module` or `server`, not both')
|
|
313
|
+
|
|
314
|
+
const given = { prefix: options.prefix, home }
|
|
315
|
+
const extension: AuthPages = {
|
|
316
|
+
name: '@kayjs/auth',
|
|
317
|
+
...module !== undefined && { serverModule: { path: module, export: 'auth', prepare: (exported: unknown) => pages ? authOfExport(exported, module)[PAGES_OF]?.adopt(given) : undefined } },
|
|
318
|
+
...module === undefined && server !== undefined && { server },
|
|
319
|
+
...pages && { routes: { dir: `${import.meta.dir}/routes`, prefix } },
|
|
320
|
+
...viewerExtension(viewer),
|
|
321
|
+
devTool: DEV_TOOL,
|
|
322
|
+
commands: COMMANDS,
|
|
323
|
+
prefix,
|
|
324
|
+
home: home ?? '/',
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const load = async (root: string): Promise<UserMaker> => module === undefined ? authOfServer(server) : authOfModule({ auth: ((await import(resolve(root, module))) as { auth: { plugin: PluginInput } }).auth }) as unknown as UserMaker
|
|
328
|
+
|
|
329
|
+
return Object.assign(extension, { [LOAD_AUTH]: load })
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// The prefix and home `authCore` reads off its `pages` option.
|
|
333
|
+
export function pagesOf(pages: boolean | AuthPagesConfig | undefined): { prefix: string, home: string, remember: RememberMode, main: boolean, privacy: string | undefined, purpose: string | undefined, profile?: ProfileOptions } | undefined {
|
|
334
|
+
if (pages == null || pages === false)
|
|
335
|
+
return undefined
|
|
336
|
+
|
|
337
|
+
const options = pages === true ? {} : pages
|
|
338
|
+
|
|
339
|
+
return { prefix: options.prefix ?? DEFAULT_PREFIX, home: options.home ?? '/', remember: options.remember ?? 'checkbox', main: options.main ?? true, privacy: options.privacy, purpose: options.purpose, ...options.profile && { profile: options.profile } }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// The label of a profile field in `locale`: a plain string as it is, a map by locale tag the entry matching `locale` exactly or by language, else the first.
|
|
343
|
+
export function profileLabel(label: ProfileField['label'], locale: string | undefined): string {
|
|
344
|
+
if (typeof label === 'string')
|
|
345
|
+
return label
|
|
346
|
+
|
|
347
|
+
const tags = Object.keys(label)
|
|
348
|
+
|
|
349
|
+
return tags.length === 0 ? '' : label[pickLocale(tags, [locale])] ?? ''
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// What `load` answered for a field as the text the form shows: nothing is empty, and anything else than a string is the app's mistake, told rather than shown as empty.
|
|
353
|
+
export function profileValue(name: string, held: unknown): string {
|
|
354
|
+
if (held === undefined || held === null)
|
|
355
|
+
return ''
|
|
356
|
+
|
|
357
|
+
if (typeof held !== 'string')
|
|
358
|
+
throw new TypeError(`kayjs-auth: pages.profile.load answered a ${typeof held} for the field "${name}", which takes a string, null or undefined`)
|
|
359
|
+
|
|
360
|
+
return held
|
|
361
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AuthFeatures } from './index'
|
|
2
|
+
import { localize } from 'kay/i18n'
|
|
3
|
+
|
|
4
|
+
// The page of one organization, named by its slug when it has one, in the request's locale.
|
|
5
|
+
export function organizationPage(features: Pick<AuthFeatures, 'pages' | 'prefix'>, organization: { id: string, slug: string | null }): string {
|
|
6
|
+
return localize(`${features.pages?.prefix ?? features.prefix}/account/organizations/${encodeURIComponent(organization.slug ?? organization.id)}`)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// The page listing the organizations, in the request's locale.
|
|
10
|
+
export function organizationsPage(features: Pick<AuthFeatures, 'pages' | 'prefix'>): string {
|
|
11
|
+
return localize(`${features.pages?.prefix ?? features.prefix}/account/organizations`)
|
|
12
|
+
}
|