@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
package/src/admin.ts
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import type { BareUser, SignedIn } from './core'
|
|
2
|
+
import type { AuditCursorInput, AuditEntry } from './events'
|
|
3
|
+
import type { AuthFailed } from './failure'
|
|
4
|
+
import type { RequestState } from './state'
|
|
5
|
+
import type { UserRecord } from './store'
|
|
6
|
+
import { clamp } from '@kaynooo/utils'
|
|
7
|
+
import { banned, linkedUser, publicUser } from './core'
|
|
8
|
+
import { eraseLeavingUser } from './erase'
|
|
9
|
+
import { auditEntries, report } from './events'
|
|
10
|
+
import { failed } from './failure'
|
|
11
|
+
import { provedLately } from './proof'
|
|
12
|
+
import { lacksTwoFactor } from './requiredTwoFactor'
|
|
13
|
+
import { securityNotice } from './securityNotice'
|
|
14
|
+
import { openSession, revokeBearerTokens } from './sessions'
|
|
15
|
+
|
|
16
|
+
// An impersonation ends this long after it starts, however busy it is.
|
|
17
|
+
const IMPERSONATION_MAX_AGE_MS = 30 * 60_000
|
|
18
|
+
const USER_PAGE_MAX = 200
|
|
19
|
+
const BAN_REASON_MAX_LENGTH = 500
|
|
20
|
+
|
|
21
|
+
// A user as an admin sees them.
|
|
22
|
+
export interface ManagedUser extends BareUser {
|
|
23
|
+
createdAt: Date
|
|
24
|
+
// Set while the user is barred from signing in, `until` null for good.
|
|
25
|
+
ban: { bannedAt: Date, until: Date | null, reason: string | null } | null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// `forbidden` when the user asking does not meet the `admin` option, acts as someone else or came with a bearer token, `two-factor-required` when `twoFactor.require` names them and they have not turned it on.
|
|
29
|
+
export type AdminFailure = 'forbidden' | 'not-found' | 'two-factor-required'
|
|
30
|
+
|
|
31
|
+
// `stale` when the admin's session neither signed in nor re-proved in the last ten minutes.
|
|
32
|
+
export type AdminChangeFailure = AdminFailure | 'stale'
|
|
33
|
+
|
|
34
|
+
export type RoleResult = { ok: true, user: ManagedUser } | AuthFailed<AdminChangeFailure | 'unknown-role' | 'self' | 'last-admin'>
|
|
35
|
+
|
|
36
|
+
export type BanResult = { ok: true, user: ManagedUser } | AuthFailed<AdminChangeFailure | 'self' | 'admin' | 'until'>
|
|
37
|
+
|
|
38
|
+
export type UnbanResult = { ok: true, user: ManagedUser } | AuthFailed<AdminChangeFailure>
|
|
39
|
+
|
|
40
|
+
export type DeleteResult = { ok: true } | AuthFailed<AdminChangeFailure | 'self' | 'admin' | 'refused'>
|
|
41
|
+
|
|
42
|
+
export function managedUser(record: UserRecord): ManagedUser {
|
|
43
|
+
return {
|
|
44
|
+
...publicUser(record),
|
|
45
|
+
createdAt: record.createdAt,
|
|
46
|
+
ban: banned(record) ? { bannedAt: record.bannedAt!, until: record.bannedUntil, reason: record.banReason } : null,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// The user asking, when they meet the `admin` option as themselves, signed in on this session: a bearer token, however scoped, manages no one.
|
|
51
|
+
export async function actingAdmin(state: RequestState): Promise<UserRecord | null> {
|
|
52
|
+
const record = await state.current()
|
|
53
|
+
|
|
54
|
+
if (!record || state.via() !== 'session' || (await state.session.get('auth'))?.impersonator)
|
|
55
|
+
return null
|
|
56
|
+
|
|
57
|
+
return await isAdmin(state, record) ? record : null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The admin changing a user, who must have signed in or re-proved lately, so a session left open or stolen grants, bans and deletes no one.
|
|
61
|
+
export async function changingAdmin(state: RequestState): Promise<{ ok: true, actor: UserRecord } | AuthFailed<'forbidden' | 'two-factor-required' | 'stale'>> {
|
|
62
|
+
const acting = await askingAdmin(state)
|
|
63
|
+
|
|
64
|
+
if (!acting.ok)
|
|
65
|
+
return acting
|
|
66
|
+
|
|
67
|
+
return await provedLately(state) ? acting : failed('stale')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The admin as `actingAdmin` finds them, or why not: an admin whom `twoFactor.require` names and who lacks it is held back until they turn it on.
|
|
71
|
+
export async function askingAdmin(state: RequestState): Promise<{ ok: true, actor: UserRecord } | AuthFailed<'forbidden' | 'two-factor-required'>> {
|
|
72
|
+
const actor = await actingAdmin(state)
|
|
73
|
+
|
|
74
|
+
if (!actor)
|
|
75
|
+
return failed('forbidden')
|
|
76
|
+
|
|
77
|
+
return await lacksTwoFactor(state, publicUser(actor)) ? failed('two-factor-required') : { ok: true, actor }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function isAdmin(state: RequestState, record: UserRecord): Promise<boolean> {
|
|
81
|
+
return await state.core.admin?.(await linkedUser(state.core, record)) ?? false
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Whether `record` is the only user meeting the `admin` option, so losing them leaves the deployment with no admin.
|
|
85
|
+
export async function lastAdmin(state: RequestState, record: UserRecord): Promise<boolean> {
|
|
86
|
+
return await isAdmin(state, record) && !await anotherAdmin(state, record.id)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Strips an admin's roles before their account is deleted, which cannot be undone: two admins deleting themselves together each see the other still there, so when none is left the roles go back. Returns `restore` for a deletion that then fails, or undefined for the last admin.
|
|
90
|
+
export async function stepAside(state: RequestState, record: UserRecord): Promise<{ restore: () => Promise<void> } | undefined> {
|
|
91
|
+
const { users } = state.core.store
|
|
92
|
+
const stripped = record.roles.length > 0 && await isAdmin(state, record)
|
|
93
|
+
const restore = async () => {
|
|
94
|
+
if (!stripped)
|
|
95
|
+
return
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
await users.update(record.id, { roles: record.roles })
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// The account is gone: nothing is left to restore.
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!stripped)
|
|
106
|
+
return { restore }
|
|
107
|
+
|
|
108
|
+
await users.update(record.id, { roles: [] })
|
|
109
|
+
|
|
110
|
+
if (await anotherAdmin(state, ''))
|
|
111
|
+
return { restore }
|
|
112
|
+
|
|
113
|
+
await restore()
|
|
114
|
+
|
|
115
|
+
return undefined
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Read a page at a time and stopped at the first admin other than `exceptId`; the total is never read, so the store need not count.
|
|
119
|
+
async function anotherAdmin(state: RequestState, exceptId: string): Promise<boolean> {
|
|
120
|
+
for (let offset = 0; ; offset += USER_PAGE_MAX) {
|
|
121
|
+
const { users } = await state.core.store.users.list({ offset, limit: USER_PAGE_MAX, total: false })
|
|
122
|
+
|
|
123
|
+
for (const user of users) {
|
|
124
|
+
if (user.id !== exceptId && await isAdmin(state, user))
|
|
125
|
+
return true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (users.length < USER_PAGE_MAX)
|
|
129
|
+
return false
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export type ListUsersResult = { ok: true, users: ManagedUser[], total: number } | AuthFailed<'forbidden' | 'two-factor-required'>
|
|
134
|
+
|
|
135
|
+
// Newest first, a page of `limit` (50 by default) from `offset`.
|
|
136
|
+
export async function listUsers(state: RequestState, query: { search?: string | undefined, offset?: number | undefined, limit?: number | undefined }): Promise<ListUsersResult> {
|
|
137
|
+
const acting = await askingAdmin(state)
|
|
138
|
+
|
|
139
|
+
if (!acting.ok)
|
|
140
|
+
return acting
|
|
141
|
+
|
|
142
|
+
const search = query.search?.trim()
|
|
143
|
+
const found = await state.core.store.users.list({
|
|
144
|
+
...search === undefined || search === '' ? {} : { search },
|
|
145
|
+
offset: Math.max(Math.trunc(query.offset ?? 0), 0),
|
|
146
|
+
limit: clamp(Math.trunc(query.limit ?? 50), 1, USER_PAGE_MAX),
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
return { ok: true, users: found.users.map(managedUser), total: found.total }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export type GetUserResult = { ok: true, user: ManagedUser, admin: boolean } | AuthFailed<AdminFailure>
|
|
153
|
+
|
|
154
|
+
export async function getUser(state: RequestState, userId: string): Promise<GetUserResult> {
|
|
155
|
+
const acting = await askingAdmin(state)
|
|
156
|
+
|
|
157
|
+
if (!acting.ok)
|
|
158
|
+
return acting
|
|
159
|
+
|
|
160
|
+
const record = await state.core.store.users.get(userId)
|
|
161
|
+
|
|
162
|
+
return record ? { ok: true, user: managedUser(record), admin: await isAdmin(state, record) } : failed('not-found')
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Takes effect on the user's next request, roles being read with the user each time.
|
|
166
|
+
export async function changeRole(state: RequestState, userId: string, role: string, granted: boolean): Promise<RoleResult> {
|
|
167
|
+
const acting = await changingAdmin(state)
|
|
168
|
+
|
|
169
|
+
if (!acting.ok)
|
|
170
|
+
return acting
|
|
171
|
+
|
|
172
|
+
const { actor } = acting
|
|
173
|
+
|
|
174
|
+
if (state.core.roles && !state.core.roles.has(role))
|
|
175
|
+
return failed('unknown-role')
|
|
176
|
+
|
|
177
|
+
const record = await state.core.store.users.get(userId)
|
|
178
|
+
|
|
179
|
+
if (!record)
|
|
180
|
+
return failed('not-found')
|
|
181
|
+
|
|
182
|
+
if (record.roles.includes(role) === granted)
|
|
183
|
+
return { ok: true, user: managedUser(record) }
|
|
184
|
+
|
|
185
|
+
const roles = granted ? [...record.roles, role] : record.roles.filter(held => held !== role)
|
|
186
|
+
|
|
187
|
+
if (record.id === actor.id && !(await isAdmin(state, { ...record, roles })))
|
|
188
|
+
return failed('self')
|
|
189
|
+
|
|
190
|
+
const updated = await state.core.store.users.update(record.id, { roles }) ?? { ...record, roles }
|
|
191
|
+
|
|
192
|
+
// Two admins revoking each other both see the other still there: when none is left, the role goes back.
|
|
193
|
+
if (!granted && await isAdmin(state, record) && !await isAdmin(state, updated) && !await anotherAdmin(state, '')) {
|
|
194
|
+
await state.core.store.users.update(record.id, { roles: record.roles })
|
|
195
|
+
|
|
196
|
+
return failed('last-admin')
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
await report(state, updated, granted ? 'role-granted' : 'role-revoked', role, actor)
|
|
200
|
+
|
|
201
|
+
return { ok: true, user: managedUser(updated) }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Bars the user from signing in, for good or `until` then, and signs them out everywhere, devices included. Another admin is refused: revoke their role first.
|
|
205
|
+
export async function banUser(state: RequestState, userId: string, options: { reason?: string | undefined, until?: Date | undefined }): Promise<BanResult> {
|
|
206
|
+
const acting = await changingAdmin(state)
|
|
207
|
+
|
|
208
|
+
if (!acting.ok)
|
|
209
|
+
return acting
|
|
210
|
+
|
|
211
|
+
const { actor } = acting
|
|
212
|
+
|
|
213
|
+
if (options.until !== undefined && !(options.until.getTime() > Date.now()))
|
|
214
|
+
return failed('until')
|
|
215
|
+
|
|
216
|
+
const record = await state.core.store.users.get(userId)
|
|
217
|
+
|
|
218
|
+
if (!record)
|
|
219
|
+
return failed('not-found')
|
|
220
|
+
|
|
221
|
+
if (record.id === actor.id)
|
|
222
|
+
return failed('self')
|
|
223
|
+
|
|
224
|
+
if (await isAdmin(state, record))
|
|
225
|
+
return failed('admin')
|
|
226
|
+
|
|
227
|
+
const given = options.reason?.trim().slice(0, BAN_REASON_MAX_LENGTH)
|
|
228
|
+
const reason = given === undefined || given === '' ? null : given
|
|
229
|
+
const patch = { bannedAt: new Date(), bannedUntil: options.until ?? null, banReason: reason, sessionVersion: record.sessionVersion + 1 }
|
|
230
|
+
const updated = await state.core.store.users.update(record.id, patch) ?? { ...record, ...patch }
|
|
231
|
+
|
|
232
|
+
await revokeBearerTokens(state.core, record.id)
|
|
233
|
+
await report(state, updated, 'banned', reason ?? undefined, actor)
|
|
234
|
+
|
|
235
|
+
return { ok: true, user: managedUser(updated) }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export async function unbanUser(state: RequestState, userId: string): Promise<UnbanResult> {
|
|
239
|
+
const acting = await changingAdmin(state)
|
|
240
|
+
|
|
241
|
+
if (!acting.ok)
|
|
242
|
+
return acting
|
|
243
|
+
|
|
244
|
+
const { actor } = acting
|
|
245
|
+
|
|
246
|
+
const record = await state.core.store.users.get(userId)
|
|
247
|
+
|
|
248
|
+
if (!record)
|
|
249
|
+
return failed('not-found')
|
|
250
|
+
|
|
251
|
+
if (record.bannedAt === null)
|
|
252
|
+
return { ok: true, user: managedUser(record) }
|
|
253
|
+
|
|
254
|
+
const patch = { bannedAt: null, bannedUntil: null, banReason: null }
|
|
255
|
+
const updated = await state.core.store.users.update(record.id, patch) ?? { ...record, ...patch }
|
|
256
|
+
|
|
257
|
+
await report(state, updated, 'unbanned', undefined, actor)
|
|
258
|
+
|
|
259
|
+
return { ok: true, user: managedUser(updated) }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Deletes the user with everything kayjs-auth keeps for them, asks `beforeDelete`, calls `onDelete` and mails them the account-deleted notice. Another admin is refused: revoke their role first. An organization the user solely owns with other members passes to its longest-standing member, its new owner told, so an abandoned account is never a dead end.
|
|
263
|
+
export async function deleteUser(state: RequestState, userId: string): Promise<DeleteResult> {
|
|
264
|
+
const acting = await changingAdmin(state)
|
|
265
|
+
|
|
266
|
+
if (!acting.ok)
|
|
267
|
+
return acting
|
|
268
|
+
|
|
269
|
+
const { actor } = acting
|
|
270
|
+
|
|
271
|
+
const record = await state.core.store.users.get(userId)
|
|
272
|
+
|
|
273
|
+
if (!record)
|
|
274
|
+
return failed('not-found')
|
|
275
|
+
|
|
276
|
+
if (record.id === actor.id)
|
|
277
|
+
return failed('self')
|
|
278
|
+
|
|
279
|
+
return await isAdmin(state, record) ? failed('admin') : eraseLeavingUser(state, record, { actor, handOver: true })
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export type AdminAuditLogResult = { ok: true, entries: AuditEntry[] } | AuthFailed<'forbidden' | 'two-factor-required'>
|
|
283
|
+
|
|
284
|
+
// Everyone's audit log, or one user's, newest first.
|
|
285
|
+
export async function adminAuditLog(state: RequestState, query: { userId?: string | undefined, before?: AuditCursorInput | undefined, limit?: number | undefined }): Promise<AdminAuditLogResult> {
|
|
286
|
+
const acting = await askingAdmin(state)
|
|
287
|
+
|
|
288
|
+
return !acting.ok ? acting : { ok: true, entries: await auditEntries(state.core, query) }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export type ImpersonateResult = { ok: true } | AuthFailed<AdminFailure | 'stale' | 'self' | 'admin' | 'banned'>
|
|
292
|
+
|
|
293
|
+
// Signs this browser in as `userId`, the admin's own sign-in kept for `admin.stopImpersonating`; 'stale' unless the admin signed in or re-proved lately. Nothing that takes a proof works meanwhile, nothing is written to the user's organizations, another admin cannot be impersonated, it ends after 30 minutes, and the user is mailed.
|
|
294
|
+
export async function impersonate(state: RequestState, userId: string): Promise<ImpersonateResult> {
|
|
295
|
+
const acting = await changingAdmin(state)
|
|
296
|
+
const own = await state.session.get('auth')
|
|
297
|
+
|
|
298
|
+
if (!acting.ok || !own)
|
|
299
|
+
return acting.ok ? failed('forbidden') : acting
|
|
300
|
+
|
|
301
|
+
const { actor } = acting
|
|
302
|
+
|
|
303
|
+
const record = await state.core.store.users.get(userId)
|
|
304
|
+
|
|
305
|
+
if (!record)
|
|
306
|
+
return failed('not-found')
|
|
307
|
+
|
|
308
|
+
if (record.id === actor.id)
|
|
309
|
+
return failed('self')
|
|
310
|
+
|
|
311
|
+
if (await isAdmin(state, record))
|
|
312
|
+
return failed('admin')
|
|
313
|
+
|
|
314
|
+
if (banned(record))
|
|
315
|
+
return failed('banned')
|
|
316
|
+
|
|
317
|
+
await state.session.unset('authChallenge')
|
|
318
|
+
await state.session.regenerate()
|
|
319
|
+
|
|
320
|
+
const session = await openSession(state.core, record, state.address, state.userAgent, IMPERSONATION_MAX_AGE_MS / 1000)
|
|
321
|
+
|
|
322
|
+
// Signed in at 0, so nothing asking for a recent sign-in passes.
|
|
323
|
+
await state.session.set('auth', { user: record.id, version: record.sessionVersion, at: 0, session, impersonator: own, until: Date.now() + IMPERSONATION_MAX_AGE_MS })
|
|
324
|
+
state.adopt(record)
|
|
325
|
+
await securityNotice(state, record, 'impersonation-started', undefined, undefined, actor)
|
|
326
|
+
|
|
327
|
+
return { ok: true }
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Signs the admin back in as themselves; signed out when their own sign-in ended meanwhile. False when not impersonating.
|
|
331
|
+
export async function stopImpersonating(state: RequestState): Promise<boolean> {
|
|
332
|
+
const signedIn = await state.session.get('auth')
|
|
333
|
+
|
|
334
|
+
if (!signedIn?.impersonator)
|
|
335
|
+
return false
|
|
336
|
+
|
|
337
|
+
await endImpersonation(state, signedIn)
|
|
338
|
+
await state.reload()
|
|
339
|
+
|
|
340
|
+
return true
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Ends the impersonation `signedIn` holds and puts the admin's own sign-in back, recording the end; `expired` says it ran out rather than the admin ended it.
|
|
344
|
+
export async function endImpersonation(state: RequestState, signedIn: SignedIn, expired = false): Promise<void> {
|
|
345
|
+
const { impersonator } = signedIn
|
|
346
|
+
|
|
347
|
+
if (!impersonator)
|
|
348
|
+
return
|
|
349
|
+
|
|
350
|
+
const record = await state.core.store.users.get(signedIn.user)
|
|
351
|
+
const actor = await state.core.store.users.get(impersonator.user)
|
|
352
|
+
|
|
353
|
+
if (record && actor)
|
|
354
|
+
await report(state, record, 'impersonation-ended', expired ? 'expired' : undefined, actor)
|
|
355
|
+
|
|
356
|
+
if (signedIn.session !== undefined)
|
|
357
|
+
await state.core.store.sessions.delete(signedIn.user, signedIn.session)
|
|
358
|
+
|
|
359
|
+
await state.session.regenerate()
|
|
360
|
+
await state.session.set('auth', impersonator)
|
|
361
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { AuditEntry, AuthEvent } from './events'
|
|
2
|
+
import { language } from './locale'
|
|
3
|
+
|
|
4
|
+
// How one event reads, given the entry's detail: a role, a provider, a passkey's name, how a sign-in happened.
|
|
5
|
+
export type AuditPhrase = string | ((detail: string | null) => string)
|
|
6
|
+
|
|
7
|
+
export type AuditWording = Record<AuthEvent, AuditPhrase>
|
|
8
|
+
|
|
9
|
+
const OAUTH_PREFIX = 'oauth:'
|
|
10
|
+
|
|
11
|
+
// The roles an organization event names after the organization's id, space-separated.
|
|
12
|
+
function orgRoles(detail: string | null): string | null {
|
|
13
|
+
const roles = detail?.split(' ').slice(1).join(', ')
|
|
14
|
+
|
|
15
|
+
return roles === undefined || roles === '' ? null : roles
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Names the detail, as `read` gives it, in the phrase when there is one, else says the plain phrase.
|
|
19
|
+
function naming(named: (detail: string) => string, plain: string, read: (detail: string | null) => string | null = detail => detail == null || detail === '' ? null : detail): AuditPhrase {
|
|
20
|
+
return (detail) => {
|
|
21
|
+
const shown = read(detail)
|
|
22
|
+
|
|
23
|
+
return shown === null ? plain : named(shown)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const ENGLISH_SIGN_INS: Record<string, string> = {
|
|
28
|
+
'password': 'with a password',
|
|
29
|
+
'register': 'on creating the account',
|
|
30
|
+
'reset-password': 'after a password reset',
|
|
31
|
+
'magic-link': 'with a mailed link',
|
|
32
|
+
'email-code': 'with a mailed code',
|
|
33
|
+
'passkey': 'with a passkey',
|
|
34
|
+
'guest': 'as a guest',
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const FRENCH_SIGN_INS: Record<string, string> = {
|
|
38
|
+
'password': 'avec un mot de passe',
|
|
39
|
+
'register': 'à la création du compte',
|
|
40
|
+
'reset-password': 'après une réinitialisation du mot de passe',
|
|
41
|
+
'magic-link': 'avec un lien reçu par e-mail',
|
|
42
|
+
'email-code': 'avec un code reçu par e-mail',
|
|
43
|
+
'passkey': 'avec une clé d’accès',
|
|
44
|
+
'guest': 'en invité',
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The phrase a detail maps to in `phrases`, or `fallback` when it maps to none.
|
|
48
|
+
function byDetail(phrases: Record<string, string>, fallback: string): AuditPhrase {
|
|
49
|
+
return detail => (detail !== null && Object.hasOwn(phrases, detail) ? phrases[detail] : undefined) ?? fallback
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function signedIn(label: string, methods: Record<string, string>, provider: (name: string) => string) {
|
|
53
|
+
return (detail: string | null) => {
|
|
54
|
+
if (detail?.startsWith(OAUTH_PREFIX))
|
|
55
|
+
return `${label} ${provider(detail.slice(OAUTH_PREFIX.length))}`
|
|
56
|
+
|
|
57
|
+
const how = detail === null ? undefined : methods[detail]
|
|
58
|
+
|
|
59
|
+
return how === undefined ? label : `${label} ${how}`
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const english: AuditWording = {
|
|
64
|
+
'signed-in': signedIn('Signed in', ENGLISH_SIGN_INS, name => `with ${name}`),
|
|
65
|
+
// Before 0.15, signing out everywhere was a `signed-out` with this detail.
|
|
66
|
+
'signed-out': byDetail({ 'everywhere': 'Signed out everywhere', 'everywhere-else': 'Signed out everywhere else' }, 'Signed out'),
|
|
67
|
+
'signed-out-everywhere': detail => detail === 'this-one-kept' ? 'Signed out everywhere else' : 'Signed out everywhere',
|
|
68
|
+
'sign-in-failed': byDetail({ 'two-factor': 'Failed sign-in: wrong code', 'passkey': 'Failed sign-in: passkey refused' }, 'Failed sign-in: wrong password'),
|
|
69
|
+
'new-sign-in': 'Signed in from a new device or network',
|
|
70
|
+
'sign-in-locked': 'Sign-in locked after too many wrong passwords or codes',
|
|
71
|
+
'password-changed': 'Password changed',
|
|
72
|
+
'email-change-requested': naming(detail => `Email change to ${detail} asked`, 'Email change asked'),
|
|
73
|
+
'email-changed': naming(detail => `Email changed to ${detail}`, 'Email changed'),
|
|
74
|
+
'two-factor-enabled': 'Two-step verification turned on',
|
|
75
|
+
'two-factor-disabled': 'Two-step verification turned off',
|
|
76
|
+
'recovery-code-used': 'Recovery code used',
|
|
77
|
+
'recovery-codes-regenerated': 'New recovery codes',
|
|
78
|
+
'provider-linked': naming(detail => `${detail} account linked`, 'Account linked'),
|
|
79
|
+
'provider-unlinked': naming(detail => `${detail} account unlinked`, 'Account unlinked'),
|
|
80
|
+
'passkey-added': naming(detail => `Passkey “${detail}” added`, 'Passkey added'),
|
|
81
|
+
'passkey-removed': naming(detail => `Passkey “${detail}” removed`, 'Passkey removed'),
|
|
82
|
+
'token-created': naming(detail => `Access token “${detail}” created`, 'Access token created'),
|
|
83
|
+
'account-deleted': 'Account deleted',
|
|
84
|
+
'account-deletion-scheduled': naming(detail => `Account deletion scheduled for ${detail.slice(0, 10)}`, 'Account deletion scheduled'),
|
|
85
|
+
'account-deletion-cancelled': 'Account deletion cancelled',
|
|
86
|
+
'organization-handed-over': 'Became an owner of an organization whose last owner left',
|
|
87
|
+
'role-granted': naming(detail => `Role ${detail} granted`, 'Role granted'),
|
|
88
|
+
'role-revoked': naming(detail => `Role ${detail} revoked`, 'Role revoked'),
|
|
89
|
+
'banned': naming(detail => `Suspended: ${detail}`, 'Suspended'),
|
|
90
|
+
'unbanned': 'Suspension lifted',
|
|
91
|
+
'impersonation-started': 'An admin started acting as the account',
|
|
92
|
+
'impersonation-ended': 'An admin stopped acting as the account',
|
|
93
|
+
'organization-created': 'Organization created',
|
|
94
|
+
'organization-deleted': 'Organization deleted',
|
|
95
|
+
'member-joined': naming(roles => `Joined an organization as ${roles}`, 'Joined an organization', orgRoles),
|
|
96
|
+
'member-left': 'Left an organization',
|
|
97
|
+
'member-removed': 'Removed from an organization',
|
|
98
|
+
'oauth-client-approved': naming(detail => `${detail} was given access`, 'An app was given access'),
|
|
99
|
+
'oauth-client-revoked': naming(detail => `${detail} lost access`, 'An app lost access'),
|
|
100
|
+
'member-roles-changed': naming(roles => `Organization roles changed to ${roles}`, 'Organization roles changed', orgRoles),
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const french: AuditWording = {
|
|
104
|
+
'signed-in': signedIn('Connexion', FRENCH_SIGN_INS, name => `avec ${name}`),
|
|
105
|
+
'signed-out': byDetail({ 'everywhere': 'Déconnexion partout', 'everywhere-else': 'Déconnexion de tous les autres appareils' }, 'Déconnexion'),
|
|
106
|
+
'signed-out-everywhere': detail => detail === 'this-one-kept' ? 'Déconnexion de tous les autres appareils' : 'Déconnexion partout',
|
|
107
|
+
'sign-in-failed': byDetail({ 'two-factor': 'Échec de connexion : code incorrect', 'passkey': 'Échec de connexion : clé d’accès refusée' }, 'Échec de connexion : mot de passe incorrect'),
|
|
108
|
+
'new-sign-in': 'Connexion depuis un nouvel appareil ou réseau',
|
|
109
|
+
'sign-in-locked': 'Connexion suspendue après trop de mots de passe ou de codes incorrects',
|
|
110
|
+
'password-changed': 'Mot de passe modifié',
|
|
111
|
+
'email-change-requested': naming(detail => `Changement d’adresse vers ${detail} demandé`, 'Changement d’adresse demandé'),
|
|
112
|
+
'email-changed': naming(detail => `Adresse changée pour ${detail}`, 'Adresse modifiée'),
|
|
113
|
+
'two-factor-enabled': 'Validation en deux étapes activée',
|
|
114
|
+
'two-factor-disabled': 'Validation en deux étapes désactivée',
|
|
115
|
+
'recovery-code-used': 'Code de secours utilisé',
|
|
116
|
+
'recovery-codes-regenerated': 'Nouveaux codes de secours',
|
|
117
|
+
'provider-linked': naming(detail => `Compte ${detail} associé`, 'Compte associé'),
|
|
118
|
+
'provider-unlinked': naming(detail => `Compte ${detail} dissocié`, 'Compte dissocié'),
|
|
119
|
+
'passkey-added': naming(detail => `Clé d’accès « ${detail} » ajoutée`, 'Clé d’accès ajoutée'),
|
|
120
|
+
'passkey-removed': naming(detail => `Clé d’accès « ${detail} » supprimée`, 'Clé d’accès supprimée'),
|
|
121
|
+
'token-created': naming(detail => `Jeton d’accès « ${detail} » créé`, 'Jeton d’accès créé'),
|
|
122
|
+
'account-deleted': 'Compte supprimé',
|
|
123
|
+
'account-deletion-scheduled': naming(detail => `Suppression du compte prévue le ${detail.slice(0, 10)}`, 'Suppression du compte prévue'),
|
|
124
|
+
'account-deletion-cancelled': 'Suppression du compte annulée',
|
|
125
|
+
'organization-handed-over': 'Devenu propriétaire d’une organisation dont le dernier propriétaire est parti',
|
|
126
|
+
'role-granted': naming(detail => `Rôle ${detail} accordé`, 'Rôle accordé'),
|
|
127
|
+
'role-revoked': naming(detail => `Rôle ${detail} retiré`, 'Rôle retiré'),
|
|
128
|
+
'banned': naming(detail => `Compte suspendu : ${detail}`, 'Compte suspendu'),
|
|
129
|
+
'unbanned': 'Suspension levée',
|
|
130
|
+
'impersonation-started': 'Un administrateur agit en tant que ce compte',
|
|
131
|
+
'impersonation-ended': 'Un administrateur a cessé d’agir en tant que ce compte',
|
|
132
|
+
'organization-created': 'Organisation créée',
|
|
133
|
+
'organization-deleted': 'Organisation supprimée',
|
|
134
|
+
'member-joined': naming(roles => `Organisation rejointe en tant que ${roles}`, 'Organisation rejointe', orgRoles),
|
|
135
|
+
'member-left': 'Organisation quittée',
|
|
136
|
+
'member-removed': 'Retiré d’une organisation',
|
|
137
|
+
'oauth-client-approved': naming(detail => `${detail} a reçu un accès`, 'Une application a reçu un accès'),
|
|
138
|
+
'oauth-client-revoked': naming(detail => `${detail} a perdu son accès`, 'Une application a perdu son accès'),
|
|
139
|
+
'member-roles-changed': naming(roles => `Rôles dans l’organisation changés pour ${roles}`, 'Rôles dans l’organisation changés', orgRoles),
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// kayjs-auth's wording of the audit log by language, the ones its pages ship.
|
|
143
|
+
export const auditWording: Record<string, AuditWording> = { en: english, fr: french }
|
|
144
|
+
|
|
145
|
+
// An audit entry as a person reads it, in `locale`'s language (English for any kayjs-auth does not ship), `wording` over kayjs-auth's for any event the app words its own way.
|
|
146
|
+
export function describeAuditEntry(entry: Pick<AuditEntry, 'event' | 'detail'>, options: { locale?: string | undefined, wording?: Partial<AuditWording> | undefined } = {}): string {
|
|
147
|
+
const phrase = options.wording?.[entry.event] ?? (auditWording[language(options.locale ?? 'en')] ?? english)[entry.event]
|
|
148
|
+
|
|
149
|
+
if (phrase === undefined)
|
|
150
|
+
return entry.event
|
|
151
|
+
|
|
152
|
+
return typeof phrase === 'string' ? phrase : phrase(entry.detail)
|
|
153
|
+
}
|