@fabrika/iam 0.0.1 → 0.9.0-dev.26
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/fabrika.config.ts +267 -0
- package/migrations/0001_init.sql +316 -0
- package/migrations/0002_oauth.sql +38 -0
- package/migrations/0003_contract_apps.sql +11 -0
- package/migrations/0004_membership.sql +93 -0
- package/migrations/0005_workloads.sql +23 -0
- package/migrations/0006_membership_transport_keys.sql +16 -0
- package/migrations/0007_member_directory.sql +7 -0
- package/migrations/0008_durable_audit.sql +28 -0
- package/migrations/0009_member_lookups.sql +7 -0
- package/migrations/0010_workload_issues.sql +6 -0
- package/migrations/0011_app_presentation.sql +15 -0
- package/migrations/0012_external_principals.sql +10 -0
- package/migrations/0013_app_mail_templates.sql +14 -0
- package/migrations/0014_invitation_wording.sql +8 -0
- package/migrations/0015_app_schema_revisions.sql +15 -0
- package/migrations/0016_principal_grant_revision.sql +9 -0
- package/migrations/0017_app_sign_ins.sql +12 -0
- package/migrations/0018_app_members.sql +13 -0
- package/migrations/0019_app_mail_html.sql +7 -0
- package/migrations/0020_public_workloads.sql +34 -0
- package/migrations/0021_installation_brand.sql +19 -0
- package/migrations/0022_password_policy.sql +12 -0
- package/migrations/0023_terminals.sql +155 -0
- package/migrations/0024_app_terminals.sql +10 -0
- package/migrations/0025_terminal_completion.sql +10 -0
- package/migrations/0026_terminal_generation.sql +12 -0
- package/migrations/0027_application_exact_actions.sql +28 -0
- package/migrations/0028_oauth_client_assertions.sql +10 -0
- package/migrations/0029_role_via.sql +6 -0
- package/migrations/0030_membership_role_via.sql +12 -0
- package/migrations/0031_person_locale.sql +11 -0
- package/migrations-postgres/0001_init.sql +331 -0
- package/migrations-postgres/0002_oauth.sql +36 -0
- package/migrations-postgres/0003_contract_apps.sql +8 -0
- package/migrations-postgres/0004_membership.sql +92 -0
- package/migrations-postgres/0005_workloads.sql +18 -0
- package/migrations-postgres/0006_membership_transport_keys.sql +12 -0
- package/migrations-postgres/0007_member_directory.sql +7 -0
- package/migrations-postgres/0008_durable_audit.sql +8 -0
- package/migrations-postgres/0009_member_lookups.sql +7 -0
- package/migrations-postgres/0010_workload_issues.sql +6 -0
- package/migrations-postgres/0011_app_presentation.sql +15 -0
- package/migrations-postgres/0012_external_principals.sql +10 -0
- package/migrations-postgres/0013_app_mail_templates.sql +14 -0
- package/migrations-postgres/0014_invitation_wording.sql +8 -0
- package/migrations-postgres/0015_app_schema_revisions.sql +15 -0
- package/migrations-postgres/0016_principal_grant_revision.sql +9 -0
- package/migrations-postgres/0017_app_sign_ins.sql +12 -0
- package/migrations-postgres/0018_app_members.sql +13 -0
- package/migrations-postgres/0019_app_mail_html.sql +7 -0
- package/migrations-postgres/0020_public_workloads.sql +9 -0
- package/migrations-postgres/0021_installation_brand.sql +19 -0
- package/migrations-postgres/0022_password_policy.sql +12 -0
- package/migrations-postgres/0023_terminals.sql +70 -0
- package/migrations-postgres/0024_app_terminals.sql +10 -0
- package/migrations-postgres/0025_terminal_completion.sql +10 -0
- package/migrations-postgres/0026_terminal_generation.sql +12 -0
- package/migrations-postgres/0027_application_exact_actions.sql +28 -0
- package/migrations-postgres/0028_oauth_client_assertions.sql +9 -0
- package/migrations-postgres/0029_role_via.sql +4 -0
- package/migrations-postgres/0030_membership_role_via.sql +7 -0
- package/migrations-postgres/0031_person_locale.sql +11 -0
- package/package.json +69 -8
- package/src/admin/access.ts +47 -0
- package/src/admin/api-keys.ts +280 -0
- package/src/admin/app-people.ts +209 -0
- package/src/admin/app-schema.ts +308 -0
- package/src/admin/audit.ts +81 -0
- package/src/admin/brand.ts +54 -0
- package/src/admin/connections.ts +48 -0
- package/src/admin/context.ts +70 -0
- package/src/admin/grant-support.ts +192 -0
- package/src/admin/grants.ts +189 -0
- package/src/admin/handlers.ts +102 -0
- package/src/admin/http.ts +24 -0
- package/src/admin/membership-keys.ts +38 -0
- package/src/admin/pagination.ts +28 -0
- package/src/admin/password-policy.ts +48 -0
- package/src/admin/passwords.ts +78 -0
- package/src/admin/policies.ts +136 -0
- package/src/admin/principals.ts +226 -0
- package/src/admin/router.ts +291 -0
- package/src/admin/rpc.ts +387 -0
- package/src/admin/sessions.ts +115 -0
- package/src/admin/share-links.ts +127 -0
- package/src/admin/terminals.ts +115 -0
- package/src/admin/user-admin.ts +76 -0
- package/src/admin/workloads.ts +85 -0
- package/src/app-presentation.ts +178 -0
- package/src/app.ts +158 -0
- package/src/auth/cookies.ts +64 -0
- package/src/auth/failure.ts +25 -0
- package/src/auth/handoff.ts +136 -0
- package/src/auth/http.ts +88 -0
- package/src/auth/invitations.ts +220 -0
- package/src/auth/known-device.ts +44 -0
- package/src/auth/login.ts +162 -0
- package/src/auth/oidc-flight.ts +91 -0
- package/src/auth/oidc.ts +148 -0
- package/src/auth/password.ts +416 -0
- package/src/auth/routes.ts +108 -0
- package/src/auth/sessions.ts +249 -0
- package/src/auth/terminals.ts +284 -0
- package/src/auth.ts +193 -0
- package/src/binding-identity.ts +25 -0
- package/src/bounded-body.ts +35 -0
- package/src/catalog/cs.ts +147 -0
- package/src/catalog/en.ts +148 -0
- package/src/catalog/index.ts +122 -0
- package/src/catalog/uk.ts +164 -0
- package/src/client-address.ts +39 -0
- package/src/common-passwords.ts +37 -0
- package/src/contract-rules.ts +80 -0
- package/src/cron.ts +54 -0
- package/src/db.ts +128 -0
- package/src/delegation.ts +100 -0
- package/src/device-http.ts +39 -0
- package/src/device.ts +269 -0
- package/src/email-identity.ts +2 -0
- package/src/env.ts +132 -0
- package/src/handoff.ts +175 -0
- package/src/html.ts +4 -0
- package/src/human-admission.ts +40 -0
- package/src/identification.ts +185 -0
- package/src/index.ts +292 -0
- package/src/installation-brand.ts +82 -0
- package/src/issue.ts +265 -0
- package/src/json.ts +30 -0
- package/src/log.ts +26 -0
- package/src/membership-http.ts +135 -0
- package/src/membership.ts +1210 -0
- package/src/messages.ts +44 -0
- package/src/narrow-permissions.ts +26 -0
- package/src/node/browser-identity.ts +69 -0
- package/src/node/migrate.ts +52 -0
- package/src/node/prune.ts +34 -0
- package/src/node/runtime.ts +221 -0
- package/src/node/server.ts +104 -0
- package/src/oauth-client-auth.ts +93 -0
- package/src/oauth-client.ts +145 -0
- package/src/oauth-config.ts +72 -0
- package/src/oauth-http.ts +262 -0
- package/src/oauth.ts +272 -0
- package/src/oidc.ts +260 -0
- package/src/origin.ts +29 -0
- package/src/password-crypto.ts +148 -0
- package/src/password-db.ts +419 -0
- package/src/password-minimum.ts +44 -0
- package/src/password-pages.ts +397 -0
- package/src/password-policy.ts +79 -0
- package/src/password-service.ts +67 -0
- package/src/person-locale.ts +103 -0
- package/src/proxy-proof.ts +36 -0
- package/src/relay-mail.ts +64 -0
- package/src/repositories/app-schema.ts +550 -0
- package/src/repositories/audit.ts +405 -0
- package/src/repositories/brand.ts +66 -0
- package/src/repositories/credentials.ts +331 -0
- package/src/repositories/device.ts +137 -0
- package/src/repositories/grants.ts +404 -0
- package/src/repositories/handoff.ts +103 -0
- package/src/repositories/membership-transport.ts +49 -0
- package/src/repositories/membership.ts +875 -0
- package/src/repositories/oauth.ts +122 -0
- package/src/repositories/password-policy.ts +44 -0
- package/src/repositories/principals.ts +358 -0
- package/src/repositories/sessions.ts +339 -0
- package/src/repositories/sql.ts +23 -0
- package/src/repositories/terminals.ts +346 -0
- package/src/repositories/workloads.ts +134 -0
- package/src/request-id.ts +46 -0
- package/src/resolve.ts +396 -0
- package/src/retention.ts +34 -0
- package/src/roles.ts +54 -0
- package/src/rpc-http.ts +333 -0
- package/src/rpc-inputs.ts +110 -0
- package/src/rpc.ts +440 -0
- package/src/secret.ts +45 -0
- package/src/service.ts +59 -0
- package/src/services.ts +199 -0
- package/src/signing.ts +260 -0
- package/src/stored-chain.ts +23 -0
- package/src/stored-permissions.ts +34 -0
- package/src/terminals.ts +15 -0
- package/src/tokens.ts +506 -0
- package/src/workload-token.ts +130 -0
- package/types/fabrika.config.d.ts +11 -0
- package/types/src/admin/access.d.ts +19 -0
- package/types/src/admin/api-keys.d.ts +26 -0
- package/types/src/admin/app-people.d.ts +8 -0
- package/types/src/admin/app-schema.d.ts +32 -0
- package/types/src/admin/audit.d.ts +10 -0
- package/types/src/admin/brand.d.ts +10 -0
- package/types/src/admin/connections.d.ts +5 -0
- package/types/src/admin/context.d.ts +46 -0
- package/types/src/admin/grant-support.d.ts +59 -0
- package/types/src/admin/grants.d.ts +8 -0
- package/types/src/admin/handlers.d.ts +82 -0
- package/types/src/admin/http.d.ts +9 -0
- package/types/src/admin/membership-keys.d.ts +8 -0
- package/types/src/admin/pagination.d.ts +5 -0
- package/types/src/admin/password-policy.d.ts +9 -0
- package/types/src/admin/passwords.d.ts +5 -0
- package/types/src/admin/policies.d.ts +15 -0
- package/types/src/admin/principals.d.ts +15 -0
- package/types/src/admin/router.d.ts +75 -0
- package/types/src/admin/rpc.d.ts +6 -0
- package/types/src/admin/sessions.d.ts +14 -0
- package/types/src/admin/share-links.d.ts +21 -0
- package/types/src/admin/terminals.d.ts +13 -0
- package/types/src/admin/user-admin.d.ts +30 -0
- package/types/src/admin/workloads.d.ts +5 -0
- package/types/src/app-presentation.d.ts +86 -0
- package/types/src/app.d.ts +62 -0
- package/types/src/auth/cookies.d.ts +29 -0
- package/types/src/auth/failure.d.ts +47 -0
- package/types/src/auth/handoff.d.ts +66 -0
- package/types/src/auth/http.d.ts +29 -0
- package/types/src/auth/invitations.d.ts +18 -0
- package/types/src/auth/known-device.d.ts +12 -0
- package/types/src/auth/login.d.ts +10 -0
- package/types/src/auth/oidc-flight.d.ts +39 -0
- package/types/src/auth/oidc.d.ts +7 -0
- package/types/src/auth/password.d.ts +7 -0
- package/types/src/auth/routes.d.ts +15 -0
- package/types/src/auth/sessions.d.ts +60 -0
- package/types/src/auth/terminals.d.ts +47 -0
- package/types/src/auth.d.ts +70 -0
- package/types/src/binding-identity.d.ts +12 -0
- package/types/src/bounded-body.d.ts +12 -0
- package/types/src/catalog/cs.d.ts +2 -0
- package/types/src/catalog/en.d.ts +2 -0
- package/types/src/catalog/index.d.ts +112 -0
- package/types/src/catalog/uk.d.ts +2 -0
- package/types/src/client-address.d.ts +31 -0
- package/types/src/common-passwords.d.ts +2 -0
- package/types/src/contract-rules.d.ts +25 -0
- package/types/src/cron.d.ts +15 -0
- package/types/src/db.d.ts +58 -0
- package/types/src/delegation.d.ts +38 -0
- package/types/src/device-http.d.ts +2 -0
- package/types/src/device.d.ts +11 -0
- package/types/src/email-identity.d.ts +1 -0
- package/types/src/env.d.ts +127 -0
- package/types/src/handoff.d.ts +57 -0
- package/types/src/html.d.ts +2 -0
- package/types/src/human-admission.d.ts +22 -0
- package/types/src/identification.d.ts +30 -0
- package/types/src/index.d.ts +62 -0
- package/types/src/installation-brand.d.ts +22 -0
- package/types/src/issue.d.ts +62 -0
- package/types/src/json.d.ts +15 -0
- package/types/src/log.d.ts +15 -0
- package/types/src/membership-http.d.ts +8 -0
- package/types/src/membership.d.ts +156 -0
- package/types/src/messages.d.ts +20 -0
- package/types/src/narrow-permissions.d.ts +7 -0
- package/types/src/node/browser-identity.d.ts +1 -0
- package/types/src/node/migrate.d.ts +4 -0
- package/types/src/node/prune.d.ts +1 -0
- package/types/src/node/runtime.d.ts +51 -0
- package/types/src/node/server.d.ts +5 -0
- package/types/src/oauth-client-auth.d.ts +26 -0
- package/types/src/oauth-client.d.ts +17 -0
- package/types/src/oauth-config.d.ts +16 -0
- package/types/src/oauth-http.d.ts +7 -0
- package/types/src/oauth.d.ts +51 -0
- package/types/src/oidc.d.ts +81 -0
- package/types/src/origin.d.ts +9 -0
- package/types/src/password-crypto.d.ts +37 -0
- package/types/src/password-db.d.ts +134 -0
- package/types/src/password-minimum.d.ts +23 -0
- package/types/src/password-pages.d.ts +143 -0
- package/types/src/password-policy.d.ts +19 -0
- package/types/src/password-service.d.ts +24 -0
- package/types/src/person-locale.d.ts +50 -0
- package/types/src/proxy-proof.d.ts +21 -0
- package/types/src/relay-mail.d.ts +25 -0
- package/types/src/repositories/app-schema.d.ts +244 -0
- package/types/src/repositories/audit.d.ts +187 -0
- package/types/src/repositories/brand.d.ts +41 -0
- package/types/src/repositories/credentials.d.ts +148 -0
- package/types/src/repositories/device.d.ts +50 -0
- package/types/src/repositories/grants.d.ts +177 -0
- package/types/src/repositories/handoff.d.ts +52 -0
- package/types/src/repositories/membership-transport.d.ts +31 -0
- package/types/src/repositories/membership.d.ts +273 -0
- package/types/src/repositories/oauth.d.ts +55 -0
- package/types/src/repositories/password-policy.d.ts +25 -0
- package/types/src/repositories/principals.d.ts +159 -0
- package/types/src/repositories/sessions.d.ts +150 -0
- package/types/src/repositories/sql.d.ts +13 -0
- package/types/src/repositories/terminals.d.ts +144 -0
- package/types/src/repositories/workloads.d.ts +61 -0
- package/types/src/request-id.d.ts +25 -0
- package/types/src/resolve.d.ts +118 -0
- package/types/src/retention.d.ts +18 -0
- package/types/src/roles.d.ts +34 -0
- package/types/src/rpc-http.d.ts +53 -0
- package/types/src/rpc-inputs.d.ts +707 -0
- package/types/src/rpc.d.ts +4 -0
- package/types/src/secret.d.ts +21 -0
- package/types/src/service.d.ts +19 -0
- package/types/src/services.d.ts +72 -0
- package/types/src/signing.d.ts +72 -0
- package/types/src/stored-chain.d.ts +6 -0
- package/types/src/stored-permissions.d.ts +12 -0
- package/types/src/terminals.d.ts +7 -0
- package/types/src/tokens.d.ts +113 -0
- package/types/src/workload-token.d.ts +7 -0
- package/README.md +0 -3
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import type { Locale } from '@fabrika/auth-core'
|
|
2
|
+
import type { InstallationBrand } from '@fabrika/proxy-core/brand'
|
|
3
|
+
import { PAGE_STYLE, pageBody } from '@fabrika/proxy-core/page-style'
|
|
4
|
+
import { type AppPresentation, appQuery, landingUrl, localeQuery } from './app-presentation'
|
|
5
|
+
import type { AuthFailure } from './auth/failure'
|
|
6
|
+
import type { Handoff } from './auth/handoff'
|
|
7
|
+
import { catalogFor, type PageText } from './catalog'
|
|
8
|
+
import { escapeHtml } from './html'
|
|
9
|
+
import { PASSWORD_MAX_CODE_POINTS, type PasswordProblem } from './password-policy'
|
|
10
|
+
import { MAX_TERMINAL_LABEL_LENGTH } from './terminals'
|
|
11
|
+
|
|
12
|
+
export function loginPage(input: {
|
|
13
|
+
readonly presentation: AppPresentation
|
|
14
|
+
/** An address on IAM's own origin, or a handoff's destination; null lands on the installation's landing. */
|
|
15
|
+
readonly redirect: string | null
|
|
16
|
+
readonly oidcEnabled: boolean
|
|
17
|
+
readonly forgotEnabled: boolean
|
|
18
|
+
/** The app this login is a handoff for (ADR-0022); absent for a plain IAM login. */
|
|
19
|
+
readonly app?: string
|
|
20
|
+
readonly handoffState?: string
|
|
21
|
+
readonly handoffChallenge?: string
|
|
22
|
+
/**
|
|
23
|
+
* The ORIGIN off IAM the submission answers its 302 with: the handoff destination's, or the console's
|
|
24
|
+
* for a login with no return address. Chromium applies `form-action` to the REDIRECT a submission
|
|
25
|
+
* answers with, not only to the action, so `'self'` alone silently blocks the 302 to
|
|
26
|
+
* `<app>/__fabrika/auth/callback` — the login succeeds, a code is issued and spent, and the browser
|
|
27
|
+
* never leaves this page. Measured in Chromium 149. The value is a REGISTERED return origin, never
|
|
28
|
+
* the request's, so widening the directive cannot widen where a browser may be sent.
|
|
29
|
+
*/
|
|
30
|
+
readonly redirectOrigin?: string
|
|
31
|
+
readonly invalidCredentials?: boolean
|
|
32
|
+
}): Response {
|
|
33
|
+
const text = catalogFor(input.presentation.locale).pages
|
|
34
|
+
// `app` rides both continuations so a form post and an SSO detour end in the same handoff. It is
|
|
35
|
+
// re-validated against the registered origins on the way back in — the form is never trusted.
|
|
36
|
+
const appQueryPart = input.app === undefined
|
|
37
|
+
? ''
|
|
38
|
+
: `&app=${encodeURIComponent(input.app)}&state=${encodeURIComponent(input.handoffState ?? '')}&code_challenge=${
|
|
39
|
+
encodeURIComponent(input.handoffChallenge ?? '')
|
|
40
|
+
}`
|
|
41
|
+
// The reader's own language rides both too, so a refused password and a refused SSO start keep speaking it.
|
|
42
|
+
const locale = localeQuery(input.presentation)
|
|
43
|
+
const oidcUrl = `/auth/oidc/start?redirect=${encodeURIComponent(input.redirect ?? '')}${appQueryPart}${locale.replace('?', '&')}`
|
|
44
|
+
const title = text.signIn.title(input.presentation.displayName)
|
|
45
|
+
return htmlPage({
|
|
46
|
+
locale: input.presentation.locale,
|
|
47
|
+
brand: input.presentation.brand,
|
|
48
|
+
title,
|
|
49
|
+
content: `<h1>${escapeHtml(title)}</h1>${message(input.invalidCredentials === true ? text.signIn.invalidCredentials : undefined)}
|
|
50
|
+
<form method="post" action="${escapeHtml(`/auth/login${locale}`)}">
|
|
51
|
+
<input type="hidden" name="redirect" value="${escapeHtml(input.redirect ?? '')}">
|
|
52
|
+
${input.app === undefined ? '' : `<input type="hidden" name="app" value="${escapeHtml(input.app)}">`}
|
|
53
|
+
${input.handoffState === undefined ? '' : `<input type="hidden" name="state" value="${escapeHtml(input.handoffState)}">`}
|
|
54
|
+
${input.handoffChallenge === undefined ? '' : `<input type="hidden" name="code_challenge" value="${escapeHtml(input.handoffChallenge)}">`}
|
|
55
|
+
<label>${escapeHtml(text.email)}<input name="email" type="email" autocomplete="username" inputmode="email" required></label>
|
|
56
|
+
${passwordInput(text, 'password', text.signIn.password, 'current-password')}
|
|
57
|
+
<button type="submit">${escapeHtml(text.signIn.submit)}</button>
|
|
58
|
+
</form>
|
|
59
|
+
${
|
|
60
|
+
input.forgotEnabled
|
|
61
|
+
? `<p><a href="${escapeHtml(`/auth/forgot-password${appQuery(input.presentation)}`)}">${escapeHtml(text.signIn.forgot)}</a></p>`
|
|
62
|
+
: ''
|
|
63
|
+
}
|
|
64
|
+
${input.oidcEnabled ? `<hr><p><a class="button" href="${escapeHtml(oidcUrl)}">${escapeHtml(text.signIn.singleSignOn)}</a></p>` : ''}`,
|
|
65
|
+
status: input.invalidCredentials === true ? 401 : 200,
|
|
66
|
+
formActionOrigin: input.redirectOrigin,
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Where the account question posts its answer, with the handoff's fields and a `decision`. */
|
|
71
|
+
export const ACCOUNT_CHOICE_PATH = '/auth/login/account'
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The question a handoff asks a browser already signed in here, before its login is used for an application it has
|
|
75
|
+
* not chosen yet. Both answers post the handoff back, and the POST reads it again: the form is a hint, never a
|
|
76
|
+
* permission. Continuing answers a 302 to the application's callback, so `form-action` admits `redirectOrigin`, the
|
|
77
|
+
* handoff's registered origin (`loginDestinationFields`).
|
|
78
|
+
*/
|
|
79
|
+
export function chooseAccountPage(input: {
|
|
80
|
+
readonly presentation: AppPresentation
|
|
81
|
+
/** The signed-in principal's label. */
|
|
82
|
+
readonly label: string
|
|
83
|
+
readonly handoff: Handoff
|
|
84
|
+
readonly redirectOrigin?: string
|
|
85
|
+
}): Response {
|
|
86
|
+
const text = catalogFor(input.presentation.locale).pages.chooseAccount
|
|
87
|
+
const title = text.title(input.presentation.displayName)
|
|
88
|
+
const { handoff } = input
|
|
89
|
+
return htmlPage({
|
|
90
|
+
locale: input.presentation.locale,
|
|
91
|
+
brand: input.presentation.brand,
|
|
92
|
+
title,
|
|
93
|
+
content: `<h1>${escapeHtml(title)}</h1><p>${escapeHtml(text.signedInAs)} <strong>${escapeHtml(input.label)}</strong>.</p>
|
|
94
|
+
<form method="post" action="${escapeHtml(`${ACCOUNT_CHOICE_PATH}${localeQuery(input.presentation)}`)}">
|
|
95
|
+
<input type="hidden" name="app" value="${escapeHtml(handoff.app)}">
|
|
96
|
+
<input type="hidden" name="redirect" value="${escapeHtml(handoff.returnUrl)}">
|
|
97
|
+
<input type="hidden" name="state" value="${escapeHtml(handoff.state)}">
|
|
98
|
+
<input type="hidden" name="code_challenge" value="${escapeHtml(handoff.challenge)}">
|
|
99
|
+
<button type="submit" name="decision" value="continue">${escapeHtml(text.continueAs(input.label))}</button>
|
|
100
|
+
<button type="submit" name="decision" value="another">${escapeHtml(text.another)}</button>
|
|
101
|
+
</form>
|
|
102
|
+
<p>${escapeHtml(text.anotherNote)}</p>`,
|
|
103
|
+
formActionOrigin: input.redirectOrigin,
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The recovery form, and the page its POST answers. The form posts back with the link's `?app=`, so the action it may
|
|
109
|
+
* issue keeps the application; the way back is the application's address when it has one, otherwise IAM's sign-in.
|
|
110
|
+
*/
|
|
111
|
+
export function forgotPasswordPage(input: { readonly presentation: AppPresentation; readonly sent?: boolean }): Response {
|
|
112
|
+
const { presentation } = input
|
|
113
|
+
const text = catalogFor(presentation.locale).pages
|
|
114
|
+
const back = landingUrl(presentation) ?? `/auth/login${localeQuery(presentation)}`
|
|
115
|
+
const content = input.sent === true
|
|
116
|
+
? `<h1>${escapeHtml(text.forgot.sentTitle)}</h1><p>${escapeHtml(text.forgot.sentText)}</p><p><a href="${escapeHtml(back)}">${
|
|
117
|
+
escapeHtml(text.forgot.back)
|
|
118
|
+
}</a></p>`
|
|
119
|
+
: `<h1>${escapeHtml(text.forgot.title)}</h1><p>${escapeHtml(text.forgot.intro(presentation.displayName))}</p>
|
|
120
|
+
<form method="post" action="${escapeHtml(`/auth/forgot-password${appQuery(presentation)}`)}">
|
|
121
|
+
<label>${escapeHtml(text.email)}<input name="email" type="email" autocomplete="username" inputmode="email" required></label>
|
|
122
|
+
<button type="submit">${escapeHtml(text.forgot.submit)}</button>
|
|
123
|
+
</form>`
|
|
124
|
+
return htmlPage({
|
|
125
|
+
locale: presentation.locale,
|
|
126
|
+
brand: presentation.brand,
|
|
127
|
+
title: input.sent === true ? text.forgot.sentTitle : text.forgot.title,
|
|
128
|
+
content,
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The page an action link opens. Its form posts back with the link's `?app=`, so a refusal speaks the same language;
|
|
134
|
+
* the completed action lands on `landingOrigin`, which `form-action` therefore admits.
|
|
135
|
+
*/
|
|
136
|
+
export function setPasswordPage(input: {
|
|
137
|
+
readonly presentation: AppPresentation
|
|
138
|
+
/** Where the completed action lands off IAM (`actionLandingOrigin`); null for IAM's own sign-in page. */
|
|
139
|
+
readonly landingOrigin: string | null
|
|
140
|
+
readonly token?: string
|
|
141
|
+
readonly purpose: 'enrollment' | 'reset'
|
|
142
|
+
/** The installation's minimum (`passwordMinLength`), which the rule and a refusal state. */
|
|
143
|
+
readonly minLength: number
|
|
144
|
+
readonly problem?: PasswordProblem
|
|
145
|
+
}): Response {
|
|
146
|
+
const { presentation } = input
|
|
147
|
+
const text = catalogFor(presentation.locale).pages
|
|
148
|
+
const title = text.setPassword.title(input.purpose)
|
|
149
|
+
const path = input.purpose === 'enrollment' ? '/auth/password/enroll' : '/auth/password/reset'
|
|
150
|
+
const problem = input.problem === undefined ? undefined : text.setPassword.problem(input.problem, input.minLength, PASSWORD_MAX_CODE_POINTS)
|
|
151
|
+
return htmlPage({
|
|
152
|
+
locale: presentation.locale,
|
|
153
|
+
brand: presentation.brand,
|
|
154
|
+
title,
|
|
155
|
+
content: `<h1>${escapeHtml(title)}</h1>${message(problem)}<p>${
|
|
156
|
+
escapeHtml(text.setPassword.rule(input.minLength, PASSWORD_MAX_CODE_POINTS))
|
|
157
|
+
}</p><p>${escapeHtml(text.setPassword.shared(presentation.displayName))}</p>
|
|
158
|
+
<form method="post" action="${escapeHtml(`${path}${appQuery(presentation)}`)}">
|
|
159
|
+
<input id="action-token" type="hidden" name="token" value="${escapeHtml(input.token ?? '')}">
|
|
160
|
+
${passwordInput(text, 'password', text.setPassword.newPassword, 'new-password')}
|
|
161
|
+
<button type="submit">${escapeHtml(title)}</button>
|
|
162
|
+
</form>`,
|
|
163
|
+
status: input.problem === undefined ? 200 : 400,
|
|
164
|
+
formActionOrigin: input.landingOrigin ?? undefined,
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The same-origin confirmation that `GET /auth/logout` renders. Signing out is a state change, so it
|
|
170
|
+
* only happens on a POST that passes the origin check; this keeps the link usable and lives here
|
|
171
|
+
* because `htmlPage` (and its CSP) is what every browser-facing IAM page is built from.
|
|
172
|
+
*/
|
|
173
|
+
export function logoutPage(input: {
|
|
174
|
+
/** An address on IAM's own origin; null signs out to the installation's landing. */
|
|
175
|
+
readonly redirect: string | null
|
|
176
|
+
/** The console's origin when the sign-out lands there, which `form-action` therefore admits. */
|
|
177
|
+
readonly landingOrigin: string | null
|
|
178
|
+
/** The installation's logo (`installationBrand`), or null for the fabrika mark. */
|
|
179
|
+
readonly brand: InstallationBrand | null
|
|
180
|
+
/** The reader's language (`readerLocale`): the sign-out names no application. */
|
|
181
|
+
readonly locale: Locale
|
|
182
|
+
}): Response {
|
|
183
|
+
const text = catalogFor(input.locale).pages
|
|
184
|
+
const query = input.redirect === null ? '' : `?redirect=${encodeURIComponent(input.redirect)}`
|
|
185
|
+
return htmlPage({
|
|
186
|
+
locale: input.locale,
|
|
187
|
+
brand: input.brand,
|
|
188
|
+
title: text.logout.title,
|
|
189
|
+
content: `<h1>${escapeHtml(text.logout.title)}</h1><p>${escapeHtml(text.logout.text)}</p>
|
|
190
|
+
<form method="post" action="${escapeHtml(`/auth/logout${query}`)}">
|
|
191
|
+
<button type="submit">${escapeHtml(text.logout.submit)}</button>
|
|
192
|
+
</form>`,
|
|
193
|
+
formActionOrigin: input.landingOrigin ?? undefined,
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Where an administrator enrols the browser it uses as a terminal (ADR-0067). */
|
|
198
|
+
export const TERMINAL_ENROL_PATH = '/auth/terminals/enroll'
|
|
199
|
+
|
|
200
|
+
/** The enrolment form. It names no application, so it speaks the reader's language (`readerLocale`). */
|
|
201
|
+
export function terminalEnrolPage(
|
|
202
|
+
input: { readonly brand: InstallationBrand | null; readonly locale: Locale; readonly labelProblem?: boolean },
|
|
203
|
+
): Response {
|
|
204
|
+
const text = catalogFor(input.locale).pages.terminal
|
|
205
|
+
return htmlPage({
|
|
206
|
+
locale: input.locale,
|
|
207
|
+
brand: input.brand,
|
|
208
|
+
title: text.enrolTitle,
|
|
209
|
+
content: `<h1>${escapeHtml(text.enrolTitle)}</h1>${
|
|
210
|
+
message(input.labelProblem === true ? text.labelProblem(MAX_TERMINAL_LABEL_LENGTH) : undefined)
|
|
211
|
+
}<p>${escapeHtml(text.enrolText)}</p>
|
|
212
|
+
<form method="post" action="${TERMINAL_ENROL_PATH}">
|
|
213
|
+
<label>${escapeHtml(text.label)}<input name="label" type="text" maxlength="${MAX_TERMINAL_LABEL_LENGTH}" autocomplete="off" required></label>
|
|
214
|
+
<button type="submit">${escapeHtml(text.submit)}</button>
|
|
215
|
+
</form>`,
|
|
216
|
+
status: input.labelProblem === true ? 400 : 200,
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** What an enrolled browser shows: the administrator is signed out and the terminal is named. */
|
|
221
|
+
export function terminalEnrolledPage(label: string, brand: InstallationBrand | null, locale: Locale): Response {
|
|
222
|
+
const text = catalogFor(locale).pages.terminal
|
|
223
|
+
return htmlPage({
|
|
224
|
+
locale,
|
|
225
|
+
brand,
|
|
226
|
+
title: text.enrolledTitle,
|
|
227
|
+
content: `<h1>${escapeHtml(text.enrolledTitle)}</h1><p>${escapeHtml(text.enrolledText(label))}</p>`,
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** A refused browser-facing auth request, in the request's language and with its status. */
|
|
232
|
+
export function authFailurePage(presentation: AppPresentation, failure: AuthFailure, status: number): Response {
|
|
233
|
+
const text = catalogFor(presentation.locale).pages.failure
|
|
234
|
+
return htmlPage({
|
|
235
|
+
locale: presentation.locale,
|
|
236
|
+
brand: presentation.brand,
|
|
237
|
+
title: text.title,
|
|
238
|
+
content: `<h1>${escapeHtml(text.title)}</h1><p>${escapeHtml(text.text(failure))}</p>`,
|
|
239
|
+
status,
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function invalidActionPage(presentation: AppPresentation): Response {
|
|
244
|
+
const text = catalogFor(presentation.locale).pages
|
|
245
|
+
return htmlPage({
|
|
246
|
+
locale: presentation.locale,
|
|
247
|
+
brand: presentation.brand,
|
|
248
|
+
title: text.invalidLink.title,
|
|
249
|
+
content: `<h1>${escapeHtml(text.invalidLink.title)}</h1><p>${escapeHtml(text.invalidLink.text)}</p><p><a href="${
|
|
250
|
+
escapeHtml(`/auth/forgot-password${appQuery(presentation)}`)
|
|
251
|
+
}">${escapeHtml(text.invalidLink.requestNew)}</a></p>`,
|
|
252
|
+
status: 400,
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The page an invitation link opens. The one-time token rides in the fragment and the script copies it into the form,
|
|
258
|
+
* which posts back with the link's `?app=`; an accepted invitation lands on the application's registered origin, which
|
|
259
|
+
* `form-action` therefore admits.
|
|
260
|
+
*/
|
|
261
|
+
export function invitationPage(presentation: AppPresentation): Response {
|
|
262
|
+
const text = catalogFor(presentation.locale).pages
|
|
263
|
+
return htmlPage({
|
|
264
|
+
locale: presentation.locale,
|
|
265
|
+
brand: presentation.brand,
|
|
266
|
+
title: text.invitation.title,
|
|
267
|
+
content: `<h1>${escapeHtml(text.invitation.title)}</h1><p>${escapeHtml(text.invitation.text)}</p>
|
|
268
|
+
<form method="post" action="${escapeHtml(`/auth/invitations/accept${appQuery(presentation)}`)}">
|
|
269
|
+
<input id="action-token" type="hidden" name="token" value="">
|
|
270
|
+
<button type="submit">${escapeHtml(text.invitation.submit)}</button>
|
|
271
|
+
</form>`,
|
|
272
|
+
formActionOrigin: presentation.landing ?? undefined,
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The answer to accepting an invitation that creates its principal: choose the password of the new account. The token
|
|
278
|
+
* rides in the form as on `setPasswordPage`, and the form posts back to the invitation with the application's `?app=`;
|
|
279
|
+
* the accepted invitation lands on `landingOrigin`, which `form-action` therefore admits.
|
|
280
|
+
*/
|
|
281
|
+
export function invitationPasswordPage(input: {
|
|
282
|
+
readonly presentation: AppPresentation
|
|
283
|
+
/** Where the new account lands off IAM (`actionLandingOrigin`); null for IAM's own sign-in page. */
|
|
284
|
+
readonly landingOrigin: string | null
|
|
285
|
+
readonly token: string
|
|
286
|
+
/** The invited mailbox, which the new account signs in with. */
|
|
287
|
+
readonly address: string
|
|
288
|
+
/** The installation's minimum (`passwordMinLength`), which the rule and a refusal state. */
|
|
289
|
+
readonly minLength: number
|
|
290
|
+
readonly problem?: PasswordProblem
|
|
291
|
+
}): Response {
|
|
292
|
+
const { presentation } = input
|
|
293
|
+
const text = catalogFor(presentation.locale).pages
|
|
294
|
+
const problem = input.problem === undefined ? undefined : text.setPassword.problem(input.problem, input.minLength, PASSWORD_MAX_CODE_POINTS)
|
|
295
|
+
return htmlPage({
|
|
296
|
+
locale: presentation.locale,
|
|
297
|
+
brand: presentation.brand,
|
|
298
|
+
title: text.invitation.createTitle,
|
|
299
|
+
content: `<h1>${escapeHtml(text.invitation.createTitle)}</h1>${message(problem)}<p>${escapeHtml(text.invitation.createText(input.address))}</p><p>${
|
|
300
|
+
escapeHtml(text.setPassword.rule(input.minLength, PASSWORD_MAX_CODE_POINTS))
|
|
301
|
+
}</p><p>${escapeHtml(text.setPassword.shared(presentation.displayName))}</p>
|
|
302
|
+
<form method="post" action="${escapeHtml(`/auth/invitations/accept${appQuery(presentation)}`)}">
|
|
303
|
+
<input id="action-token" type="hidden" name="token" value="${escapeHtml(input.token)}">
|
|
304
|
+
<input type="email" name="username" value="${escapeHtml(input.address)}" autocomplete="username" readonly hidden>
|
|
305
|
+
${passwordInput(text, 'password', text.setPassword.newPassword, 'new-password')}
|
|
306
|
+
<button type="submit">${escapeHtml(text.invitation.createSubmit)}</button>
|
|
307
|
+
</form>`,
|
|
308
|
+
status: input.problem === undefined ? 200 : 400,
|
|
309
|
+
formActionOrigin: input.landingOrigin ?? undefined,
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** An outcome of accepting an invitation. `signIn` adds the way back for a browser with no session here. */
|
|
314
|
+
export function invitationOutcomePage(
|
|
315
|
+
input: {
|
|
316
|
+
readonly presentation: AppPresentation
|
|
317
|
+
readonly title: (text: PageText['invitation']) => string
|
|
318
|
+
readonly text: (text: PageText['invitation']) => string
|
|
319
|
+
readonly status: number
|
|
320
|
+
readonly signIn?: string
|
|
321
|
+
/** IAM's sign-out page, which asks before it signs out. */
|
|
322
|
+
readonly signOut?: string
|
|
323
|
+
},
|
|
324
|
+
): Response {
|
|
325
|
+
const text = catalogFor(input.presentation.locale).pages.invitation
|
|
326
|
+
const title = input.title(text)
|
|
327
|
+
const signIn = input.signIn === undefined ? '' : `<p><a class="button" href="${escapeHtml(input.signIn)}">${escapeHtml(text.signIn)}</a></p>`
|
|
328
|
+
const signOut = input.signOut === undefined ? '' : `<p><a class="button" href="${escapeHtml(input.signOut)}">${escapeHtml(text.signOut)}</a></p>`
|
|
329
|
+
return htmlPage({
|
|
330
|
+
locale: input.presentation.locale,
|
|
331
|
+
brand: input.presentation.brand,
|
|
332
|
+
title,
|
|
333
|
+
content: `<h1>${escapeHtml(title)}</h1><p>${escapeHtml(input.text(text))}</p>${signIn}${signOut}`,
|
|
334
|
+
status: input.status,
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function passwordInput(text: PageText, name: string, label: string, autocomplete: 'current-password' | 'new-password'): string {
|
|
339
|
+
return `<label>${
|
|
340
|
+
escapeHtml(label)
|
|
341
|
+
}<span class="password"><input id="${name}" name="${name}" type="password" autocomplete="${autocomplete}" required><button type="button" data-show="${name}" data-label-show="${
|
|
342
|
+
escapeHtml(text.showPassword)
|
|
343
|
+
}" data-label-hide="${escapeHtml(text.hidePassword)}">${escapeHtml(text.showPassword)}</button></span></label>`
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function message(value: string | undefined): string {
|
|
347
|
+
return value === undefined ? '' : `<p role="alert" class="error">${escapeHtml(value)}</p>`
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* `formActionOrigin` widens `form-action` by exactly one origin: the login page's handoff destination, or the
|
|
352
|
+
* registered origin an action or an invitation page lands on. It must already be an origin (scheme + host + optional
|
|
353
|
+
* port) that `normalizeOrigin` admitted, which refuses every character that would end or widen the directive; the
|
|
354
|
+
* registry stores only its output, and `presentationFor` checks a stored origin again.
|
|
355
|
+
*
|
|
356
|
+
* `brand` is required, so no page forgets the installation's logo; null draws the fabrika mark. The logo is an `<img>`
|
|
357
|
+
* of a `data:` URI, which is all `img-src data:` admits: an image runs no script, and a `data:` URI loads nothing from
|
|
358
|
+
* anywhere. `default-src` and `script-src` stay closed to it.
|
|
359
|
+
*/
|
|
360
|
+
export function htmlPage(page: {
|
|
361
|
+
readonly locale: Locale
|
|
362
|
+
readonly brand: InstallationBrand | null
|
|
363
|
+
readonly title: string
|
|
364
|
+
readonly content: string
|
|
365
|
+
readonly status?: number
|
|
366
|
+
readonly formActionOrigin?: string
|
|
367
|
+
}): Response {
|
|
368
|
+
const formAction = page.formActionOrigin === undefined ? `'self'` : `'self' ${page.formActionOrigin}`
|
|
369
|
+
const nonce = randomNonce()
|
|
370
|
+
const html =
|
|
371
|
+
`<!doctype html><html lang="${page.locale}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${
|
|
372
|
+
escapeHtml(page.title)
|
|
373
|
+
}</title>
|
|
374
|
+
<style nonce="${nonce}">${PAGE_STYLE}</style></head><body>${
|
|
375
|
+
pageBody(page.content, { runsOn: escapeHtml(catalogFor(page.locale).pages.runsOn) }, page.brand)
|
|
376
|
+
}
|
|
377
|
+
<script nonce="${nonce}">var actionToken=document.getElementById('action-token');if(actionToken&&!actionToken.value){var fragment=new URLSearchParams(location.hash.slice(1));var token=fragment.get('token');if(token){actionToken.value=token;history.replaceState(null,'',location.pathname+location.search);}}document.querySelectorAll('[data-show]').forEach(function(button){button.addEventListener('click',function(){var input=document.getElementById(button.dataset.show);if(!input)return;var show=input.type==='password';input.type=show?'text':'password';button.textContent=show?button.dataset.labelHide:button.dataset.labelShow;} );});</script></body></html>`
|
|
378
|
+
return new Response(html, {
|
|
379
|
+
status: page.status ?? 200,
|
|
380
|
+
headers: {
|
|
381
|
+
'content-type': 'text/html; charset=utf-8',
|
|
382
|
+
'cache-control': 'no-store',
|
|
383
|
+
'content-security-policy':
|
|
384
|
+
`default-src 'none'; style-src 'nonce-${nonce}'; script-src 'nonce-${nonce}'; img-src data:; form-action ${formAction}; base-uri 'none'; frame-ancestors 'none'`,
|
|
385
|
+
'referrer-policy': 'no-referrer',
|
|
386
|
+
'x-content-type-options': 'nosniff',
|
|
387
|
+
'x-frame-options': 'DENY',
|
|
388
|
+
},
|
|
389
|
+
})
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function randomNonce(): string {
|
|
393
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16))
|
|
394
|
+
let value = ''
|
|
395
|
+
for (const byte of bytes) value += byte.toString(16).padStart(2, '0')
|
|
396
|
+
return value
|
|
397
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { PASSWORD_MAX_LENGTH } from '@fabrika/iam-contract'
|
|
2
|
+
import { COMMON_PASSWORD_LIST } from './common-passwords'
|
|
3
|
+
|
|
4
|
+
export const PASSWORD_MAX_CODE_POINTS = PASSWORD_MAX_LENGTH
|
|
5
|
+
|
|
6
|
+
/** Long ones the list does not hold, and the Czech ones a short minimum lets through: "heslo" is "password". */
|
|
7
|
+
const COMMON_PASSWORDS = new Set([
|
|
8
|
+
'123456789012345',
|
|
9
|
+
'correcthorsebatterystaple',
|
|
10
|
+
'letmeinletmeinletmein',
|
|
11
|
+
'passwordpassword',
|
|
12
|
+
'qwertyuiopqwerty',
|
|
13
|
+
'welcome123456789',
|
|
14
|
+
'heslo',
|
|
15
|
+
'heslo1',
|
|
16
|
+
'heslo12',
|
|
17
|
+
'heslo123',
|
|
18
|
+
'heslo1234',
|
|
19
|
+
'hesloheslo',
|
|
20
|
+
'ahoj',
|
|
21
|
+
'ahoj123',
|
|
22
|
+
'ahojahoj',
|
|
23
|
+
'qwertz',
|
|
24
|
+
'qwertzu',
|
|
25
|
+
'qwertzui',
|
|
26
|
+
'qwertzuiop',
|
|
27
|
+
'asdfgh',
|
|
28
|
+
'yxcvbnm',
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
export interface PasswordPolicyContext {
|
|
32
|
+
readonly email?: string | null
|
|
33
|
+
readonly label?: string | null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Why a chosen password is refused; the page words it in the person's language. */
|
|
37
|
+
export type PasswordProblem = 'too_short' | 'too_long' | 'guessable'
|
|
38
|
+
|
|
39
|
+
export type PasswordPolicyResult =
|
|
40
|
+
| { readonly ok: true; readonly password: string }
|
|
41
|
+
| { readonly ok: false; readonly problem: PasswordProblem }
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Validate only length and whole-password blocklists; do not impose composition rules. `minLength` is the installation's
|
|
45
|
+
* (`passwordMinLength`), which every caller reads for the request: there is no default to fall back on by forgetting it.
|
|
46
|
+
*/
|
|
47
|
+
export function validatePassword(raw: string, minLength: number, context: PasswordPolicyContext = {}): PasswordPolicyResult {
|
|
48
|
+
const password = raw.normalize('NFC')
|
|
49
|
+
const length = Array.from(password).length
|
|
50
|
+
if (length < minLength) {
|
|
51
|
+
return { ok: false, problem: 'too_short' }
|
|
52
|
+
}
|
|
53
|
+
if (length > PASSWORD_MAX_CODE_POINTS) {
|
|
54
|
+
return { ok: false, problem: 'too_long' }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const comparable = password.toLocaleLowerCase('en-US')
|
|
58
|
+
if (COMMON_PASSWORDS.has(comparable) || COMMON_PASSWORD_LIST.has(comparable) || contextPasswords(context).has(comparable)) {
|
|
59
|
+
return { ok: false, problem: 'guessable' }
|
|
60
|
+
}
|
|
61
|
+
return { ok: true, password }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function contextPasswords(context: PasswordPolicyContext): Set<string> {
|
|
65
|
+
const values = new Set<string>()
|
|
66
|
+
addContextValue(values, context.email)
|
|
67
|
+
addContextValue(values, context.label)
|
|
68
|
+
if (context.email) {
|
|
69
|
+
const separator = context.email.lastIndexOf('@')
|
|
70
|
+
if (separator > 0) addContextValue(values, context.email.slice(0, separator))
|
|
71
|
+
}
|
|
72
|
+
return values
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function addContextValue(values: Set<string>, raw: string | null | undefined): void {
|
|
76
|
+
if (!raw) return
|
|
77
|
+
const value = raw.normalize('NFC').trim().toLocaleLowerCase('en-US')
|
|
78
|
+
if (value !== '') values.add(value)
|
|
79
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { EmailMessage } from '@fabrika/email'
|
|
2
|
+
import { type AppPresentation, appQuery, withPersonLocale } from './app-presentation'
|
|
3
|
+
import type { PrincipalRow } from './db'
|
|
4
|
+
import { EMAIL_SEND_DEADLINE_MS, renderEmail } from './messages'
|
|
5
|
+
import { randomToken } from './oidc'
|
|
6
|
+
import type { ActionIssuance, PasswordActionPurpose } from './password-db'
|
|
7
|
+
import { parseLocale } from './person-locale'
|
|
8
|
+
import { hashToken } from './secret'
|
|
9
|
+
import type { Services } from './services'
|
|
10
|
+
|
|
11
|
+
const ACTION_TTL_SECONDS = 60 * 60
|
|
12
|
+
|
|
13
|
+
export interface IssuedPasswordAction {
|
|
14
|
+
readonly id: string
|
|
15
|
+
readonly url: string
|
|
16
|
+
readonly expiresAt: number
|
|
17
|
+
readonly message: EmailMessage | null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Issue an opaque one-time action. The plaintext token exists only in the returned URL. The action keeps the
|
|
22
|
+
* presentation's application: the link names it (`?app=`) for the page's language, and the stored row decides where the
|
|
23
|
+
* completed action lands. The mail speaks the principal's own language where IAM keeps one, else the presentation's;
|
|
24
|
+
* the action keeps the person's language (the principal's, or the one the request named), so its page speaks it too.
|
|
25
|
+
*/
|
|
26
|
+
export async function issuePasswordAction(
|
|
27
|
+
services: Services,
|
|
28
|
+
input: {
|
|
29
|
+
readonly principal: PrincipalRow
|
|
30
|
+
readonly purpose: PasswordActionPurpose
|
|
31
|
+
readonly issuedBy?: string | null
|
|
32
|
+
readonly presentation: AppPresentation
|
|
33
|
+
},
|
|
34
|
+
issuance: ActionIssuance,
|
|
35
|
+
): Promise<IssuedPasswordAction> {
|
|
36
|
+
const presentation = withPersonLocale(input.presentation, parseLocale(input.principal.locale))
|
|
37
|
+
const token = randomToken(32)
|
|
38
|
+
const expiresAt = Math.floor(Date.now() / 1000) + ACTION_TTL_SECONDS
|
|
39
|
+
const id = await services.repositories.passwords.issueActionToken({
|
|
40
|
+
principalId: input.principal.id,
|
|
41
|
+
purpose: input.purpose,
|
|
42
|
+
tokenHash: await hashToken(token),
|
|
43
|
+
issuedBy: input.issuedBy,
|
|
44
|
+
expiresAt,
|
|
45
|
+
app: presentation.app,
|
|
46
|
+
locale: presentation.personLocale,
|
|
47
|
+
}, issuance)
|
|
48
|
+
const path = input.purpose === 'enrollment' ? '/auth/password/enroll' : '/auth/password/reset'
|
|
49
|
+
const url = new URL(`${path}${appQuery(presentation)}`, services.config.issuer)
|
|
50
|
+
// Fragments never reach HTTP access logs; the action page copies it into the POST form in-browser.
|
|
51
|
+
url.hash = new URLSearchParams({ token }).toString()
|
|
52
|
+
const message = input.principal.email === null
|
|
53
|
+
? null
|
|
54
|
+
: await renderEmail(services, {
|
|
55
|
+
to: input.principal.email,
|
|
56
|
+
presentation,
|
|
57
|
+
kind: input.purpose,
|
|
58
|
+
values: { appName: presentation.displayName, recipient: input.principal.email, actionUrl: url.toString(), expiresAt },
|
|
59
|
+
idempotencyKey: `iam-password-${id}`,
|
|
60
|
+
})
|
|
61
|
+
return { id, url: url.toString(), expiresAt, message }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function deliverPasswordAction(services: Services, action: IssuedPasswordAction): Promise<void> {
|
|
65
|
+
if (services.email === null || action.message === null) return
|
|
66
|
+
await services.email.send(action.message, AbortSignal.timeout(EMAIL_SEND_DEADLINE_MS))
|
|
67
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// The language a person reads IAM in. An application declares its own (`app_presentation.locale`); a person may have
|
|
2
|
+
// one of their own, which an application names on an invitation and IAM keeps (`principals.locale`). Every source is
|
|
3
|
+
// a hint for words and nothing else: none of them decides a landing, an origin or an authority.
|
|
4
|
+
|
|
5
|
+
import { DEFAULT_LOCALE, isLocale, type Locale, LOCALE_COOKIE, LOCALE_PARAM } from '@fabrika/auth-core'
|
|
6
|
+
import { clearCookie, readCookie, serializeCookie } from './auth/cookies'
|
|
7
|
+
|
|
8
|
+
/** A year: the cookie is a preference, renewed by each sign-in of a person with a language of their own. */
|
|
9
|
+
const LOCALE_COOKIE_MAX_AGE_SECONDS = 365 * 24 * 60 * 60
|
|
10
|
+
|
|
11
|
+
/** The most `Accept-Language` entries read; a browser sends a handful, and a longer list buys nothing. */
|
|
12
|
+
const MAX_LANGUAGE_RANGES = 32
|
|
13
|
+
|
|
14
|
+
/** A stored or submitted value as a locale IAM speaks, or null: a value it does not (or no longer) speak counts as none. */
|
|
15
|
+
export function parseLocale(value: string | null | undefined): Locale | null {
|
|
16
|
+
return isLocale(value) ? value : null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The language a page speaks, from the most specific source that names one IAM speaks:
|
|
21
|
+
*
|
|
22
|
+
* 1. `person`: the reader's own, as IAM stored it for this flow (an invitation's, a password action's, a principal's);
|
|
23
|
+
* 2. `hint`: a link's or a form's `?locale=`, which IAM writes into the links it mails a person whose language it knows;
|
|
24
|
+
* 3. `remembered`: the language the browser last signed in with here (`LOCALE_COOKIE`);
|
|
25
|
+
* 4. `app`: the application's declared language;
|
|
26
|
+
* 5. `browser`: the best `Accept-Language` match;
|
|
27
|
+
* 6. the installation's default.
|
|
28
|
+
*
|
|
29
|
+
* The application's language ranks above the browser's, so an application that declares one keeps every page it
|
|
30
|
+
* showed before; only a language the person was given wins over it.
|
|
31
|
+
*/
|
|
32
|
+
export function chooseLocale(sources: {
|
|
33
|
+
readonly person?: Locale | null
|
|
34
|
+
readonly hint?: Locale | null
|
|
35
|
+
readonly remembered?: Locale | null
|
|
36
|
+
readonly app?: Locale | null
|
|
37
|
+
readonly browser?: Locale | null
|
|
38
|
+
}): Locale {
|
|
39
|
+
return sources.person ?? sources.hint ?? sources.remembered ?? sources.app ?? sources.browser ?? DEFAULT_LOCALE
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The request's own hints: its `?locale=`, the remembered cookie, and its `Accept-Language`. */
|
|
43
|
+
export function requestLocales(request: Request | null | undefined): {
|
|
44
|
+
readonly hint: Locale | null
|
|
45
|
+
readonly remembered: Locale | null
|
|
46
|
+
readonly browser: Locale | null
|
|
47
|
+
} {
|
|
48
|
+
if (request === null || request === undefined) return { hint: null, remembered: null, browser: null }
|
|
49
|
+
return {
|
|
50
|
+
hint: parseLocale(new URL(request.url).searchParams.get(LOCALE_PARAM)),
|
|
51
|
+
remembered: parseLocale(readCookie(request.headers.get('Cookie'), LOCALE_COOKIE)),
|
|
52
|
+
browser: browserLocale(request.headers.get('Accept-Language')),
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The language of a page that names no application and knows no person: the request's hints, else the default. */
|
|
57
|
+
export function readerLocale(request: Request | null | undefined): Locale {
|
|
58
|
+
return chooseLocale(requestLocales(request))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The best language IAM speaks among an `Accept-Language` header's ranges (RFC 9110 §12.5.4): by weight, ties in the
|
|
63
|
+
* order written, each range matched by its primary subtag (`uk-UA` is `uk`). A range of weight 0, `*` and a malformed
|
|
64
|
+
* weight are skipped. Null when no range names a language IAM speaks.
|
|
65
|
+
*/
|
|
66
|
+
export function browserLocale(header: string | null): Locale | null {
|
|
67
|
+
if (header === null || header === '') return null
|
|
68
|
+
const ranges = header.split(',', MAX_LANGUAGE_RANGES).flatMap((part, index) => {
|
|
69
|
+
const [range = '', ...parameters] = part.split(';').map((piece) => piece.trim())
|
|
70
|
+
let weight = 1
|
|
71
|
+
for (const parameter of parameters) {
|
|
72
|
+
const match = /^q=([01](?:\.\d{0,3})?)$/i.exec(parameter)
|
|
73
|
+
if (match === null) return []
|
|
74
|
+
weight = Number(match[1])
|
|
75
|
+
}
|
|
76
|
+
const primary = range.toLowerCase().split('-')[0] ?? ''
|
|
77
|
+
return weight > 0 && weight <= 1 && isLocale(primary) ? [{ locale: primary, weight, index }] : []
|
|
78
|
+
})
|
|
79
|
+
ranges.sort((a, b) => b.weight - a.weight || a.index - b.index)
|
|
80
|
+
return ranges[0]?.locale ?? null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The `Set-Cookie` a sign-in of a person with `locale` (null for none) answers with: it remembers theirs, or clears a
|
|
85
|
+
* language the browser still holds from someone else; null when there is nothing to change.
|
|
86
|
+
*/
|
|
87
|
+
export function signInLocaleCookie(request: Request, locale: Locale | null): string | null {
|
|
88
|
+
if (locale !== null) {
|
|
89
|
+
return rememberLocaleCookie(locale)
|
|
90
|
+
}
|
|
91
|
+
return readCookie(request.headers.get('Cookie'), LOCALE_COOKIE) === null ? null : clearCookie(LOCALE_COOKIE)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The `Set-Cookie` that remembers `locale` in `LOCALE_COOKIE` on IAM's host, for the pages IAM shows before it knows
|
|
96
|
+
* who is reading: a sign-in page reads it, so a person whose language differs from the application's keeps theirs
|
|
97
|
+
* after the first visit. A sign-in writes it, and so does an invitation's "sign in first", whose sign-in the
|
|
98
|
+
* application's proxy starts without a `?locale=` of IAM's. Host-only, like every cookie IAM writes, and words only:
|
|
99
|
+
* nothing reads it as a claim about the person.
|
|
100
|
+
*/
|
|
101
|
+
export function rememberLocaleCookie(locale: Locale): string {
|
|
102
|
+
return serializeCookie(LOCALE_COOKIE, locale, { httpOnly: true, secure: true, sameSite: 'Lax', maxAge: LOCALE_COOKIE_MAX_AGE_SECONDS })
|
|
103
|
+
}
|