@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,249 @@
|
|
|
1
|
+
import { SESSION_COOKIE } from '@fabrika/auth-core'
|
|
2
|
+
import { iamAdministrationScope } from '@fabrika/iam-contract'
|
|
3
|
+
import { IAM_APP } from '../admin/router'
|
|
4
|
+
import { installationLanding, installationPresentation } from '../app-presentation'
|
|
5
|
+
import { sessionUsable } from '../auth'
|
|
6
|
+
import { type AuthenticationMethod, type PrincipalRow, principalStatus, type SessionRow } from '../db'
|
|
7
|
+
import { type RequestContext } from '../env'
|
|
8
|
+
import { logError } from '../log'
|
|
9
|
+
import { randomToken } from '../oidc'
|
|
10
|
+
import { parseLocale, signInLocaleCookie } from '../person-locale'
|
|
11
|
+
import { resolvePermissionsAt } from '../resolve'
|
|
12
|
+
import { hashToken } from '../secret'
|
|
13
|
+
import { type Services } from '../services'
|
|
14
|
+
import { clearCookie, readCookie, serializeCookie } from './cookies'
|
|
15
|
+
import { type Handoff, handoffLocation } from './handoff'
|
|
16
|
+
|
|
17
|
+
import { authFailurePage } from '../password-pages'
|
|
18
|
+
import { redirectHeaders, safeRedirect, sameOrigin } from './http'
|
|
19
|
+
|
|
20
|
+
import { endingFlightCookies } from './oidc-flight'
|
|
21
|
+
|
|
22
|
+
/** SSO session lifetime — the long-lived credential a browser carries (30 days). */
|
|
23
|
+
const SESSION_TTL_SECONDS = 30 * 24 * 60 * 60
|
|
24
|
+
|
|
25
|
+
/** The caller's IAM session, if the browser already holds a valid one on this host. */
|
|
26
|
+
export async function currentSession(request: Request, services: Services) {
|
|
27
|
+
const raw = readCookie(request.headers.get('Cookie'), SESSION_COOKIE)
|
|
28
|
+
if (raw === null) {
|
|
29
|
+
return null
|
|
30
|
+
}
|
|
31
|
+
const session = await services.repositories.sessions.getActiveSessionByHash(await hashToken(raw))
|
|
32
|
+
if (session === null) {
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
// A child session cannot father another: only the IAM login itself may authorize a handoff.
|
|
36
|
+
if (session.app !== null) {
|
|
37
|
+
return null
|
|
38
|
+
}
|
|
39
|
+
// And a session whose method is no longer enabled must not silently father a handoff.
|
|
40
|
+
return sessionUsable(session, services.config) ? session : null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A login a handoff may continue: the browser's IAM session and its principal, an active user. */
|
|
44
|
+
export interface CurrentLogin {
|
|
45
|
+
readonly session: SessionRow
|
|
46
|
+
readonly principal: PrincipalRow
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The caller's IAM login when its principal is an active user, else null — which signs in afresh. */
|
|
50
|
+
export async function currentLogin(request: Request, services: Services): Promise<CurrentLogin | null> {
|
|
51
|
+
const session = await currentSession(request, services)
|
|
52
|
+
if (session === null) return null
|
|
53
|
+
const principal = await services.repositories.principals.getPrincipalById(session.principal_id)
|
|
54
|
+
if (principal === null || principal.type !== 'user' || principalStatus(principal) !== 'active') return null
|
|
55
|
+
return { session, principal }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Open a login: the session and its `auth_log` row are written in one batch, before the answer. A login that cannot
|
|
60
|
+
* be logged opens no session and answers 503. A principal that administers at IAM, the installation or the people of
|
|
61
|
+
* an application (ADR-0058), is logged `privileged`.
|
|
62
|
+
*/
|
|
63
|
+
export async function issueSession(
|
|
64
|
+
request: Request,
|
|
65
|
+
services: Services,
|
|
66
|
+
ctx: RequestContext,
|
|
67
|
+
input: {
|
|
68
|
+
principalId: string
|
|
69
|
+
idpSub?: string | null
|
|
70
|
+
email?: string | null
|
|
71
|
+
authenticationMethod?: AuthenticationMethod
|
|
72
|
+
/** Where the login lands; null for no destination of its own, which is the installation's landing. */
|
|
73
|
+
redirect: string | null
|
|
74
|
+
reason: string
|
|
75
|
+
/** The correlation id the composition root allowed; see `src/request-id.ts`. */
|
|
76
|
+
requestId: string
|
|
77
|
+
clearOidc?: boolean
|
|
78
|
+
/** When set, land on the app's callback with a code instead of on `redirect` (ADR-0022). */
|
|
79
|
+
handoff?: Handoff | null
|
|
80
|
+
},
|
|
81
|
+
): Promise<Response> {
|
|
82
|
+
const sessionToken = randomToken(32)
|
|
83
|
+
const expiresAt = Math.floor(Date.now() / 1000) + SESSION_TTL_SECONDS
|
|
84
|
+
let sessionId: string
|
|
85
|
+
try {
|
|
86
|
+
sessionId = await services.repositories.sessions.createSession({
|
|
87
|
+
tokenHash: await hashToken(sessionToken),
|
|
88
|
+
principalId: input.principalId,
|
|
89
|
+
idpSub: input.idpSub,
|
|
90
|
+
email: input.email,
|
|
91
|
+
authenticationMethod: input.authenticationMethod,
|
|
92
|
+
expiresAt,
|
|
93
|
+
}, {
|
|
94
|
+
requestId: input.requestId,
|
|
95
|
+
app: IAM_APP,
|
|
96
|
+
kind: 'authenticate',
|
|
97
|
+
principalId: input.principalId,
|
|
98
|
+
decision: 'allow',
|
|
99
|
+
reason: input.reason,
|
|
100
|
+
privileged: await administersIam(services, input.principalId),
|
|
101
|
+
})
|
|
102
|
+
} catch (err) {
|
|
103
|
+
logError(`login could not be recorded for request '${input.requestId}'`, err)
|
|
104
|
+
return authFailurePage(await installationPresentation(services, { request }), { kind: 'login_unavailable' }, 503)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The IAM cookie is set either way: it is what spares the NEXT handoff, to this or any other app, a
|
|
108
|
+
// password. What changes is where the browser goes — its own destination, or the app's callback.
|
|
109
|
+
const location = input.handoff == null
|
|
110
|
+
? input.redirect ?? await installationLanding(services)
|
|
111
|
+
: await handoffLocation(services, input.handoff, sessionId)
|
|
112
|
+
const headers = redirectHeaders(location)
|
|
113
|
+
headers.append('Set-Cookie', sessionCookie(sessionToken))
|
|
114
|
+
// A person with a language of their own keeps it on this browser's next sign-in page here (`LOCALE_COOKIE`); one
|
|
115
|
+
// without clears the language a previous person left on a shared browser.
|
|
116
|
+
// The session has committed and the cookie picks words only: a failed read leaves the remembered language as it is.
|
|
117
|
+
const locale = await services.repositories.principals.getPrincipalById(input.principalId).then(
|
|
118
|
+
(principal) => signInLocaleCookie(request, parseLocale(principal?.locale)),
|
|
119
|
+
(err: unknown) => {
|
|
120
|
+
logError(`the language of login for request '${input.requestId}' could not be read`, err)
|
|
121
|
+
return null
|
|
122
|
+
},
|
|
123
|
+
)
|
|
124
|
+
if (locale !== null) {
|
|
125
|
+
headers.append('Set-Cookie', locale)
|
|
126
|
+
}
|
|
127
|
+
if (input.clearOidc === true) {
|
|
128
|
+
for (const cookie of endingFlightCookies()) {
|
|
129
|
+
headers.append('Set-Cookie', cookie)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return new Response(null, { status: 302, headers })
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Answer a refused login after its `deny` row is in `auth_log`. A refusal that cannot be logged answers 503 instead,
|
|
137
|
+
* so no login attempt goes unrecorded.
|
|
138
|
+
*/
|
|
139
|
+
/** The fixed `auth_log.reason` of a refused login. A reason never carries what the caller submitted. */
|
|
140
|
+
export type LoginRefusal =
|
|
141
|
+
| 'password_invalid'
|
|
142
|
+
| 'password_throttled'
|
|
143
|
+
| 'password_malformed'
|
|
144
|
+
| 'handoff_refused'
|
|
145
|
+
| 'login_cross_origin'
|
|
146
|
+
| 'oidc_invalid_state'
|
|
147
|
+
| 'oidc_code_exchange_failed'
|
|
148
|
+
| 'oidc_invalid_id_token'
|
|
149
|
+
| 'oidc_not_admitted'
|
|
150
|
+
| 'oidc_disabled'
|
|
151
|
+
| 'oidc_unknown_principal'
|
|
152
|
+
| 'oidc_external_principal'
|
|
153
|
+
| 'local_login_refused'
|
|
154
|
+
|
|
155
|
+
export async function refuseLogin(
|
|
156
|
+
services: Services,
|
|
157
|
+
request: Request,
|
|
158
|
+
entry: { requestId: string; principalId: string | null; reason: LoginRefusal },
|
|
159
|
+
answer: () => Response | Promise<Response>,
|
|
160
|
+
): Promise<Response> {
|
|
161
|
+
try {
|
|
162
|
+
await services.repositories.audit.writeAuthLog({
|
|
163
|
+
requestId: entry.requestId,
|
|
164
|
+
app: IAM_APP,
|
|
165
|
+
kind: 'authenticate',
|
|
166
|
+
principalId: entry.principalId,
|
|
167
|
+
decision: 'deny',
|
|
168
|
+
reason: entry.reason,
|
|
169
|
+
})
|
|
170
|
+
} catch (err) {
|
|
171
|
+
logError(`a refused login could not be recorded for request '${entry.requestId}'`, err)
|
|
172
|
+
return authFailurePage(await installationPresentation(services, { request }), { kind: 'login_unavailable' }, 503)
|
|
173
|
+
}
|
|
174
|
+
return answer()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Whether the principal administers at IAM, the installation or the people of an application — what marks its login privileged. */
|
|
178
|
+
export async function administersIam(services: Services, principalId: string): Promise<boolean> {
|
|
179
|
+
const principal = await services.repositories.principals.getPrincipalById(principalId)
|
|
180
|
+
if (principal === null) return false
|
|
181
|
+
return iamAdministrationScope(await resolvePermissionsAt(services, principal, IAM_APP)) !== null
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Revoke the login and clear the cookie. Same-origin only, like every other mutating form here:
|
|
186
|
+
* `px_session` is `SameSite=Lax`, so a cross-site top-level GET carried it, and because every app
|
|
187
|
+
* session hangs off this one, that single request logged the human out of every host at once.
|
|
188
|
+
* The revocation and its `logout` row in `auth_log` are one batch; one that cannot be written answers 503
|
|
189
|
+
* and leaves the session as it was.
|
|
190
|
+
*/
|
|
191
|
+
export async function handleLogout(request: Request, services: Services, requestId: string): Promise<Response> {
|
|
192
|
+
// Logout names no application, like its confirmation page: these answers speak the reader's language.
|
|
193
|
+
if (!sameOrigin(request, services.config)) {
|
|
194
|
+
return authFailurePage(await installationPresentation(services, { request }), { kind: 'cross_origin' }, 403)
|
|
195
|
+
}
|
|
196
|
+
if (!await endLogin(request, services, requestId)) {
|
|
197
|
+
return authFailurePage(await installationPresentation(services, { request }), { kind: 'logout_unavailable' }, 503)
|
|
198
|
+
}
|
|
199
|
+
const url = new URL(request.url)
|
|
200
|
+
return signedOutRedirect(safeRedirect(url.searchParams.get('redirect'), services.config) ?? await installationLanding(services))
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* End the browser's IAM login, and with it every app session derived from it. The revocation and its `logout` row are
|
|
205
|
+
* one batch; false when that batch could not be written, and the login stands. The caller checks `sameOrigin` first.
|
|
206
|
+
*/
|
|
207
|
+
export async function endLogin(request: Request, services: Services, requestId: string): Promise<boolean> {
|
|
208
|
+
const cookie = readCookie(request.headers.get('Cookie'), SESSION_COOKIE)
|
|
209
|
+
if (!cookie) return true
|
|
210
|
+
try {
|
|
211
|
+
await services.repositories.sessions.revokeSessionByHash(await hashToken(cookie), { requestId, app: IAM_APP, reason: 'logout' })
|
|
212
|
+
} catch (err) {
|
|
213
|
+
logError(`logout could not be recorded for request '${requestId}'`, err)
|
|
214
|
+
return false
|
|
215
|
+
}
|
|
216
|
+
return true
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The 302 out of an ended login, which clears its cookie. */
|
|
220
|
+
export function signedOutRedirect(location: string): Response {
|
|
221
|
+
const headers = redirectHeaders(location)
|
|
222
|
+
headers.append('Set-Cookie', clearCookie(SESSION_COOKIE, { path: '/' }))
|
|
223
|
+
return new Response(null, { status: 302, headers })
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Build the long-lived login `Set-Cookie` — HOST-ONLY, on IAM's own host and nowhere else (ADR-0023).
|
|
228
|
+
*
|
|
229
|
+
* `SESSION_COOKIE` carries the `__Host-` prefix, which a browser honours only when the cookie has
|
|
230
|
+
* `Secure`, `Path=/` and NO `Domain`. All three are set here; adding a `Domain` would make the browser
|
|
231
|
+
* drop the cookie outright rather than widen it.
|
|
232
|
+
*
|
|
233
|
+
* **`Secure` is unconditional**, and it used to be derived from the issuer's scheme. The prefix
|
|
234
|
+
* removed the decision: without `Secure` the browser refuses the cookie whatever the transport, so a
|
|
235
|
+
* conditional could only ever produce a cookie nothing would store. It stays correct behind a
|
|
236
|
+
* TLS-TERMINATING BALANCER (the socket is plain HTTP, the browser spoke HTTPS) and over plain HTTP on
|
|
237
|
+
* `localhost` / `*.localhost`, which browsers treat as potentially trustworthy. An installation
|
|
238
|
+
* served over cleartext HTTP on a public hostname can hold no session at all — which is the correct
|
|
239
|
+
* outcome for a 30-day credential, not a regression to work around.
|
|
240
|
+
*/
|
|
241
|
+
function sessionCookie(value: string): string {
|
|
242
|
+
return serializeCookie(SESSION_COOKIE, value, {
|
|
243
|
+
httpOnly: true,
|
|
244
|
+
secure: true,
|
|
245
|
+
sameSite: 'Lax',
|
|
246
|
+
maxAge: SESSION_TTL_SECONDS,
|
|
247
|
+
path: '/',
|
|
248
|
+
})
|
|
249
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { FABRIKA_COOKIE_PREFIX, IDENTIFICATION_QUERY_PARAMETER, permits, SESSION_COOKIE } from '@fabrika/auth-core'
|
|
2
|
+
import { IAM_ACTIONS } from '@fabrika/iam-contract'
|
|
3
|
+
import { uuidv7 } from '@fabrika/platform'
|
|
4
|
+
import { IAM_APP } from '../admin/router'
|
|
5
|
+
import { type AppPresentation, INSTALLATION_PRESENTATION, presentationFor } from '../app-presentation'
|
|
6
|
+
import { auditLabelOf, DEFAULT_TERMINAL_SESSION_TTL_SECONDS, type TerminalRow } from '../db'
|
|
7
|
+
import { normalizeOrigin, resolveReturnUrl } from '../handoff'
|
|
8
|
+
import { IDENTIFICATION_TTL_SECONDS } from '../identification'
|
|
9
|
+
import { installationBrand } from '../installation-brand'
|
|
10
|
+
import { logError } from '../log'
|
|
11
|
+
import { randomToken } from '../oidc'
|
|
12
|
+
import { authFailurePage, TERMINAL_ENROL_PATH, terminalEnrolledPage, terminalEnrolPage } from '../password-pages'
|
|
13
|
+
import { readerLocale } from '../person-locale'
|
|
14
|
+
import { unixNow } from '../repositories/sql'
|
|
15
|
+
import { resolvePermissionsAt } from '../resolve'
|
|
16
|
+
import { hashToken } from '../secret'
|
|
17
|
+
import type { Services } from '../services'
|
|
18
|
+
import { terminalLabel } from '../terminals'
|
|
19
|
+
import { clearCookie, readCookie, serializeCookie } from './cookies'
|
|
20
|
+
import { type Handoff, handoffLocation } from './handoff'
|
|
21
|
+
import { printable, readForm, redirectHeaders, sameOrigin, stringValue } from './http'
|
|
22
|
+
import { type CurrentLogin, currentLogin } from './sessions'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The cookie that makes a browser a terminal (ADR-0067 §1). It is not a session: no proxy reads it and it authorizes no
|
|
26
|
+
* request. IAM reads it on its own host, only to start an identification.
|
|
27
|
+
*/
|
|
28
|
+
export const TERMINAL_COOKIE = `${FABRIKA_COOKIE_PREFIX}terminal`
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 400 days, the longest lifetime a browser keeps (RFC 6265bis caps `Max-Age` there, and Chromium enforces it). A terminal
|
|
32
|
+
* ends when an administrator revokes it, not when a cookie expires, so it asks for the most a browser grants.
|
|
33
|
+
*/
|
|
34
|
+
export const TERMINAL_COOKIE_MAX_AGE_SECONDS = 400 * 24 * 60 * 60
|
|
35
|
+
|
|
36
|
+
/** The longest identification id the completion looks up; IAM's own are far shorter. */
|
|
37
|
+
const MAX_IDENTIFICATION_ID_LENGTH = 128
|
|
38
|
+
|
|
39
|
+
/** Where an application's proxy sends a signed-out terminal's browser; the proxy's `SIGNOUT_RELEASE_PATH` names it. */
|
|
40
|
+
export const TERMINAL_RELEASE_PATH = '/auth/terminals/release'
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* `GET` renders the enrolment form to a login that may enrol; `POST` enrols. A browser without a login is sent to sign
|
|
44
|
+
* in and back here. The form and every refusal name no application, so they speak the reader's language (`readerLocale`).
|
|
45
|
+
*/
|
|
46
|
+
export async function handleTerminalEnrolment(request: Request, services: Services, requestId: string): Promise<Response> {
|
|
47
|
+
if (request.method === 'POST') {
|
|
48
|
+
return enrol(request, services, requestId)
|
|
49
|
+
}
|
|
50
|
+
const login = await currentLogin(request, services)
|
|
51
|
+
if (login === null) {
|
|
52
|
+
const back = new URL(TERMINAL_ENROL_PATH, services.config.issuer).toString()
|
|
53
|
+
return new Response(null, { status: 302, headers: redirectHeaders(`/auth/login?redirect=${encodeURIComponent(back)}`) })
|
|
54
|
+
}
|
|
55
|
+
if (!await mayEnrol(services, login)) {
|
|
56
|
+
return authFailurePage(readerFailure(request), { kind: 'terminal_enrolment_not_allowed' }, 403)
|
|
57
|
+
}
|
|
58
|
+
return terminalEnrolPage({ brand: await installationBrand(services), locale: readerLocale(request) })
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A refusal that names no application (the enrolment's, a terminal completion's): IAM's own words, without the logo, in
|
|
63
|
+
* the reader's language.
|
|
64
|
+
*/
|
|
65
|
+
function readerFailure(request: Request): AppPresentation {
|
|
66
|
+
return { ...INSTALLATION_PRESENTATION, locale: readerLocale(request) }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Enrol this browser, in one batch: the terminal's service principal and its row, the end of the administrator's login
|
|
71
|
+
* here with its `logout` row, and the enrolment's audit record. Only then are the terminal cookie set and the login
|
|
72
|
+
* cookie cleared, so the terminal is never left signed in as the person who set it up.
|
|
73
|
+
*/
|
|
74
|
+
async function enrol(request: Request, services: Services, requestId: string): Promise<Response> {
|
|
75
|
+
if (!sameOrigin(request, services.config)) {
|
|
76
|
+
return authFailurePage(readerFailure(request), { kind: 'cross_origin' }, 403)
|
|
77
|
+
}
|
|
78
|
+
const login = await currentLogin(request, services)
|
|
79
|
+
if (login === null || !await mayEnrol(services, login)) {
|
|
80
|
+
return authFailurePage(readerFailure(request), { kind: 'terminal_enrolment_not_allowed' }, 403)
|
|
81
|
+
}
|
|
82
|
+
const form = await readForm(request)
|
|
83
|
+
if (form === null) {
|
|
84
|
+
return authFailurePage(readerFailure(request), { kind: 'malformed_form' }, 400)
|
|
85
|
+
}
|
|
86
|
+
const label = terminalLabel(stringValue(form.get('label')) ?? '')
|
|
87
|
+
if (label === null) {
|
|
88
|
+
return terminalEnrolPage({ brand: await installationBrand(services), locale: readerLocale(request), labelProblem: true })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const terminalId = uuidv7()
|
|
92
|
+
const token = randomToken(32)
|
|
93
|
+
const { principals, terminals, sessions, audit } = services.repositories
|
|
94
|
+
try {
|
|
95
|
+
await audit.commit([
|
|
96
|
+
principals.createServiceStatement(terminalId, label),
|
|
97
|
+
terminals.enrolStatement({
|
|
98
|
+
principalId: terminalId,
|
|
99
|
+
label,
|
|
100
|
+
enrolledBy: login.principal.id,
|
|
101
|
+
tokenHash: await hashToken(token),
|
|
102
|
+
sessionTtl: DEFAULT_TERMINAL_SESSION_TTL_SECONDS,
|
|
103
|
+
}),
|
|
104
|
+
sessions.revokeStatement(login.session.id),
|
|
105
|
+
audit.authLogStatement({
|
|
106
|
+
requestId,
|
|
107
|
+
app: IAM_APP,
|
|
108
|
+
kind: 'logout',
|
|
109
|
+
principalId: login.principal.id,
|
|
110
|
+
decision: 'allow',
|
|
111
|
+
reason: 'terminal_enrolment',
|
|
112
|
+
}),
|
|
113
|
+
audit.eventStatement({
|
|
114
|
+
requestId,
|
|
115
|
+
principalId: login.principal.id,
|
|
116
|
+
principalLabel: auditLabelOf(login.principal),
|
|
117
|
+
app: IAM_APP,
|
|
118
|
+
action: 'iam.terminal.enrol',
|
|
119
|
+
resourceType: 'terminal',
|
|
120
|
+
resourceId: terminalId,
|
|
121
|
+
metadata: { label },
|
|
122
|
+
}),
|
|
123
|
+
])
|
|
124
|
+
} catch (err) {
|
|
125
|
+
logError(`terminal enrolment could not be recorded for request '${requestId}'`, err)
|
|
126
|
+
return authFailurePage(readerFailure(request), { kind: 'terminal_enrolment_unavailable' }, 503)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const response = terminalEnrolledPage(label, await installationBrand(services), readerLocale(request))
|
|
130
|
+
response.headers.append('Set-Cookie', terminalCookie(token))
|
|
131
|
+
response.headers.append('Set-Cookie', clearCookie(SESSION_COOKIE, { path: '/' }))
|
|
132
|
+
return response
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Whether the login's principal holds `iam.terminals.manage` at IAM, resolved live. */
|
|
136
|
+
async function mayEnrol(services: Services, login: CurrentLogin): Promise<boolean> {
|
|
137
|
+
return permits(await resolvePermissionsAt(services, login.principal, IAM_APP), IAM_ACTIONS.TERMINALS_MANAGE)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** HOST-ONLY on IAM's host, like the login cookie: the `__Host-` prefix makes the browser require `Secure`, `Path=/`, no `Domain`. */
|
|
141
|
+
function terminalCookie(value: string): string {
|
|
142
|
+
return serializeCookie(TERMINAL_COOKIE, value, {
|
|
143
|
+
httpOnly: true,
|
|
144
|
+
secure: true,
|
|
145
|
+
sameSite: 'Lax',
|
|
146
|
+
maxAge: TERMINAL_COOKIE_MAX_AGE_SECONDS,
|
|
147
|
+
path: '/',
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The enrolled terminal this browser is, by its cookie, with the cookie's value; null for any other browser. */
|
|
152
|
+
async function browserTerminal(request: Request, services: Services): Promise<{ terminal: TerminalRow; token: string } | null> {
|
|
153
|
+
const token = readCookie(request.headers.get('Cookie'), TERMINAL_COOKIE)
|
|
154
|
+
if (token === null) {
|
|
155
|
+
return null
|
|
156
|
+
}
|
|
157
|
+
const terminal = await services.repositories.terminals.getActiveByTokenHash(await hashToken(token))
|
|
158
|
+
return terminal === null ? null : { terminal, token }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Send a terminal's browser to the application to identify the person at it (ADR-0067 §2): when the browser is an
|
|
163
|
+
* enrolled terminal, the handoff's application is one of its asserters and declares an identification page. The
|
|
164
|
+
* identification holds the handoff, so IAM alone issues the code once the application names the person. Null for any
|
|
165
|
+
* other sign-in, which goes on as usual. The terminal cookie is refreshed, so a terminal in use never reaches its cap.
|
|
166
|
+
*/
|
|
167
|
+
export async function identificationRedirect(
|
|
168
|
+
request: Request,
|
|
169
|
+
services: Services,
|
|
170
|
+
handoff: Handoff,
|
|
171
|
+
presentation: AppPresentation,
|
|
172
|
+
requestId: string,
|
|
173
|
+
): Promise<Response | null> {
|
|
174
|
+
const browser = await browserTerminal(request, services)
|
|
175
|
+
if (browser === null) {
|
|
176
|
+
return null
|
|
177
|
+
}
|
|
178
|
+
const { terminals, appSchema } = services.repositories
|
|
179
|
+
const terminalId = browser.terminal.principal_id
|
|
180
|
+
if (await terminals.getAsserter(terminalId, handoff.app) === null) {
|
|
181
|
+
return null
|
|
182
|
+
}
|
|
183
|
+
const identify = await appSchema.getTerminalIdentifyPath(handoff.app)
|
|
184
|
+
const origin = normalizeOrigin(handoff.returnUrl)
|
|
185
|
+
if (identify === null || origin === null) {
|
|
186
|
+
return null
|
|
187
|
+
}
|
|
188
|
+
const id = randomToken(32)
|
|
189
|
+
try {
|
|
190
|
+
await terminals.createIdentification({
|
|
191
|
+
idHash: await hashToken(id),
|
|
192
|
+
terminalId,
|
|
193
|
+
app: handoff.app,
|
|
194
|
+
returnUrl: handoff.returnUrl,
|
|
195
|
+
handoffState: handoff.state,
|
|
196
|
+
handoffChallenge: handoff.challenge,
|
|
197
|
+
expiresAt: unixNow() + IDENTIFICATION_TTL_SECONDS,
|
|
198
|
+
}, {
|
|
199
|
+
requestId,
|
|
200
|
+
app: IAM_APP,
|
|
201
|
+
kind: 'authenticate',
|
|
202
|
+
principalId: null,
|
|
203
|
+
decision: 'allow',
|
|
204
|
+
reason: 'terminal_identification',
|
|
205
|
+
terminal: { terminalId, assertedBy: handoff.app },
|
|
206
|
+
})
|
|
207
|
+
} catch (err) {
|
|
208
|
+
logError(`a terminal identification could not be opened for request '${requestId}'`, err)
|
|
209
|
+
return authFailurePage(presentation, { kind: 'login_unavailable' }, 503)
|
|
210
|
+
}
|
|
211
|
+
const location = new URL(identify, origin)
|
|
212
|
+
location.searchParams.set(IDENTIFICATION_QUERY_PARAMETER, id)
|
|
213
|
+
const headers = redirectHeaders(location.toString())
|
|
214
|
+
headers.append('Set-Cookie', terminalCookie(browser.token))
|
|
215
|
+
return new Response(null, { status: 302, headers })
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* `GET /auth/terminals/complete?identification=` (ADR-0067 §3): the terminal's browser arrives here from the application
|
|
220
|
+
* after its assertion, and IAM issues the handoff's code to it. Only the browser of the identification's own terminal
|
|
221
|
+
* completes it, once, within the completion window, while the asserted login is still valid; anything else is refused
|
|
222
|
+
* and issues nothing. The code goes to the proxy's callback on the handoff's return origin, so the application never
|
|
223
|
+
* sees it, and the proxy redeems it only with the verifier it set on the browser that started the sign-in.
|
|
224
|
+
*
|
|
225
|
+
* A GET without a same-origin check, like the release: the application sends the browser here from its own host.
|
|
226
|
+
* Completing an identification the application already asserted is all a cross-site request can do.
|
|
227
|
+
*/
|
|
228
|
+
export async function handleTerminalCompletion(request: Request, services: Services, requestId: string): Promise<Response> {
|
|
229
|
+
const id = new URL(request.url).searchParams.get(IDENTIFICATION_QUERY_PARAMETER) ?? ''
|
|
230
|
+
const browser = await browserTerminal(request, services)
|
|
231
|
+
if (browser === null || id === '' || id.length > MAX_IDENTIFICATION_ID_LENGTH) {
|
|
232
|
+
return authFailurePage(readerFailure(request), { kind: 'terminal_completion_refused' }, 403)
|
|
233
|
+
}
|
|
234
|
+
const { terminals, sessions } = services.repositories
|
|
235
|
+
try {
|
|
236
|
+
const completed = await terminals.completeIdentification(await hashToken(id), browser.terminal.principal_id, unixNow())
|
|
237
|
+
const login = completed?.session_id == null ? null : await sessions.getActiveSessionById(completed.session_id)
|
|
238
|
+
if (completed === null || login === null) {
|
|
239
|
+
return authFailurePage(readerFailure(request), { kind: 'terminal_completion_refused' }, 403)
|
|
240
|
+
}
|
|
241
|
+
const location = await handoffLocation(services, {
|
|
242
|
+
app: completed.app,
|
|
243
|
+
returnUrl: completed.return_url,
|
|
244
|
+
state: completed.handoff_state,
|
|
245
|
+
challenge: completed.handoff_challenge,
|
|
246
|
+
}, login.id)
|
|
247
|
+
return new Response(null, { status: 302, headers: redirectHeaders(location) })
|
|
248
|
+
} catch (err) {
|
|
249
|
+
logError(`a terminal identification could not be completed for request '${requestId}'`, err)
|
|
250
|
+
return authFailurePage(readerFailure(request), { kind: 'login_unavailable' }, 503)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* `GET /auth/terminals/release?app=&redirect=` (ADR-0067 §5): end the sessions of this browser's terminal, then return
|
|
256
|
+
* to `redirect`, which must lie in the app's registered return origins. A browser that is no terminal is only sent back.
|
|
257
|
+
*
|
|
258
|
+
* A GET without a same-origin check, unlike every other auth route that changes state: the application's proxy sends
|
|
259
|
+
* the browser here from its own sign-out, a navigation from another host. The most a cross-site request can do is end
|
|
260
|
+
* the short session of whoever stands at the terminal.
|
|
261
|
+
*/
|
|
262
|
+
export async function handleTerminalRelease(request: Request, services: Services, requestId: string): Promise<Response> {
|
|
263
|
+
const url = new URL(request.url)
|
|
264
|
+
const app = url.searchParams.get('app')
|
|
265
|
+
const redirect = url.searchParams.get('redirect')
|
|
266
|
+
const presentation = await presentationFor(services, app, { request })
|
|
267
|
+
if (app === null || app === '' || redirect === null || redirect === '') {
|
|
268
|
+
return authFailurePage(presentation, { kind: 'no_return_address' }, 400)
|
|
269
|
+
}
|
|
270
|
+
const returnUrl = await resolveReturnUrl(services, app, redirect)
|
|
271
|
+
if (returnUrl === null) {
|
|
272
|
+
return authFailurePage(presentation, { kind: 'unregistered_origin', origin: printable(normalizeOrigin(redirect) ?? 'that address') }, 400)
|
|
273
|
+
}
|
|
274
|
+
const browser = await browserTerminal(request, services)
|
|
275
|
+
if (browser !== null) {
|
|
276
|
+
try {
|
|
277
|
+
await services.repositories.terminals.release(browser.terminal.principal_id, { requestId, app: IAM_APP, reason: 'terminal_release' })
|
|
278
|
+
} catch (err) {
|
|
279
|
+
logError(`a terminal release could not be recorded for request '${requestId}'`, err)
|
|
280
|
+
return authFailurePage(presentation, { kind: 'logout_unavailable' }, 503)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return new Response(null, { status: 302, headers: redirectHeaders(returnUrl) })
|
|
284
|
+
}
|