@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
package/src/cbor.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// A CBOR value as WebAuthn structures hold one: integers, byte and text strings, arrays, maps keyed by integers or text, and the simple values.
|
|
2
|
+
export type CborValue = number | string | boolean | null | undefined | Uint8Array | CborValue[] | CborMap
|
|
3
|
+
export type CborMap = Map<number | string, CborValue>
|
|
4
|
+
|
|
5
|
+
const MAX_DEPTH = 16
|
|
6
|
+
const SIMPLE = new Map<number, CborValue>([[20, false], [21, true], [22, null], [23, undefined]])
|
|
7
|
+
const UTF8 = new TextDecoder('utf-8', { fatal: true })
|
|
8
|
+
|
|
9
|
+
// The CBOR item starting at `offset` in `bytes`, and the offset it ends at. Throws on what no WebAuthn structure holds anywhere in the attestation object, its statement and extensions included: indefinite lengths, tags, floats, a map key that repeats or is neither an integer nor text.
|
|
10
|
+
export function decodeCbor(bytes: Uint8Array, offset = 0): [CborValue, number] {
|
|
11
|
+
return item(bytes, offset, 0)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function item(bytes: Uint8Array, offset: number, depth: number): [CborValue, number] {
|
|
15
|
+
if (depth > MAX_DEPTH)
|
|
16
|
+
throw new Error('CBOR nested too deep')
|
|
17
|
+
|
|
18
|
+
const initial = byteAt(bytes, offset)
|
|
19
|
+
const major = initial >> 5
|
|
20
|
+
const info = initial & 0x1F
|
|
21
|
+
|
|
22
|
+
if (major === 7) {
|
|
23
|
+
if (!SIMPLE.has(info))
|
|
24
|
+
throw new Error('unsupported CBOR simple value')
|
|
25
|
+
|
|
26
|
+
return [SIMPLE.get(info), offset + 1]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const [argument, start] = head(bytes, offset, info)
|
|
30
|
+
|
|
31
|
+
if (major === 0)
|
|
32
|
+
return [argument, start]
|
|
33
|
+
|
|
34
|
+
if (major === 1)
|
|
35
|
+
return [-1 - argument, start]
|
|
36
|
+
|
|
37
|
+
if (major === 2)
|
|
38
|
+
return [bytes.slice(start, within(bytes, start, argument)), start + argument]
|
|
39
|
+
|
|
40
|
+
if (major === 3)
|
|
41
|
+
return [UTF8.decode(bytes.subarray(start, within(bytes, start, argument))), start + argument]
|
|
42
|
+
|
|
43
|
+
// Each entry takes a byte at least, so a count past the bytes left is refused before anything is allocated for it.
|
|
44
|
+
if (major === 4) {
|
|
45
|
+
within(bytes, start, argument)
|
|
46
|
+
|
|
47
|
+
const array: CborValue[] = []
|
|
48
|
+
let next = start
|
|
49
|
+
|
|
50
|
+
for (let index = 0; index < argument; index++) {
|
|
51
|
+
const [value, end] = item(bytes, next, depth + 1)
|
|
52
|
+
|
|
53
|
+
array.push(value)
|
|
54
|
+
next = end
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return [array, next]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (major === 5) {
|
|
61
|
+
within(bytes, start, argument * 2)
|
|
62
|
+
|
|
63
|
+
const map: CborMap = new Map()
|
|
64
|
+
let next = start
|
|
65
|
+
|
|
66
|
+
for (let index = 0; index < argument; index++) {
|
|
67
|
+
const [key, keyEnd] = item(bytes, next, depth + 1)
|
|
68
|
+
|
|
69
|
+
if ((typeof key !== 'number' && typeof key !== 'string') || map.has(key))
|
|
70
|
+
throw new Error('invalid CBOR map key')
|
|
71
|
+
|
|
72
|
+
const [value, end] = item(bytes, keyEnd, depth + 1)
|
|
73
|
+
|
|
74
|
+
map.set(key, value)
|
|
75
|
+
next = end
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return [map, next]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
throw new Error('unsupported CBOR tag')
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The integer a head carries, as a length or a value, and the offset past it.
|
|
85
|
+
function head(bytes: Uint8Array, offset: number, info: number): [number, number] {
|
|
86
|
+
if (info < 24)
|
|
87
|
+
return [info, offset + 1]
|
|
88
|
+
|
|
89
|
+
if (info === 24)
|
|
90
|
+
return [byteAt(bytes, offset + 1), offset + 2]
|
|
91
|
+
|
|
92
|
+
if (info === 25)
|
|
93
|
+
return [byteAt(bytes, offset + 1) * 0x100 + byteAt(bytes, offset + 2), offset + 3]
|
|
94
|
+
|
|
95
|
+
if (info === 26)
|
|
96
|
+
return [uint32(bytes, offset + 1), offset + 5]
|
|
97
|
+
|
|
98
|
+
if (info === 27) {
|
|
99
|
+
const high = uint32(bytes, offset + 1)
|
|
100
|
+
|
|
101
|
+
if (high >= 0x200000)
|
|
102
|
+
throw new Error('CBOR integer past 2^53')
|
|
103
|
+
|
|
104
|
+
return [high * 0x100000000 + uint32(bytes, offset + 5), offset + 9]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
throw new Error('unsupported CBOR length')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function uint32(bytes: Uint8Array, offset: number): number {
|
|
111
|
+
return byteAt(bytes, offset) * 0x1000000 + byteAt(bytes, offset + 1) * 0x10000 + byteAt(bytes, offset + 2) * 0x100 + byteAt(bytes, offset + 3)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function byteAt(bytes: Uint8Array, offset: number): number {
|
|
115
|
+
const byte = bytes[offset]
|
|
116
|
+
|
|
117
|
+
if (byte === undefined)
|
|
118
|
+
throw new Error('CBOR cut short')
|
|
119
|
+
|
|
120
|
+
return byte
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Where `length` bytes from `start` end, when `bytes` holds them.
|
|
124
|
+
function within(bytes: Uint8Array, start: number, length: number): number {
|
|
125
|
+
if (length > bytes.length - start)
|
|
126
|
+
throw new Error('CBOR cut short')
|
|
127
|
+
|
|
128
|
+
return start + length
|
|
129
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { ctx } from 'kay'
|
|
3
|
+
import { formatDate } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import Button from '@kayjs/ui/button.kay'
|
|
6
|
+
import Notice from '@kayjs/ui/notice.kay'
|
|
7
|
+
import { claimBanner, currentViewer } from '../viewer'
|
|
8
|
+
|
|
9
|
+
const { auth, request } = currentViewer()
|
|
10
|
+
const features = auth.features()
|
|
11
|
+
const first = claimBanner('deletion')
|
|
12
|
+
---
|
|
13
|
+
// Shown to a user whose account waits out `account.deleteAfter`, with the way to cancel; nothing for anyone else. Read ahead of the render, as the impersonation banner is.
|
|
14
|
+
if first && ctx.deletion then at
|
|
15
|
+
Notice.kayjs-auth(tone="warning" data-deletion-pending)
|
|
16
|
+
.flex.flex-wrap.items-center.justify-between.gap-3
|
|
17
|
+
span {wording('auth.deletion.banner', 'Your account will be deleted on {date}.', { date: formatDate(at, { dateStyle: 'long' }) })}
|
|
18
|
+
form(method="post" action={`${features.prefix}/cancel-deletion`})
|
|
19
|
+
input(type="hidden" name="redirect" value={new URL(request.url).pathname})
|
|
20
|
+
Button(type="submit" label={wording('auth.deletion.cancel', 'Cancel the deletion')})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { ctx } from 'kay'
|
|
3
|
+
import { localize } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import Button from '@kayjs/ui/button.kay'
|
|
6
|
+
import Notice from '@kayjs/ui/notice.kay'
|
|
7
|
+
import { claimBanner, currentViewer } from '../viewer'
|
|
8
|
+
|
|
9
|
+
const features = currentViewer().auth.features()
|
|
10
|
+
// Back to the admin pages when they are on, else to the pages' home.
|
|
11
|
+
const back = localize(features.admin && features.pages ? `${features.pages.prefix}/admin` : features.pages?.home ?? '/')
|
|
12
|
+
const first = claimBanner()
|
|
13
|
+
---
|
|
14
|
+
// Shown to an admin acting as a user, with the way back; nothing for anyone else. Read ahead of the render of any page reaching a template that reads `ctx`, so it is in the first bytes.
|
|
15
|
+
if first && ctx.impersonator && ctx.user then found
|
|
16
|
+
Notice.kayjs-auth(tone="warning" data-impersonating)
|
|
17
|
+
.flex.flex-wrap.items-center.justify-between.gap-3
|
|
18
|
+
span {wording('auth.impersonation.banner', 'You are acting as {user}.', { user: found.username ?? found.email ?? found.id })}
|
|
19
|
+
form(method="post" action={`${features.prefix}/stop-impersonating`})
|
|
20
|
+
input(type="hidden" name="redirect" value={back})
|
|
21
|
+
Button(type="submit" label={wording('auth.impersonation.stop', 'Back to my account')})
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { ctx } from 'kay'
|
|
3
|
+
|
|
4
|
+
const { role } = props<{ role?: string | undefined }>()
|
|
5
|
+
---
|
|
6
|
+
// A guest is no signed-in user. `ctx.user` and `ctx.can` are read ahead of the render of each page reaching this component, so it shows in the first bytes.
|
|
7
|
+
if ctx.user && (role === undefined || ctx.can(role))
|
|
8
|
+
slot
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { ctx } from 'kay'
|
|
3
|
+
import { localize } from 'kay/i18n'
|
|
4
|
+
import { wording } from 'kay/words'
|
|
5
|
+
import Button from '@kayjs/ui/button.kay'
|
|
6
|
+
import { currentViewer } from '../viewer'
|
|
7
|
+
|
|
8
|
+
// `account={false}` leaves out the link to the account page, as the account page itself does.
|
|
9
|
+
const { account = true } = props<{ account?: boolean }>()
|
|
10
|
+
const features = currentViewer().auth.features()
|
|
11
|
+
const accountPage = features.pages ? localize(`${features.pages.prefix}/account`) : null
|
|
12
|
+
const home = localize(features.pages?.home ?? '/')
|
|
13
|
+
---
|
|
14
|
+
if ctx.user then found
|
|
15
|
+
.kayjs-auth.flex.flex-wrap.items-center.gap-3
|
|
16
|
+
span.text-muted {found.username ?? found.email}
|
|
17
|
+
if account && accountPage
|
|
18
|
+
a(href={accountPage}) {wording('auth.account.title', 'Your account')}
|
|
19
|
+
form(method="post" action={`${features.prefix}/sign-out`})
|
|
20
|
+
input(type="hidden" name="redirect" value={home})
|
|
21
|
+
Button(type="submit" label={wording('auth.account.signOut', 'Sign out')})
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { defineContext } from 'kay/library'
|
|
2
|
+
import { viewerValues } from './viewer'
|
|
3
|
+
|
|
4
|
+
// The request values kayjs-auth gives every template as `ctx`, `ctx.user` and `ctx.can(role)`, read ahead of the pages reaching a template that reads `ctx`.
|
|
5
|
+
export default defineContext(viewerValues)
|
package/src/core.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import type { Guard } from 'kay'
|
|
2
|
+
import type { Kv, Maintenance } from 'kay/library'
|
|
3
|
+
import type { SessionSettings } from 'kay/server'
|
|
4
|
+
import type { Access } from './access'
|
|
5
|
+
import type { CaptchaOptions } from './captcha'
|
|
6
|
+
import type { EmailCore, PendingCode } from './email'
|
|
7
|
+
import type { AuthEventInfo } from './events'
|
|
8
|
+
import type { AuthFailed, AuthFailure } from './failure'
|
|
9
|
+
import type { ForwardAuthCore } from './forwardAuth'
|
|
10
|
+
import type { Keys } from './keys'
|
|
11
|
+
import type { ResolvedLimits } from './limits'
|
|
12
|
+
import type { createOAuthClient } from './oauth/client'
|
|
13
|
+
import type { OAuthFlow } from './oauth/flow'
|
|
14
|
+
import type { createTokenRefresher, StoredCredential } from './oauth/refresher'
|
|
15
|
+
import type { OAuthProvider, OAuthUser } from './oauth/types'
|
|
16
|
+
import type { OrganizationsCore } from './organizationsCore'
|
|
17
|
+
import type { ProfileOptions, RememberMode } from './pages'
|
|
18
|
+
import type { PasskeyChallenge, Relying } from './passkeys'
|
|
19
|
+
import type { Challenge } from './proof'
|
|
20
|
+
import type { AuthLimiters } from './rates'
|
|
21
|
+
import type { ResolvedRules } from './rules'
|
|
22
|
+
import type { AuthStore, IdentityRecord, UserRecord } from './store'
|
|
23
|
+
import type { Requirement } from './types'
|
|
24
|
+
import type { UserDataRules } from './userData'
|
|
25
|
+
import { localPath } from 'kay'
|
|
26
|
+
import { failed } from './failure'
|
|
27
|
+
|
|
28
|
+
// A visit or a token use is recorded at most this often, so a busy browser or device does not write on every request.
|
|
29
|
+
export const TOUCH_EVERY_MS = 5 * 60 * 1000
|
|
30
|
+
|
|
31
|
+
// What kayjs-auth keeps in the app's kay session: the app's session type includes it, `session<AuthSessionData & { cart: Cart }>(...)`.
|
|
32
|
+
export interface AuthSessionData {
|
|
33
|
+
auth?: SignedIn
|
|
34
|
+
authOAuth?: OAuthFlow
|
|
35
|
+
authChallenge?: Challenge
|
|
36
|
+
authEmailCode?: PendingCode
|
|
37
|
+
authPasskey?: PasskeyChallenge
|
|
38
|
+
// Whether the sign-in under way asked to be remembered, from the call that started it.
|
|
39
|
+
authRemember?: boolean
|
|
40
|
+
// The account this browser registered, or proved the password of while its address was unverified: it may have the link resent and verify it without signing in. Gone once a later registration replaced it.
|
|
41
|
+
authRegistered?: string
|
|
42
|
+
// The address this browser registered while another account held it, for the resend button to answer as it does a new account's, mailing no one.
|
|
43
|
+
authHeld?: string
|
|
44
|
+
// The page the visitor who registered, or signed in unverified, asked for: the verification mail a resend sends leads back to it.
|
|
45
|
+
authBack?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SignedIn {
|
|
49
|
+
user: string
|
|
50
|
+
// The user's session version when they signed in: raising it signs this session out.
|
|
51
|
+
version: number
|
|
52
|
+
// When they signed in or last re-proved who they are, in milliseconds since the epoch.
|
|
53
|
+
at: number
|
|
54
|
+
// The sign-in's id in the store's sessions; missing from one signed in before kayjs-auth kept them, which gets one on its next request.
|
|
55
|
+
session?: string
|
|
56
|
+
// Set when a passkey signed in, which counts as two factors for `twoFactor.require`.
|
|
57
|
+
passkey?: boolean
|
|
58
|
+
// The admin's own sign-in while they act as this user, put back when they stop.
|
|
59
|
+
impersonator?: Omit<SignedIn, 'impersonator' | 'until'>
|
|
60
|
+
// When an impersonation ends whatever the activity, in milliseconds since the epoch.
|
|
61
|
+
until?: number
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// An OAuth app registered at a provider: `{ provider: discordProvider(), clientId, clientSecret }`.
|
|
65
|
+
export interface OAuthApp {
|
|
66
|
+
// eslint-disable-next-line ts/no-explicit-any -- an app's provider written inline reads its own user answer untyped
|
|
67
|
+
provider: OAuthProvider<any, OAuthUser>
|
|
68
|
+
clientId: string
|
|
69
|
+
// Left out for Apple, whose preset signs its own, or a public client.
|
|
70
|
+
clientSecret?: string | undefined
|
|
71
|
+
// What a sign-in button calls the provider, over the preset's own label.
|
|
72
|
+
label?: string | undefined
|
|
73
|
+
// Keeps the provider's access and refresh tokens, encrypted, for `providerToken()`; false by default, so a sign-in that only needs the profile stores no token.
|
|
74
|
+
keepTokens?: boolean | undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// A provider account as kayjs-auth reads it; `email` is set only when the provider says it is verified.
|
|
78
|
+
export interface Profile extends OAuthUser {
|
|
79
|
+
provider: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A user as the app sees them, secrets left out, without what costs a read: the user guards, gates, results and admin lists work with. `data` is what the app keeps through the `user` option, `{}` without one.
|
|
83
|
+
export interface BareUser<Data extends object = Record<string, unknown>> {
|
|
84
|
+
id: string
|
|
85
|
+
email: string | null
|
|
86
|
+
emailVerified: boolean
|
|
87
|
+
username: string | null
|
|
88
|
+
roles: string[]
|
|
89
|
+
hasPassword: boolean
|
|
90
|
+
twoFactor: boolean
|
|
91
|
+
locale: string | null
|
|
92
|
+
guest: boolean
|
|
93
|
+
data: Data
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// A user with the provider accounts linked to them, `providers` holding the id each gave: what `auth.user()`, `requireUser()` and the app's hooks (`onSignUp`, `onProviderProfile`, `beforeDelete`, `onExport`, `admin`, `onEvent`) receive.
|
|
97
|
+
export interface User<Data extends object = Record<string, unknown>> extends BareUser<Data> {
|
|
98
|
+
providers: Record<string, { id: string }>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// What a sign-in or account method answers when its feature is switched off.
|
|
102
|
+
export type Off = AuthFailed<'off'>
|
|
103
|
+
|
|
104
|
+
export const OFF: Off = Object.freeze(failed('off'))
|
|
105
|
+
|
|
106
|
+
// The features `auth({ password, passkeys, magicLink, twoFactor })` switch, on unless set to false.
|
|
107
|
+
export interface Switches {
|
|
108
|
+
password: boolean
|
|
109
|
+
passkeys: boolean
|
|
110
|
+
magicLink: boolean
|
|
111
|
+
twoFactor: boolean
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// What every request's `Auth` shares.
|
|
115
|
+
export interface Core {
|
|
116
|
+
store: AuthStore
|
|
117
|
+
// Where attempts are counted: the store's `rates`, else the app's `store.rates`, else this process's memory.
|
|
118
|
+
rates: AuthLimiters
|
|
119
|
+
keys: Keys
|
|
120
|
+
// The site's public origin: `site` of authCore, else the site kay.config.ts or SITE_URL sets.
|
|
121
|
+
url?: string | undefined
|
|
122
|
+
// Builds what depends on the site's origin, throwing while OAuth sign-in or mailed links lack one; a server asks as it boots and on each request until it has one.
|
|
123
|
+
settle: () => void
|
|
124
|
+
prefix: string
|
|
125
|
+
signInPage: string
|
|
126
|
+
// The `signInPage` the app gave, which a guard sends visitors to even without the prebuilt pages.
|
|
127
|
+
ownSignInPage?: string | undefined
|
|
128
|
+
twoFactorPage: string
|
|
129
|
+
// The prebuilt pages' prefix and home, with the `pages` option.
|
|
130
|
+
pages?: { prefix: string, home: string, remember: RememberMode, main: boolean, privacy?: string | undefined, purpose?: string | undefined, profile?: ProfileOptions | undefined } | undefined
|
|
131
|
+
// The site's name, as mails and authenticators show it.
|
|
132
|
+
name: string
|
|
133
|
+
limits: ResolvedLimits
|
|
134
|
+
// Seconds a session signed in with `remember: true` lives unseen.
|
|
135
|
+
rememberMaxAge: number
|
|
136
|
+
// The password and username rules, the defaults over the app's `rules`.
|
|
137
|
+
rules: ResolvedRules
|
|
138
|
+
// The app's page listing the user's sessions, linked from new sign-in mails.
|
|
139
|
+
sessionsPage?: string | undefined
|
|
140
|
+
// The app's page setting up two-factor, where `forwardAuth` sends a user without it.
|
|
141
|
+
securityPage?: string | undefined
|
|
142
|
+
// Where an address is, such as a city, shown beside each session.
|
|
143
|
+
locate?: ((ip: string) => string | null | undefined | Promise<string | null | undefined>) | undefined
|
|
144
|
+
// The scopes a bearer token may be given.
|
|
145
|
+
scopes: Set<string>
|
|
146
|
+
switches: Switches
|
|
147
|
+
// Who passkeys are made for, from `url`; none without it.
|
|
148
|
+
passkeys?: Relying | undefined
|
|
149
|
+
captcha?: CaptchaOptions | undefined
|
|
150
|
+
// Whether a new password is known from a breach.
|
|
151
|
+
breached?: ((password: string) => boolean | Promise<boolean>) | undefined
|
|
152
|
+
email?: EmailCore | undefined
|
|
153
|
+
// Set when the app is an OAuth server for MCP clients: the prebuilt pages and a store keeping its data are there.
|
|
154
|
+
oauthServer?: { fetchDocument: (url: string) => Promise<unknown>, documents: Kv, defaultScopes: string[] } | undefined
|
|
155
|
+
// Set with the `forwardAuth` option: the domains a sign-in may return to, and whether two-factor is needed to pass.
|
|
156
|
+
forward?: ForwardAuthCore | undefined
|
|
157
|
+
oauth: Map<string, { app: OAuthApp, client: ReturnType<typeof createOAuthClient<never, OAuthUser>> }>
|
|
158
|
+
refreshers: Map<string, ReturnType<typeof createTokenRefresher<string, RefreshSubject>>>
|
|
159
|
+
allow?: ((profile: Profile) => boolean | Promise<boolean>) | undefined
|
|
160
|
+
// Whether visitors make their own accounts; `createUser` makes them otherwise.
|
|
161
|
+
signUp: boolean
|
|
162
|
+
defaultRoles: string[]
|
|
163
|
+
// The roles the app declares; any role goes without them.
|
|
164
|
+
roles?: Set<string> | undefined
|
|
165
|
+
// The same roles with the ones each includes, for `userIdsHolding`.
|
|
166
|
+
hierarchy?: Record<string, string[]> | undefined
|
|
167
|
+
// Who must have two-factor on, the `twoFactor.require` option: guards, gates, the admin and the OAuth consent send or refuse a user it names who lacks it.
|
|
168
|
+
twoFactorRequire?: Requirement<BareUser, string> | undefined
|
|
169
|
+
// Whether a user meets the `admin` option; nobody does without it.
|
|
170
|
+
admin?: ((user: User) => Promise<boolean>) | undefined
|
|
171
|
+
// Whether a user meets a requirement, through the app's `authorize` when it has one, as `guard()` and `requireUser({ require })` ask.
|
|
172
|
+
access: Access<string>
|
|
173
|
+
// `auth.guard()` of this `authCore`, which the `guard()` of a module-less `auth()` asks for through the request's `auth`.
|
|
174
|
+
guard: (options: { redirect?: string | undefined, require?: Requirement<BareUser, string> | undefined, scope?: string | string[] | false | undefined, twoFactor?: true | undefined }) => Guard
|
|
175
|
+
onSignUp?: ((user: User, profile: Profile | undefined) => void | Promise<void>) | undefined
|
|
176
|
+
// The data to keep on the user, laid over what they hold; nothing keeps it as is.
|
|
177
|
+
onProviderProfile?: ((user: User, profile: Profile) => unknown) | undefined
|
|
178
|
+
userData?: UserDataRules | undefined
|
|
179
|
+
// Asked before the erase, with the user and who acts when it is not them: a refusal keeps the account.
|
|
180
|
+
beforeDelete?: ((user: User, actor: User | null) => Promise<AuthFailure<'refused'> | undefined>) | undefined
|
|
181
|
+
|
|
182
|
+
// Seconds a guest is kept once their sessions are gone.
|
|
183
|
+
guestMaxAge: number
|
|
184
|
+
// Seconds an account registered and never verified is kept, where unverified accounts cannot sign in; for good when undefined.
|
|
185
|
+
unverifiedMaxAge?: number | undefined
|
|
186
|
+
// Set with `account.deleteAfter`: the seconds `account.delete` leaves an account pending before the prune deletes it, and the app's own page the mailed link leads to; without it the deletion is immediate.
|
|
187
|
+
deletion?: { after: number, cancelPage?: string | undefined } | undefined
|
|
188
|
+
onExport?: ((user: User) => unknown) | undefined
|
|
189
|
+
onEvent?: ((info: AuthEventInfo) => unknown) | undefined
|
|
190
|
+
|
|
191
|
+
organizations?: OrganizationsCore | undefined
|
|
192
|
+
// Kept with the `audit` option: entries live `maxAge` seconds.
|
|
193
|
+
audit?: { maxAge: number, prunedAt: number, ip: 'full' | 'truncated', sweep?: Maintenance | undefined } | undefined
|
|
194
|
+
// When the store last pruned what expired.
|
|
195
|
+
pruned: { at: number, settings?: SessionSettings | undefined, sweep?: Maintenance | undefined }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// What a provider token refresh works on: the identity, and its tokens opened.
|
|
199
|
+
export interface RefreshSubject {
|
|
200
|
+
identity: IdentityRecord
|
|
201
|
+
credential: StoredCredential
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function publicUser(record: UserRecord): BareUser {
|
|
205
|
+
return {
|
|
206
|
+
id: record.id,
|
|
207
|
+
email: record.email,
|
|
208
|
+
emailVerified: record.emailVerifiedAt !== null,
|
|
209
|
+
username: record.username,
|
|
210
|
+
roles: [...record.roles],
|
|
211
|
+
hasPassword: record.passwordHash !== null,
|
|
212
|
+
twoFactor: record.totpEnabledAt !== null,
|
|
213
|
+
locale: record.locale,
|
|
214
|
+
guest: record.guest,
|
|
215
|
+
data: structuredClone(record.data ?? {}),
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// The provider accounts linked to a user, by provider; read only where the app set up OAuth, as no user has one otherwise.
|
|
220
|
+
// A user who has no provider linked yet, as a new account is.
|
|
221
|
+
export function unlinked(user: BareUser): User {
|
|
222
|
+
return { ...user, providers: {} }
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// The user with their linked providers read, for what hands one to the app's hooks.
|
|
226
|
+
export async function linkedUser(core: Pick<Core, 'oauth' | 'store'>, record: UserRecord): Promise<User> {
|
|
227
|
+
return { ...publicUser(record), providers: await linkedProviders(core, record.id) }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export async function linkedProviders(core: Pick<Core, 'oauth' | 'store'>, userId: string): Promise<User['providers']> {
|
|
231
|
+
if (core.oauth.size === 0)
|
|
232
|
+
return {}
|
|
233
|
+
|
|
234
|
+
return Object.fromEntries((await core.store.identities.listByUser(userId)).map(identity => [identity.provider, { id: identity.providerId }]))
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function newUserRecord(fields: Partial<UserRecord>): UserRecord {
|
|
238
|
+
return {
|
|
239
|
+
id: crypto.randomUUID(),
|
|
240
|
+
email: null,
|
|
241
|
+
emailVerifiedAt: null,
|
|
242
|
+
username: null,
|
|
243
|
+
passwordHash: null,
|
|
244
|
+
totpSecret: null,
|
|
245
|
+
totpEnabledAt: null,
|
|
246
|
+
totpLastStep: 0,
|
|
247
|
+
roles: [],
|
|
248
|
+
sessionVersion: 0,
|
|
249
|
+
lastSignIn: null,
|
|
250
|
+
locale: null,
|
|
251
|
+
guest: false,
|
|
252
|
+
bannedAt: null,
|
|
253
|
+
bannedUntil: null,
|
|
254
|
+
banReason: null,
|
|
255
|
+
deleteAt: null,
|
|
256
|
+
data: {},
|
|
257
|
+
createdAt: new Date(),
|
|
258
|
+
...fields,
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Where a flow lands when nothing asked for another page: the prebuilt pages' `home`, else `/`.
|
|
263
|
+
export function homeOf(core: Pick<Core, 'pages'>): string {
|
|
264
|
+
return core.pages?.home ?? '/'
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function withQuery(path: string, query: string): string {
|
|
268
|
+
return `${path}${path.includes('?') ? '&' : '?'}${query}`
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// The sign-in page, or any page, with where to come back to once the visitor is in: `?redirect=`.
|
|
272
|
+
export function withBack(path: string, back: string): string {
|
|
273
|
+
return withQuery(path, `redirect=${encodeURIComponent(back)}`)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// The page a link or a form was given to lead back to: a path of the site other than the pages' home, which a bare link leads to anyway; null for none.
|
|
277
|
+
export function backOf(core: Pick<Core, 'pages'>, redirect: string | null | undefined): string | null {
|
|
278
|
+
const home = homeOf(core)
|
|
279
|
+
const path = localPath(redirect, home)
|
|
280
|
+
|
|
281
|
+
return path === home ? null : path
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Whether `request` may be a page opening, which is redirected to sign in: a GET or HEAD that is not clearly something else. A fetch (a `Sec-Fetch-Mode` other than a navigation) and a client asking for neither HTML nor anything (`Accept: application/json`) keep their 401; a request saying nothing, as a test's or curl's, is taken for a page.
|
|
285
|
+
export function isNavigation(request: Request): boolean {
|
|
286
|
+
if (request.method !== 'GET' && request.method !== 'HEAD')
|
|
287
|
+
return false
|
|
288
|
+
|
|
289
|
+
const mode = request.headers.get('sec-fetch-mode')
|
|
290
|
+
const accept = request.headers.get('accept')
|
|
291
|
+
|
|
292
|
+
if (mode !== null && mode !== 'navigate' && mode !== 'nested-navigate')
|
|
293
|
+
return false
|
|
294
|
+
|
|
295
|
+
return accept === null || accept.includes('text/html') || accept.includes('*/*')
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Where a guard sends an anonymous visitor: the prebuilt sign-in page, else the app's own `signInPage`, else nowhere and the visitor gets a 401.
|
|
299
|
+
export function guardPageOf(core: Pick<Core, 'pages' | 'signInPage' | 'ownSignInPage'>): string | undefined {
|
|
300
|
+
return core.pages ? core.signInPage : core.ownSignInPage
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Whether an admin bars `record` from signing in at this moment.
|
|
304
|
+
export function banned(record: UserRecord, now = Date.now()): boolean {
|
|
305
|
+
return record.bannedAt !== null && (record.bannedUntil === null || record.bannedUntil.getTime() > now)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export { sameText } from 'kay/library'
|