@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,178 @@
|
|
|
1
|
+
import { DEFAULT_LOCALE, displayNameProblem, isLocale, type Locale, LOCALE_PARAM } from '@fabrika/auth-core'
|
|
2
|
+
import type { InstallationBrand } from '@fabrika/proxy-core/brand'
|
|
3
|
+
import { installationBrand } from './installation-brand'
|
|
4
|
+
import { normalizeOrigin } from './origin'
|
|
5
|
+
import { chooseLocale, parseLocale, requestLocales } from './person-locale'
|
|
6
|
+
import type { AppPresentationRow } from './repositories/app-schema'
|
|
7
|
+
import type { Services } from './services'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The application a page or an e-mail is for, as IAM names it: the language, the display name, and where a completed
|
|
11
|
+
* action lands. `app: null` is IAM's own page, in the reader's language else the installation's.
|
|
12
|
+
*/
|
|
13
|
+
export interface AppPresentation {
|
|
14
|
+
readonly app: string | null
|
|
15
|
+
/** The language of the page or the mail (`chooseLocale`). */
|
|
16
|
+
readonly locale: Locale
|
|
17
|
+
/**
|
|
18
|
+
* The reader's own language when this page or mail knows one: stored for the flow, or named by its link. The links
|
|
19
|
+
* and forms it builds carry it (`appQuery`), so the next page keeps it; null when the words follow the application.
|
|
20
|
+
*/
|
|
21
|
+
readonly personLocale: Locale | null
|
|
22
|
+
readonly displayName: string | null
|
|
23
|
+
/** The app's first registered return origin (Control registers one, its public origin), or null for none. */
|
|
24
|
+
readonly landing: string | null
|
|
25
|
+
/**
|
|
26
|
+
* The installation's client logo, which every page draws in place of the fabrika mark, or null for the mark. It is
|
|
27
|
+
* the installation's and not the application's; it rides here because every page already takes its presentation.
|
|
28
|
+
* An e-mail does not show it.
|
|
29
|
+
*/
|
|
30
|
+
readonly brand: InstallationBrand | null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The installation's words with no logo, for an e-mail. A page takes `installationPresentation`, which reads the brand. */
|
|
34
|
+
export const INSTALLATION_PRESENTATION: AppPresentation = {
|
|
35
|
+
app: null,
|
|
36
|
+
locale: DEFAULT_LOCALE,
|
|
37
|
+
personLocale: null,
|
|
38
|
+
displayName: null,
|
|
39
|
+
landing: null,
|
|
40
|
+
brand: null,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Who reads a page or a mail, beside the application it is for. `request` is the page's own: its `?locale=`, the
|
|
45
|
+
* remembered language and its `Accept-Language` pick words; a mail has none. `locale` is the person's own language as
|
|
46
|
+
* IAM stored it for this flow (an invitation's, a password action's, a principal's), which beats every other source.
|
|
47
|
+
*/
|
|
48
|
+
export interface Reader {
|
|
49
|
+
readonly request?: Request | null
|
|
50
|
+
readonly locale?: string | null
|
|
51
|
+
/**
|
|
52
|
+
* The brand a caller already holds, a request's earlier presentation's or `null` for an e-mail, which shows none;
|
|
53
|
+
* given, the brand is not read again.
|
|
54
|
+
*/
|
|
55
|
+
readonly brand?: InstallationBrand | null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The presentation of IAM's own page, in the reader's language (else the installation's) and with its logo. */
|
|
59
|
+
export async function installationPresentation(services: Services, reader: Reader): Promise<AppPresentation> {
|
|
60
|
+
return { ...readerPresentation(INSTALLATION_PRESENTATION, null, reader), brand: await brandOf(services, reader) }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** An app id is far shorter; a longer query value names no application and is not looked up. */
|
|
64
|
+
const MAX_APP_ID_LENGTH = 200
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The presentation of `app`, which is a hint from a query or a form, or a stored row's app. It names an application only
|
|
68
|
+
* when IAM knows one by that id — a reconciled locale or display name, or a registered return origin — and is the
|
|
69
|
+
* installation's otherwise. It decides words and the landing, never authority: the landing is a registered origin, so a
|
|
70
|
+
* forged `app` can only pick another registered application's language and address. The language is the reader's
|
|
71
|
+
* where one is known, else the application's (`chooseLocale`).
|
|
72
|
+
*/
|
|
73
|
+
export async function presentationFor(services: Services, app: string | null, reader: Reader): Promise<AppPresentation> {
|
|
74
|
+
if (app === null || app === '' || app.length > MAX_APP_ID_LENGTH) {
|
|
75
|
+
return installationPresentation(services, reader)
|
|
76
|
+
}
|
|
77
|
+
const [row, landing, brand] = await Promise.all([
|
|
78
|
+
services.repositories.appSchema.getPresentation(app),
|
|
79
|
+
registeredLanding(services, app),
|
|
80
|
+
brandOf(services, reader),
|
|
81
|
+
])
|
|
82
|
+
if (row === null && landing === null) {
|
|
83
|
+
return { ...readerPresentation(INSTALLATION_PRESENTATION, null, reader), brand }
|
|
84
|
+
}
|
|
85
|
+
const declared = parsePresentation(row)
|
|
86
|
+
return readerPresentation(
|
|
87
|
+
{ app, locale: DEFAULT_LOCALE, personLocale: null, displayName: declared.displayName, landing, brand },
|
|
88
|
+
declared.locale,
|
|
89
|
+
reader,
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function brandOf(services: Services, reader: Reader): Promise<InstallationBrand | null> {
|
|
94
|
+
return reader.brand === undefined ? installationBrand(services) : Promise.resolve(reader.brand)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** `presentation` in the person's own `locale`, when there is one; unchanged for none. */
|
|
98
|
+
export function withPersonLocale(presentation: AppPresentation, locale: Locale | null): AppPresentation {
|
|
99
|
+
return locale === null ? presentation : { ...presentation, personLocale: locale, locale }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** `presentation` speaking to `reader`: the person's stored language, the request's hints, then the application's. */
|
|
103
|
+
function readerPresentation(presentation: AppPresentation, appLocale: Locale | null, reader: Reader): AppPresentation {
|
|
104
|
+
const request = requestLocales(reader.request)
|
|
105
|
+
const person = parseLocale(reader.locale) ?? request.hint ?? request.remembered
|
|
106
|
+
return { ...presentation, personLocale: person, locale: chooseLocale({ person, app: appLocale, browser: request.browser }) }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The app's first registered return origin that the origin rule still admits, or null. */
|
|
110
|
+
async function registeredLanding(services: Services, app: string): Promise<string | null> {
|
|
111
|
+
const origins = await services.repositories.handoff.listReturnOrigins(app)
|
|
112
|
+
// A stored origin is written into a CSP directive and a `Location`; one the rule no longer admits is no landing.
|
|
113
|
+
return origins.find((origin) => normalizeOrigin(origin) === origin) ?? null
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A stored row read back: a locale IAM no longer speaks, or a name the rule refuses, counts as not declared. */
|
|
117
|
+
export function parsePresentation(row: AppPresentationRow | null): { locale: Locale | null; displayName: string | null } {
|
|
118
|
+
const locale = row?.locale ?? null
|
|
119
|
+
const displayName = row?.display_name ?? null
|
|
120
|
+
return {
|
|
121
|
+
locale: isLocale(locale) ? locale : null,
|
|
122
|
+
displayName: displayName !== null && displayNameProblem(displayName) === null ? displayName : null,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The URL a completed action sends the browser to inside the app, or null when the app has no registered origin. */
|
|
127
|
+
export function landingUrl(presentation: AppPresentation): string | null {
|
|
128
|
+
return presentation.landing === null ? null : `${presentation.landing}/`
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The installation's console: the registered origin of the first admin audience that has one, or null for none. */
|
|
132
|
+
export async function installationConsole(services: Services): Promise<string | null> {
|
|
133
|
+
for (const app of services.config.adminAudiences) {
|
|
134
|
+
if (app === '' || app.length > MAX_APP_ID_LENGTH) continue
|
|
135
|
+
const landing = await registeredLanding(services, app)
|
|
136
|
+
if (landing !== null) return landing
|
|
137
|
+
}
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Where a browser lands when nothing names its destination — a sign-in, a sign-out or a completed action of no
|
|
143
|
+
* application: the installation's console, whose proxy then signs the browser in through the normal handoff. IAM's
|
|
144
|
+
* own origin serves no page at `/`, and on Zerops the proxy answers it with 403. Without a registered console it is
|
|
145
|
+
* IAM's sign-in page.
|
|
146
|
+
*/
|
|
147
|
+
export async function installationLanding(services: Services): Promise<string> {
|
|
148
|
+
const console = await installationConsole(services)
|
|
149
|
+
return console === null ? new URL('/auth/login', services.config.issuer).toString() : `${console}/`
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The origin off IAM where a completed action of `presentation` lands, which its page's `form-action` must admit:
|
|
154
|
+
* the application's own, else the console. Null when it lands on IAM's own sign-in page.
|
|
155
|
+
*/
|
|
156
|
+
export async function actionLandingOrigin(services: Services, presentation: AppPresentation): Promise<string | null> {
|
|
157
|
+
return presentation.landing ?? await installationConsole(services)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The query a link or a form carries so the next page speaks for the same application and in the same person's
|
|
162
|
+
* language: `?app=` and, where the reader's own language is known, `locale=`. Empty for neither.
|
|
163
|
+
*/
|
|
164
|
+
export function appQuery(presentation: AppPresentation): string {
|
|
165
|
+
const query = new URLSearchParams()
|
|
166
|
+
if (presentation.app !== null) query.set('app', presentation.app)
|
|
167
|
+
if (presentation.personLocale !== null) query.set(LOCALE_PARAM, presentation.personLocale)
|
|
168
|
+
const text = query.toString()
|
|
169
|
+
return text === '' ? '' : `?${text}`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The query that carries only the reader's own language (`?locale=`), for a link or a form to a page whose application
|
|
174
|
+
* travels elsewhere: IAM's sign-in, whose `?app=` names a handoff. Empty when the words follow the application.
|
|
175
|
+
*/
|
|
176
|
+
export function localeQuery(presentation: AppPresentation): string {
|
|
177
|
+
return presentation.personLocale === null ? '' : `?${new URLSearchParams({ [LOCALE_PARAM]: presentation.personLocale })}`
|
|
178
|
+
}
|
package/src/app.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { AuthContext, FetchService, RequestExecutionContext } from '@fabrika/app'
|
|
2
|
+
import { dispatchRpcRequest, serveService, type ServiceHandler } from '@fabrika/app/service'
|
|
3
|
+
import { WORKLOAD_TOKEN_PATH } from '@fabrika/auth-core'
|
|
4
|
+
import type { ResolvedAdmin } from './admin/router'
|
|
5
|
+
import { handleAdmin } from './admin/router'
|
|
6
|
+
import { adminRpcAuth, adminRpcRouter } from './admin/rpc'
|
|
7
|
+
import { handleAuth } from './auth/routes'
|
|
8
|
+
import { readClientAddress } from './client-address'
|
|
9
|
+
import { handleDeviceAuthorization } from './device-http'
|
|
10
|
+
import type { Env } from './env'
|
|
11
|
+
import { handleMembershipTransport } from './membership-http'
|
|
12
|
+
import { handleOAuth } from './oauth-http'
|
|
13
|
+
import { proxyProof, UNPROVEN_CLIENT_ADDRESS } from './proxy-proof'
|
|
14
|
+
import { generateRequestId, readRequestId } from './request-id'
|
|
15
|
+
import { createIamRpc } from './rpc'
|
|
16
|
+
import { handleMintHttp, handleRpcHttp } from './rpc-http'
|
|
17
|
+
import { type IamEndpointId, iamServiceContract, type IamServiceOptions } from './service'
|
|
18
|
+
import { buildServices, type Services } from './services'
|
|
19
|
+
import { handleWorkloadToken } from './workload-token'
|
|
20
|
+
|
|
21
|
+
export interface IamAppContext {
|
|
22
|
+
readonly env: Env
|
|
23
|
+
readonly request: Request
|
|
24
|
+
readonly exec: RequestExecutionContext
|
|
25
|
+
readonly services: Services
|
|
26
|
+
readonly requestId: string
|
|
27
|
+
/** What this composition's ingress says the client is, or `null` when it cannot see one. */
|
|
28
|
+
readonly clientAddress: string | null
|
|
29
|
+
auth: AuthContext
|
|
30
|
+
admin: ResolvedAdmin | null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface IamAppOptions {
|
|
34
|
+
/** Shared secret for the process-only management transport. Empty disables the surface. */
|
|
35
|
+
readonly rpcKey?: string
|
|
36
|
+
/** Shared secret for the process-only proxy mint transport. Empty disables the surface. */
|
|
37
|
+
readonly proxyKey?: string
|
|
38
|
+
/**
|
|
39
|
+
* Mount the membership transport (`src/membership-http.ts`) for applications that have no IAM service binding. Each
|
|
40
|
+
* call is authenticated by its application's own key, not by a composition secret.
|
|
41
|
+
*/
|
|
42
|
+
readonly membershipTransport?: boolean
|
|
43
|
+
/** Process liveness is mounted explicitly because the public Worker does not expose it. */
|
|
44
|
+
readonly health?: boolean
|
|
45
|
+
/**
|
|
46
|
+
* The ONE header this composition's ingress overwrites with the observed client address. Omitted
|
|
47
|
+
* means this installation cannot see its clients and keeps only the account and deployment-wide
|
|
48
|
+
* buckets — never a guess. See `src/client-address.ts`.
|
|
49
|
+
*/
|
|
50
|
+
readonly clientAddressHeader?: string
|
|
51
|
+
/**
|
|
52
|
+
* The ONE header this composition's ingress overwrites with its own correlation id. Omitted means
|
|
53
|
+
* every request gets a generated id. See `src/request-id.ts`.
|
|
54
|
+
*/
|
|
55
|
+
readonly requestIdHeader?: string
|
|
56
|
+
/**
|
|
57
|
+
* The secret the proxy sends in `PROXY_SECRET_HEADER` to prove it wrote the two headers above. Set, a request without
|
|
58
|
+
* it keys on `UNPROVEN_CLIENT_ADDRESS` and gets a generated id; omitted or empty, the headers are read as they arrive.
|
|
59
|
+
* See `src/proxy-proof.ts`.
|
|
60
|
+
*/
|
|
61
|
+
readonly proxySecret?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The service contract this composition serves: a switched-off transport has no row (`IamServiceOptions`). */
|
|
65
|
+
export function iamServiceOptions(options: IamAppOptions): IamServiceOptions {
|
|
66
|
+
return {
|
|
67
|
+
health: options.health === true,
|
|
68
|
+
managementRpc: (options.rpcKey ?? '').trim() !== '',
|
|
69
|
+
mintRpc: (options.proxyKey ?? '').trim() !== '',
|
|
70
|
+
membershipTransport: options.membershipTransport === true,
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Build IAM as a runtime-neutral service: `serveService` over IAM's service contract, which is also what the
|
|
76
|
+
* platform proxy decides from on Zerops. Every row authenticates itself.
|
|
77
|
+
*
|
|
78
|
+
* The native Cloudflare service-binding RPC remains on the WorkerEntrypoint. The optional HTTP
|
|
79
|
+
* transports are mounted by the Bun composition because their shared-secret boundary is process-only.
|
|
80
|
+
*/
|
|
81
|
+
export function createIamApp(options: IamAppOptions = {}): FetchService<Env> {
|
|
82
|
+
const proven = proxyProof(options.proxySecret ?? '')
|
|
83
|
+
const context = async (request: Request, env: Env, exec: RequestExecutionContext): Promise<IamAppContext> => {
|
|
84
|
+
const attributed = await proven(request)
|
|
85
|
+
return {
|
|
86
|
+
env,
|
|
87
|
+
request,
|
|
88
|
+
exec,
|
|
89
|
+
services: buildServices(env),
|
|
90
|
+
requestId: attributed ? readRequestId(request, options.requestIdHeader) : generateRequestId(),
|
|
91
|
+
clientAddress: attributed ? readClientAddress(request, options.clientAddressHeader) : UNPROVEN_CLIENT_ADDRESS,
|
|
92
|
+
auth: anonymousAuth(),
|
|
93
|
+
admin: null,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const serve = (handle: (ctx: IamAppContext) => Response | Promise<Response>): ServiceHandler<Env> => async ({ request, env, exec }) =>
|
|
97
|
+
handle(await context(request, env, exec))
|
|
98
|
+
const auth = serve((ctx) => handleAuth(ctx.request, ctx.services, ctx.env, ctx.exec, ctx.clientAddress, ctx.requestId))
|
|
99
|
+
const oauth = serve((ctx) => handleOAuth(ctx.request, ctx.services, ctx.env, ctx.requestId))
|
|
100
|
+
const admin = serve((ctx) => handleAdmin(ctx.request, ctx.services, ctx.env, ctx.exec, ctx.requestId))
|
|
101
|
+
const mint = serve((ctx) => handleMintHttp(ctx.request, createIamRpc(ctx.env, ctx.exec), options.proxyKey ?? ''))
|
|
102
|
+
const rpc = serve((ctx) => handleRpcHttp(ctx.request, createIamRpc(ctx.env, ctx.exec), options.rpcKey ?? ''))
|
|
103
|
+
return serveService<IamEndpointId, Env>(iamServiceContract(iamServiceOptions(options)), {
|
|
104
|
+
'health': () => Response.json({ status: 'ok' }),
|
|
105
|
+
'jwks': auth,
|
|
106
|
+
'oauth-metadata': oauth,
|
|
107
|
+
'oauth': oauth,
|
|
108
|
+
'auth-root': serve(handleIamAuth),
|
|
109
|
+
'auth': serve(handleIamAuth),
|
|
110
|
+
'mint-session': mint,
|
|
111
|
+
'mint-key': mint,
|
|
112
|
+
'mint-exchange': mint,
|
|
113
|
+
'mint-delegation': mint,
|
|
114
|
+
'admin-rpc': serve((ctx) => adminRpcAuth(ctx.request, ctx, () => dispatchRpcRequest({ router: adminRpcRouter, ctx, request: ctx.request }))),
|
|
115
|
+
'admin-root': admin,
|
|
116
|
+
'admin': admin,
|
|
117
|
+
'membership': serve((ctx) => handleMembershipTransport(ctx.request, ctx.services, createIamRpc(ctx.env, ctx.exec))),
|
|
118
|
+
'rpc-root': rpc,
|
|
119
|
+
'rpc': rpc,
|
|
120
|
+
}, {
|
|
121
|
+
onError(error) {
|
|
122
|
+
console.error('iam request failed:', error instanceof Error ? error.message : 'unknown error')
|
|
123
|
+
return new Response('internal error', {
|
|
124
|
+
status: 500,
|
|
125
|
+
headers: { 'content-type': 'text/plain; charset=utf-8' },
|
|
126
|
+
})
|
|
127
|
+
},
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function anonymousAuth(): AuthContext {
|
|
132
|
+
return {
|
|
133
|
+
ok: true,
|
|
134
|
+
principal: null,
|
|
135
|
+
can: () => false,
|
|
136
|
+
scopedTo: () => [],
|
|
137
|
+
audit: () => Promise.resolve(),
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The mint transport has rows of its own under the reserved `/auth/mint`, so no mint path arrives here. */
|
|
142
|
+
async function handleIamAuth(ctx: IamAppContext): Promise<Response> {
|
|
143
|
+
const pathname = new URL(ctx.request.url).pathname
|
|
144
|
+
if (pathname.startsWith('/auth/device/')) {
|
|
145
|
+
return handleDeviceAuthorization(ctx.request, ctx.services, ctx.clientAddress, ctx.requestId)
|
|
146
|
+
}
|
|
147
|
+
if (pathname === WORKLOAD_TOKEN_PATH) {
|
|
148
|
+
return handleWorkloadToken(ctx.request, ctx.services, ctx.env, ctx.exec, ctx.requestId, ctx.clientAddress)
|
|
149
|
+
}
|
|
150
|
+
return handleAuth(ctx.request, ctx.services, ctx.env, ctx.exec, ctx.clientAddress, ctx.requestId)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The bare application, with no composition options at all — routing only. Neither entrypoint serves
|
|
155
|
+
* it: `src/index.ts` and `src/node/server.ts` each build their own, because each has to name the one
|
|
156
|
+
* headers its own ingress guarantees (see `IamAppOptions.clientAddressHeader` and `requestIdHeader`).
|
|
157
|
+
*/
|
|
158
|
+
export const iamApp = createIamApp()
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cookie (de)serialization for the auth flow. Minimal — only what login/callback/logout need:
|
|
3
|
+
* a `Set-Cookie` builder and a single-cookie reader off the `Cookie` header.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* There is deliberately **no `domain` option**. Every cookie this service writes is host-only: the
|
|
8
|
+
* login cookie belongs to IAM's host and an app's session is written on the app's own host by its
|
|
9
|
+
* proxy ([ADR-0023](../../../../docs/decisions/0023-one-session-per-host.md)). `SESSION_COOKIE` also
|
|
10
|
+
* carries the `__Host-` prefix, which makes a browser refuse any cookie of that name that names a
|
|
11
|
+
* `Domain` — so the option could not do anything but break the cookie it was applied to.
|
|
12
|
+
*/
|
|
13
|
+
export interface CookieOptions {
|
|
14
|
+
/** Lifetime in seconds. Omit for a session cookie; 0 to expire immediately. */
|
|
15
|
+
maxAge?: number
|
|
16
|
+
httpOnly?: boolean
|
|
17
|
+
secure?: boolean
|
|
18
|
+
sameSite?: 'Lax' | 'Strict' | 'None'
|
|
19
|
+
path?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Build a `Set-Cookie` value. */
|
|
23
|
+
export function serializeCookie(name: string, value: string, opts: CookieOptions = {}): string {
|
|
24
|
+
const parts = [`${name}=${value}`, `Path=${opts.path ?? '/'}`]
|
|
25
|
+
if (opts.maxAge !== undefined) {
|
|
26
|
+
parts.push(`Max-Age=${opts.maxAge}`)
|
|
27
|
+
}
|
|
28
|
+
if (opts.httpOnly) {
|
|
29
|
+
parts.push('HttpOnly')
|
|
30
|
+
}
|
|
31
|
+
if (opts.secure) {
|
|
32
|
+
parts.push('Secure')
|
|
33
|
+
}
|
|
34
|
+
if (opts.sameSite) {
|
|
35
|
+
parts.push(`SameSite=${opts.sameSite}`)
|
|
36
|
+
}
|
|
37
|
+
return parts.join('; ')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A `Set-Cookie` that immediately expires `name` (logout / one-shot cookie cleanup). `Secure` is
|
|
42
|
+
* unconditional for the same reason it is on the way in: a deletion must satisfy the same attribute
|
|
43
|
+
* rules as the cookie it replaces, or the browser keeps the original.
|
|
44
|
+
*/
|
|
45
|
+
export function clearCookie(name: string, opts: Pick<CookieOptions, 'path'> = {}): string {
|
|
46
|
+
return serializeCookie(name, '', { ...opts, maxAge: 0, httpOnly: true, secure: true, sameSite: 'Lax' })
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Read a single cookie value out of a raw `Cookie` header. Returns null when absent. */
|
|
50
|
+
export function readCookie(header: string | null, name: string): string | null {
|
|
51
|
+
if (!header) {
|
|
52
|
+
return null
|
|
53
|
+
}
|
|
54
|
+
for (const part of header.split(';')) {
|
|
55
|
+
const eq = part.indexOf('=')
|
|
56
|
+
if (eq === -1) {
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
if (part.slice(0, eq).trim() === name) {
|
|
60
|
+
return part.slice(eq + 1).trim()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why a browser-facing auth request is refused. The page words it in the request's language; a refusal of a handoff
|
|
3
|
+
* names the address it refused, because that is what an operator can act on.
|
|
4
|
+
*/
|
|
5
|
+
export type AuthFailure =
|
|
6
|
+
| { readonly kind: 'no_return_address' }
|
|
7
|
+
| { readonly kind: 'no_handoff_proof' }
|
|
8
|
+
| { readonly kind: 'unregistered_origin'; readonly origin: string }
|
|
9
|
+
| { readonly kind: 'password_disabled' }
|
|
10
|
+
| { readonly kind: 'oidc_disabled' }
|
|
11
|
+
| { readonly kind: 'cross_origin' }
|
|
12
|
+
| { readonly kind: 'malformed_form' }
|
|
13
|
+
| { readonly kind: 'login_unavailable' }
|
|
14
|
+
| { readonly kind: 'logout_unavailable' }
|
|
15
|
+
| { readonly kind: 'oidc_invalid_state' }
|
|
16
|
+
| { readonly kind: 'oidc_exchange_failed' }
|
|
17
|
+
| { readonly kind: 'oidc_invalid_id_token' }
|
|
18
|
+
| { readonly kind: 'not_admitted' }
|
|
19
|
+
/** The browser holds no login of a principal that may enrol a terminal. */
|
|
20
|
+
| { readonly kind: 'terminal_enrolment_not_allowed' }
|
|
21
|
+
| { readonly kind: 'terminal_enrolment_unavailable' }
|
|
22
|
+
/** A terminal identification completed by another browser, twice, too late, or after its login ended. */
|
|
23
|
+
| { readonly kind: 'terminal_completion_refused' }
|
|
24
|
+
/** A refusal of the principal resolution; `reason` is its fixed code, never what the caller sent. */
|
|
25
|
+
| { readonly kind: 'login_refused'; readonly reason: string }
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { AUTH_CALLBACK_PATH, AUTH_CODE_PARAM, AUTH_HANDOFF_CHALLENGE_PARAM, AUTH_HANDOFF_STATE_PARAM } from '@fabrika/auth-core'
|
|
2
|
+
|
|
3
|
+
import { installationConsole } from '../app-presentation'
|
|
4
|
+
import { issueAuthCode, normalizeOrigin, resolveReturnUrl } from '../handoff'
|
|
5
|
+
import { type Services } from '../services'
|
|
6
|
+
import type { AuthFailure } from './failure'
|
|
7
|
+
import { printable, safeRedirect } from './http'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A session handoff in flight ([ADR-0023](../../../../docs/decisions/0023-one-session-per-host.md)):
|
|
11
|
+
* which app the browser came from and where it goes back to. `null` means a login that started on
|
|
12
|
+
* IAM's own host and stays there.
|
|
13
|
+
*/
|
|
14
|
+
export interface Handoff {
|
|
15
|
+
app: string
|
|
16
|
+
returnUrl: string
|
|
17
|
+
state: string
|
|
18
|
+
challenge: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What `?app=` + `?redirect=` turned out to be. `none` is a login with no app at all; `refused`
|
|
23
|
+
* carries the failure of a hard 400.
|
|
24
|
+
*/
|
|
25
|
+
type HandoffReading =
|
|
26
|
+
| { kind: 'none' }
|
|
27
|
+
| { kind: 'handoff'; handoff: Handoff }
|
|
28
|
+
| { kind: 'refused'; failure: AuthFailure }
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Read `?app=` + `?redirect=` as a handoff, validating the return URL against what that app has
|
|
32
|
+
* registered.
|
|
33
|
+
*
|
|
34
|
+
* **Two outcomes once an app is named**: the registry contains the return address and the browser
|
|
35
|
+
* gets a code, or it does not and this is a `400` that names the address. There is no third path —
|
|
36
|
+
* ADR-0023 retired the shared session cookie, so a handoff is the ONLY way a session reaches an app's
|
|
37
|
+
* host and an app whose registry does not cover its own origin cannot log anyone in. Answering that
|
|
38
|
+
* with a fallback would produce a login that succeeds and lands somewhere the browser has no session,
|
|
39
|
+
* i.e. a loop with nothing logged anywhere; answering with the address is what an operator can act on.
|
|
40
|
+
*
|
|
41
|
+
* `none` survives only for a login with no `?app=` at all — someone opening IAM's own login page.
|
|
42
|
+
*/
|
|
43
|
+
export async function readHandoff(
|
|
44
|
+
services: Services,
|
|
45
|
+
app: string | null,
|
|
46
|
+
redirect: string | null,
|
|
47
|
+
state: string | null,
|
|
48
|
+
challenge: string | null,
|
|
49
|
+
): Promise<HandoffReading> {
|
|
50
|
+
if (app === null || app === '') {
|
|
51
|
+
return { kind: 'none' }
|
|
52
|
+
}
|
|
53
|
+
if (redirect === null || redirect === '') {
|
|
54
|
+
return { kind: 'refused', failure: { kind: 'no_return_address' } }
|
|
55
|
+
}
|
|
56
|
+
if (!validHandoffState(state) || !validHandoffChallenge(challenge)) {
|
|
57
|
+
return { kind: 'refused', failure: { kind: 'no_handoff_proof' } }
|
|
58
|
+
}
|
|
59
|
+
const returnUrl = await resolveReturnUrl(services, app, redirect)
|
|
60
|
+
if (returnUrl !== null) {
|
|
61
|
+
return { kind: 'handoff', handoff: { app, returnUrl, state, challenge } }
|
|
62
|
+
}
|
|
63
|
+
const origin = normalizeOrigin(redirect) ?? 'that address'
|
|
64
|
+
return { kind: 'refused', failure: { kind: 'unregistered_origin', origin: printable(origin) } }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The handoff a reading carries, or null — after the caller has already answered a `refused`. */
|
|
68
|
+
export function handoffOf(reading: HandoffReading): Handoff | null {
|
|
69
|
+
return reading.kind === 'handoff' ? reading.handoff : null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* What a login page or an OIDC flight should carry as its return address: the handoff's validated
|
|
74
|
+
* destination when there is one, otherwise an address on IAM's own origin (`safeRedirect`), or null
|
|
75
|
+
* for none, which `issueSession` answers with the installation's landing.
|
|
76
|
+
*/
|
|
77
|
+
export function handoffRedirectOr(handoff: Handoff | null, raw: string | null, services: Services): string | null {
|
|
78
|
+
return handoff === null ? safeRedirect(raw, services.config) : handoff.returnUrl
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* What the login page needs to know about where its submission lands: a handoff's app id and proof,
|
|
83
|
+
* which it carries in hidden fields, and the origin off IAM its `form-action` must admit — the
|
|
84
|
+
* handoff's destination, or the console for a login with no return address. Both origins are
|
|
85
|
+
* REGISTERED ones: the handoff's is taken from the return URL `readHandoff` resolved, so it is the
|
|
86
|
+
* same value the 302 will use and never a caller's spelling.
|
|
87
|
+
*/
|
|
88
|
+
export async function loginDestinationFields(
|
|
89
|
+
services: Services,
|
|
90
|
+
handoff: Handoff | null,
|
|
91
|
+
redirect: string | null,
|
|
92
|
+
): Promise<{ app?: string; redirectOrigin?: string; handoffState?: string; handoffChallenge?: string }> {
|
|
93
|
+
if (handoff === null) {
|
|
94
|
+
const console = redirect === null ? await installationConsole(services) : null
|
|
95
|
+
return console === null ? {} : { redirectOrigin: console }
|
|
96
|
+
}
|
|
97
|
+
const origin = normalizeOrigin(handoff.returnUrl)
|
|
98
|
+
return {
|
|
99
|
+
app: handoff.app,
|
|
100
|
+
handoffState: handoff.state,
|
|
101
|
+
handoffChallenge: handoff.challenge,
|
|
102
|
+
...(origin === null ? {} : { redirectOrigin: origin }),
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Issue the code and build the URL of the app's own callback. */
|
|
107
|
+
export async function handoffLocation(services: Services, handoff: Handoff, parentSessionId: string): Promise<string> {
|
|
108
|
+
const issued = await issueAuthCode(services, {
|
|
109
|
+
app: handoff.app,
|
|
110
|
+
parentSessionId,
|
|
111
|
+
returnUrl: handoff.returnUrl,
|
|
112
|
+
challenge: handoff.challenge,
|
|
113
|
+
})
|
|
114
|
+
const callback = new URL(AUTH_CALLBACK_PATH, handoff.returnUrl)
|
|
115
|
+
callback.searchParams.set(AUTH_CODE_PARAM, issued.code)
|
|
116
|
+
callback.searchParams.set(AUTH_HANDOFF_STATE_PARAM, handoff.state)
|
|
117
|
+
return callback.toString()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** IAM's own sign-in page for the same handoff, which signs a browser in afresh and then continues it. */
|
|
121
|
+
export function handoffLoginUrl(services: Services, handoff: Handoff): string {
|
|
122
|
+
const login = new URL('/auth/login', services.config.issuer)
|
|
123
|
+
login.searchParams.set('app', handoff.app)
|
|
124
|
+
login.searchParams.set('redirect', handoff.returnUrl)
|
|
125
|
+
login.searchParams.set(AUTH_HANDOFF_STATE_PARAM, handoff.state)
|
|
126
|
+
login.searchParams.set(AUTH_HANDOFF_CHALLENGE_PARAM, handoff.challenge)
|
|
127
|
+
return login.toString()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function validHandoffState(value: string | null): value is string {
|
|
131
|
+
return value !== null && /^[A-Za-z0-9_-]{16,128}$/.test(value)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function validHandoffChallenge(value: string | null): value is string {
|
|
135
|
+
return value !== null && /^[A-Za-z0-9_-]{43}$/.test(value)
|
|
136
|
+
}
|
package/src/auth/http.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { readBoundedText } from '../bounded-body'
|
|
2
|
+
import { type Config } from '../services'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Open-redirect guard for a `?redirect=` that stays on IAM: accept only an absolute URL on IAM's OWN
|
|
6
|
+
* origin, and answer null for anything else, which the caller sends to the installation's landing
|
|
7
|
+
* (`installationLanding`). Prevents `/auth/login?redirect=https://evil.example` from bouncing a
|
|
8
|
+
* logged-in user out.
|
|
9
|
+
*
|
|
10
|
+
* It used to also admit the configured session-cookie domain and, locally, any `*.localhost` — the
|
|
11
|
+
* allowlist for the shared cookie's reach. ADR-0023 retired that cookie, so **the return-origin
|
|
12
|
+
* registry is now the only authority on sending a browser to another host**, and it is consulted by
|
|
13
|
+
* `readHandoff` before this function is ever reached. Widening this back would be a second, weaker
|
|
14
|
+
* answer to the same question.
|
|
15
|
+
*
|
|
16
|
+
* The comparison is the whole origin, not the hostname: the issuer already fixes the scheme and port,
|
|
17
|
+
* so there is nothing left for a special case to decide.
|
|
18
|
+
*/
|
|
19
|
+
export function safeRedirect(raw: string | null, config: Config): string | null {
|
|
20
|
+
if (!raw) {
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const target = new URL(raw)
|
|
25
|
+
return target.origin === new URL(config.issuer).origin ? target.toString() : null
|
|
26
|
+
} catch {
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function sameOrigin(request: Request, config: Config): boolean {
|
|
32
|
+
const fetchSite = request.headers.get('Sec-Fetch-Site')
|
|
33
|
+
if (fetchSite !== null && fetchSite !== 'same-origin') return false
|
|
34
|
+
const originHeader = request.headers.get('Origin')
|
|
35
|
+
// `Origin: null` is what a real browser sends here, and it is not an attack. Every page these
|
|
36
|
+
// forms live on carries `Referrer-Policy: no-referrer`, and Fetch's "append a request Origin
|
|
37
|
+
// header" serializes the origin as `null` under that policy — so a same-origin form POST arrives
|
|
38
|
+
// with `Origin: null` and no `Referer` at all. `Sec-Fetch-Site` is then the only same-origin proof
|
|
39
|
+
// left, and it is the one a page cannot forge: `Sec-` is a forbidden header prefix, so the browser
|
|
40
|
+
// is its only writer, and a cross-site post is already refused above. Measured in Chromium 149,
|
|
41
|
+
// not assumed.
|
|
42
|
+
if (originHeader === 'null') return fetchSite === 'same-origin'
|
|
43
|
+
const origin = originHeader ?? originOf(request.headers.get('Referer'))
|
|
44
|
+
if (origin === null) return false
|
|
45
|
+
try {
|
|
46
|
+
return new URL(origin).origin === new URL(config.issuer).origin
|
|
47
|
+
} catch {
|
|
48
|
+
return false
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function originOf(value: string | null): string | null {
|
|
53
|
+
if (value === null) return null
|
|
54
|
+
try {
|
|
55
|
+
return new URL(value).origin
|
|
56
|
+
} catch {
|
|
57
|
+
return null
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function readForm(request: Request): Promise<URLSearchParams | null> {
|
|
62
|
+
const contentType = request.headers.get('Content-Type') ?? ''
|
|
63
|
+
if (!contentType.toLowerCase().startsWith('application/x-www-form-urlencoded')) return null
|
|
64
|
+
const body = await readBoundedText(request, 16_384)
|
|
65
|
+
return body.ok ? new URLSearchParams(body.text) : null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function stringValue(value: string | null): string | null {
|
|
69
|
+
return typeof value === 'string' ? value : null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function methodNotAllowed(): Response {
|
|
73
|
+
return new Response('Method not allowed', { status: 405, headers: { Allow: 'GET, POST' } })
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Headers for a 302 out of this surface. `no-store` belongs on every one of them, not just the
|
|
78
|
+
* password pages: these are the responses that carry the 30-day session cookie and, on the handoff
|
|
79
|
+
* path, the single-use code in the `Location` itself.
|
|
80
|
+
*/
|
|
81
|
+
export function redirectHeaders(location: string): Headers {
|
|
82
|
+
return new Headers({ location, 'cache-control': 'no-store' })
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Keep an untrusted value fit for an error page: printable ASCII, bounded. */
|
|
86
|
+
export function printable(value: string): string {
|
|
87
|
+
return value.replace(/[^\x20-\x7e]/g, '').slice(0, 120)
|
|
88
|
+
}
|