@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,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ListSessionsInput,
|
|
3
|
+
type PrincipalIdInput,
|
|
4
|
+
type RevokedSessionsResponse,
|
|
5
|
+
type SessionDto,
|
|
6
|
+
type SessionIdInput,
|
|
7
|
+
type SessionList,
|
|
8
|
+
} from '@fabrika/iam-contract'
|
|
9
|
+
import { sessionUsable } from '../auth'
|
|
10
|
+
import type { ListedSessionRow } from '../repositories/sessions'
|
|
11
|
+
|
|
12
|
+
import { type AdminContext, adminEvent, fail } from './context'
|
|
13
|
+
|
|
14
|
+
import { cursorOf, normalizeLimit } from './pagination'
|
|
15
|
+
import { requireActableTarget } from './user-admin'
|
|
16
|
+
|
|
17
|
+
/** Stored lifecycle stays compatible; availability also reflects the parent, account and installation switches. */
|
|
18
|
+
function toSessionDto(c: AdminContext, row: ListedSessionRow, now: number, disabled: boolean): SessionDto {
|
|
19
|
+
const unavailableReason = row.revoked_at !== null
|
|
20
|
+
? 'revoked'
|
|
21
|
+
: row.expires_at <= now
|
|
22
|
+
? 'expired'
|
|
23
|
+
: row.parent_session_id !== null && (row.parent_expires_at === null || row.parent_expires_at <= now || row.parent_revoked_at !== null)
|
|
24
|
+
? 'parent_ended'
|
|
25
|
+
: disabled
|
|
26
|
+
? 'account_disabled'
|
|
27
|
+
: !sessionUsable(row, c.services.config)
|
|
28
|
+
? 'method_disabled'
|
|
29
|
+
: null
|
|
30
|
+
return {
|
|
31
|
+
id: row.id,
|
|
32
|
+
principalId: row.principal_id,
|
|
33
|
+
authenticationMethod: row.authentication_method,
|
|
34
|
+
app: row.app,
|
|
35
|
+
parentSessionId: row.parent_session_id,
|
|
36
|
+
status: row.revoked_at !== null ? 'revoked' : row.expires_at <= now ? 'expired' : 'active',
|
|
37
|
+
createdAt: row.created_at,
|
|
38
|
+
expiresAt: row.expires_at,
|
|
39
|
+
revokedAt: row.revoked_at,
|
|
40
|
+
unavailableReason,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Page by sign-in when requested, keeping all of that page's application sessions with their parent. */
|
|
45
|
+
export async function listSessionsUseCase(c: AdminContext, input: ListSessionsInput): Promise<SessionList> {
|
|
46
|
+
const principal = await c.services.repositories.principals.getPrincipalById(input.principalId)
|
|
47
|
+
if (!principal) {
|
|
48
|
+
return fail(404, 'principal not found')
|
|
49
|
+
}
|
|
50
|
+
const limit = normalizeLimit(input.limit)
|
|
51
|
+
const rows = await c.services.repositories.sessions.listSessionsForPrincipal(input.principalId, {
|
|
52
|
+
...(input.before ? { before: input.before } : {}),
|
|
53
|
+
limit,
|
|
54
|
+
signInsOnly: input.signInsOnly,
|
|
55
|
+
})
|
|
56
|
+
const now = Math.floor(Date.now() / 1000)
|
|
57
|
+
const items = rows.map((row) => toSessionDto(c, row, now, principal.disabled_at !== null))
|
|
58
|
+
const children = input.signInsOnly ? await c.services.repositories.sessions.listChildren(rows.map((row) => row.id)) : []
|
|
59
|
+
return {
|
|
60
|
+
items,
|
|
61
|
+
nextCursor: cursorOf(items, limit, (item) => item.id),
|
|
62
|
+
applications: children.map((row) => toSessionDto(c, row, now, principal.disabled_at !== null)),
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Revoke ONE session.
|
|
68
|
+
*
|
|
69
|
+
* Revoking an IAM session ends every app session derived from it in the same write: a child is valid
|
|
70
|
+
* only while its parent is, and the lookup joins to the parent on every use (ADR-0022). So there is
|
|
71
|
+
* no sweep to run and no window in which a child outlives its parent.
|
|
72
|
+
*/
|
|
73
|
+
export async function revokeSessionUseCase(c: AdminContext, input: SessionIdInput): Promise<RevokedSessionsResponse> {
|
|
74
|
+
const session = await c.services.repositories.sessions.getSessionById(input.id)
|
|
75
|
+
if (!session) {
|
|
76
|
+
return fail(404, 'session not found')
|
|
77
|
+
}
|
|
78
|
+
const holder = await c.services.repositories.principals.getPrincipalById(session.principal_id)
|
|
79
|
+
if (!holder) {
|
|
80
|
+
return fail(404, 'session not found')
|
|
81
|
+
}
|
|
82
|
+
await requireActableTarget(c, holder)
|
|
83
|
+
const revoked = await c.services.repositories.sessions.revokeSessionById(
|
|
84
|
+
input.id,
|
|
85
|
+
adminEvent(c, {
|
|
86
|
+
action: 'iam.session.revoke',
|
|
87
|
+
resourceType: 'session',
|
|
88
|
+
resourceId: input.id,
|
|
89
|
+
metadata: {
|
|
90
|
+
principalId: session.principal_id,
|
|
91
|
+
authenticationMethod: session.authentication_method,
|
|
92
|
+
app: session.app,
|
|
93
|
+
parentSessionId: session.parent_session_id,
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
)
|
|
97
|
+
// Already revoked: idempotent, reported as such rather than as a failure, and recorded nowhere.
|
|
98
|
+
return { revoked: revoked ? 1 : 0 }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Revoke every live session a principal holds — the "sign them out everywhere" answer. */
|
|
102
|
+
export async function revokeSessionsForPrincipalUseCase(c: AdminContext, input: PrincipalIdInput): Promise<RevokedSessionsResponse> {
|
|
103
|
+
const principal = await c.services.repositories.principals.getPrincipalById(input.principalId)
|
|
104
|
+
if (!principal) {
|
|
105
|
+
return fail(404, 'principal not found')
|
|
106
|
+
}
|
|
107
|
+
await requireActableTarget(c, principal)
|
|
108
|
+
// The recorded count is the unrevoked sessions this request saw; the row itself is written only when some went.
|
|
109
|
+
const unrevoked = await c.services.repositories.sessions.countUnrevokedSessions(input.principalId)
|
|
110
|
+
const revoked = await c.services.repositories.sessions.revokeSessionsForPrincipal(
|
|
111
|
+
input.principalId,
|
|
112
|
+
adminEvent(c, { action: 'iam.session.revoke_all', resourceType: 'principal', resourceId: input.principalId, metadata: { revoked: unrevoked } }),
|
|
113
|
+
)
|
|
114
|
+
return { revoked }
|
|
115
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { type IssueKeyInput, type PermissionEntry } from '@fabrika/auth-core'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type CursorList,
|
|
5
|
+
type IssuedShareLinkResponse,
|
|
6
|
+
type IssueShareLinkRequest,
|
|
7
|
+
type ListShareLinksInput,
|
|
8
|
+
type OkResponse,
|
|
9
|
+
type ShareLinkListItem,
|
|
10
|
+
} from '@fabrika/iam-contract'
|
|
11
|
+
import { type CredentialGrantRow, type CredentialRow } from '../db'
|
|
12
|
+
import { issueKey } from '../issue'
|
|
13
|
+
import { resolvePermissionsAt } from '../resolve'
|
|
14
|
+
import { type AdminContext, adminEvent, fail } from './context'
|
|
15
|
+
import { knownApps } from './grant-support'
|
|
16
|
+
import { cursorOf, groupBy, normalizeLimit } from './pagination'
|
|
17
|
+
|
|
18
|
+
function toShareLinkListItem(row: CredentialRow, grants: CredentialGrantRow[]): ShareLinkListItem {
|
|
19
|
+
return {
|
|
20
|
+
id: row.id,
|
|
21
|
+
label: row.label,
|
|
22
|
+
app: row.app,
|
|
23
|
+
issuedBy: row.issued_by,
|
|
24
|
+
expiresAt: row.expires_at,
|
|
25
|
+
revokedAt: row.revoked_at,
|
|
26
|
+
createdAt: row.created_at,
|
|
27
|
+
grants: grants.map((g) => ({
|
|
28
|
+
action: g.action,
|
|
29
|
+
scope: g.scope_type === null || g.scope_value === null ? null : { type: g.scope_type, value: g.scope_value },
|
|
30
|
+
})),
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* One page of share links. Two queries per page (the credentials read plus one grants read for the
|
|
36
|
+
* page's ids), revoked links hidden unless asked for, and `app` finally filterable now that an
|
|
37
|
+
* anonymous credential carries one (SEC-2, SEC-22).
|
|
38
|
+
*/
|
|
39
|
+
export async function listShareLinksUseCase(c: AdminContext, input: ListShareLinksInput): Promise<CursorList<ShareLinkListItem>> {
|
|
40
|
+
const limit = normalizeLimit(input.limit)
|
|
41
|
+
const creds = await c.services.repositories.credentials.listAnonymousCredentials({
|
|
42
|
+
...(input.app !== undefined ? { app: input.app } : {}),
|
|
43
|
+
...(input.includeRevoked === true ? { includeRevoked: true } : {}),
|
|
44
|
+
...(input.before ? { before: input.before } : {}),
|
|
45
|
+
limit,
|
|
46
|
+
})
|
|
47
|
+
const grants = groupBy(
|
|
48
|
+
await c.services.repositories.credentials.getCredentialGrantsFor(creds.map((cred) => cred.id)),
|
|
49
|
+
(row) => row.credential_id,
|
|
50
|
+
)
|
|
51
|
+
const items = creds.map((cred) => toShareLinkListItem(cred, grants.get(cred.id) ?? []))
|
|
52
|
+
return { items, nextCursor: cursorOf(items, limit, (item) => item.id) }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Issue an anonymous share link FOR ONE APP.
|
|
57
|
+
*
|
|
58
|
+
* `app` is required and is not IAM's own. A share link's grants are frozen at issue and spent
|
|
59
|
+
* wherever the holder presents them, so the app has to be stated: it is what `resolveCredential`
|
|
60
|
+
* checks at every mint, and without it an admin of one app could mint authority good at every other
|
|
61
|
+
* app behind the proxy (SEC-2). Delegation still runs against the admin's permissions — resolved for
|
|
62
|
+
* that app, which is the same question the credential will later ask.
|
|
63
|
+
*/
|
|
64
|
+
export async function createShareLinkUseCase(c: AdminContext, input: IssueShareLinkRequest): Promise<IssuedShareLinkResponse> {
|
|
65
|
+
if (input.grants.length === 0 || input.grants.some((grant) => !grant.action)) return fail(400, 'grants required (each: { action, scope? })')
|
|
66
|
+
const grants = input.grants.map(({ action, scope }) => ({ action, scope: scope == null ? null : { type: scope.type, value: scope.value } }))
|
|
67
|
+
const { app, label, expiresAt } = input
|
|
68
|
+
if (!(await knownApps(c)).includes(app)) return fail(404, 'unknown app')
|
|
69
|
+
|
|
70
|
+
// The issuer is the resolved admin (`c.admin`) passed directly to `issueKey`, so `credential` is
|
|
71
|
+
// unused here (the RPC entrypoint resolves it; this admin path already has the caller).
|
|
72
|
+
const issueInput: IssueKeyInput = {
|
|
73
|
+
app,
|
|
74
|
+
credential: null,
|
|
75
|
+
requestId: c.requestId,
|
|
76
|
+
permissions: grants,
|
|
77
|
+
...(label !== undefined ? { label } : {}),
|
|
78
|
+
expiresAt,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// An admin holding the cross-app `admin` role keeps `*` at every app; an app-scoped admin only
|
|
82
|
+
// delegates what they hold THERE, which is the whole point of naming the app.
|
|
83
|
+
const issuer = { id: c.admin.id, permissions: await effectiveIssuerPermissions(c, app) }
|
|
84
|
+
const record = ({ credentialId }: { credentialId: string }) =>
|
|
85
|
+
adminEvent(c, {
|
|
86
|
+
action: 'iam.credential.create',
|
|
87
|
+
resourceType: 'credential',
|
|
88
|
+
resourceId: credentialId,
|
|
89
|
+
metadata: { app, label: label ?? null, grants },
|
|
90
|
+
})
|
|
91
|
+
const { result, problem } = await issueKey(c.services, issueInput, issuer, app, record)
|
|
92
|
+
if (!result.ok) {
|
|
93
|
+
// Admin already gated: a `problem` is a malformed request, anything else the delegation rule.
|
|
94
|
+
return problem === undefined ? fail(403, `not allowed: ${result.reason}`) : fail(400, problem)
|
|
95
|
+
}
|
|
96
|
+
const issued: IssuedShareLinkResponse = { id: result.id, token: result.token }
|
|
97
|
+
return issued
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The admin's permissions AT THE TARGET APP, for the delegation check. `c.admin.permissions` were
|
|
102
|
+
* resolved against IAM's own app; delegating into another app has to be measured there.
|
|
103
|
+
*/
|
|
104
|
+
async function effectiveIssuerPermissions(c: AdminContext, app: string): Promise<PermissionEntry[]> {
|
|
105
|
+
const principal = await c.services.repositories.principals.getPrincipalById(c.admin.id)
|
|
106
|
+
if (!principal) {
|
|
107
|
+
// A synthetic caller (the provisioning key) has no row; it already
|
|
108
|
+
// carries its own resolved permission set, which is global by construction.
|
|
109
|
+
return c.admin.permissions
|
|
110
|
+
}
|
|
111
|
+
return resolvePermissionsAt(c.services, principal, app)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function revokeShareLinkUseCase(c: AdminContext, input: { id: string }): Promise<OkResponse> {
|
|
115
|
+
const id = input.id
|
|
116
|
+
// Only anonymous credentials are share links; a principal-bound key is managed on the api-keys
|
|
117
|
+
// page, so it reads as not-found here.
|
|
118
|
+
const cred = await c.services.repositories.credentials.getCredentialById(id)
|
|
119
|
+
if (!cred || cred.principal_id !== null) {
|
|
120
|
+
return fail(404, 'share link not found')
|
|
121
|
+
}
|
|
122
|
+
await c.services.repositories.credentials.revokeCredential(
|
|
123
|
+
id,
|
|
124
|
+
adminEvent(c, { action: 'iam.credential.revoke', resourceType: 'credential', resourceId: id, metadata: { label: cred.label } }),
|
|
125
|
+
)
|
|
126
|
+
return { ok: true }
|
|
127
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { isActionAllowed } from '@fabrika/auth-core'
|
|
2
|
+
import {
|
|
3
|
+
type CursorList,
|
|
4
|
+
type ListTerminalsInput,
|
|
5
|
+
type OkResponse,
|
|
6
|
+
type SetTerminalProfileRequest,
|
|
7
|
+
type TerminalAsserterDto,
|
|
8
|
+
type TerminalDto,
|
|
9
|
+
type TerminalIdInput,
|
|
10
|
+
} from '@fabrika/iam-contract'
|
|
11
|
+
import { MAX_TERMINAL_SESSION_TTL_SECONDS, type TerminalAsserter, type TerminalRow } from '../db'
|
|
12
|
+
import { reachesCrossAppAuthority } from '../resolve'
|
|
13
|
+
import { terminalLabel } from '../terminals'
|
|
14
|
+
import { type AdminContext, adminEvent, fail } from './context'
|
|
15
|
+
import { knownApps } from './grant-support'
|
|
16
|
+
import { cursorOf, normalizeLimit } from './pagination'
|
|
17
|
+
|
|
18
|
+
function toTerminalDto(row: TerminalRow, asserters: readonly TerminalAsserter[]): TerminalDto {
|
|
19
|
+
return {
|
|
20
|
+
terminalId: row.principal_id,
|
|
21
|
+
label: row.label,
|
|
22
|
+
enrolledBy: row.enrolled_by,
|
|
23
|
+
sessionTtl: row.session_ttl,
|
|
24
|
+
asserters: asserters.map((asserter) => ({ app: asserter.app, ceiling: [...asserter.ceiling] })),
|
|
25
|
+
status: row.revoked_at === null ? 'active' : 'revoked',
|
|
26
|
+
createdAt: row.created_at,
|
|
27
|
+
lastSeenAt: row.last_seen_at,
|
|
28
|
+
revokedAt: row.revoked_at,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One page of terminals, newest first, revoked ones included. Two queries: the page and its asserters. */
|
|
33
|
+
export async function listTerminalsUseCase(c: AdminContext, input: ListTerminalsInput): Promise<CursorList<TerminalDto>> {
|
|
34
|
+
const limit = normalizeLimit(input.limit)
|
|
35
|
+
const { terminals } = c.services.repositories
|
|
36
|
+
const rows = await terminals.list({ ...(input.before ? { before: input.before } : {}), limit })
|
|
37
|
+
const asserters = await terminals.listAssertersFor(rows.map((row) => row.principal_id))
|
|
38
|
+
const items = rows.map((row) => toTerminalDto(row, asserters.get(row.principal_id) ?? []))
|
|
39
|
+
return { items, nextCursor: cursorOf(items, limit, (item) => item.terminalId) }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Replace an enrolled terminal's profile. An asserter is a registered application that is not a platform audience: a
|
|
44
|
+
* platform audience's token carries cross-app authority, and IAM's own re-resolves the subject's permissions, either of
|
|
45
|
+
* which would step past the ceiling. Each ceiling pattern must name actions of its application, as a typo check; the
|
|
46
|
+
* ceiling only narrows what the person brings, so a pattern there grants nothing.
|
|
47
|
+
*/
|
|
48
|
+
export async function setTerminalProfileUseCase(c: AdminContext, input: SetTerminalProfileRequest): Promise<TerminalDto> {
|
|
49
|
+
const label = terminalLabel(input.label)
|
|
50
|
+
if (label === null) {
|
|
51
|
+
return fail(400, 'label must be 1–100 characters without control characters')
|
|
52
|
+
}
|
|
53
|
+
if (!Number.isInteger(input.sessionTtl) || input.sessionTtl < 1 || input.sessionTtl > MAX_TERMINAL_SESSION_TTL_SECONDS) {
|
|
54
|
+
return fail(400, `sessionTtl must be a whole number of seconds from 1 to ${MAX_TERMINAL_SESSION_TTL_SECONDS}`)
|
|
55
|
+
}
|
|
56
|
+
const asserters = await checkedAsserters(c, input.asserters)
|
|
57
|
+
const { terminals } = c.services.repositories
|
|
58
|
+
const changed = await terminals.setProfile(
|
|
59
|
+
input.terminalId,
|
|
60
|
+
{ label, sessionTtl: input.sessionTtl, asserters },
|
|
61
|
+
adminEvent(c, {
|
|
62
|
+
action: 'iam.terminal.profile.update',
|
|
63
|
+
resourceType: 'terminal',
|
|
64
|
+
resourceId: input.terminalId,
|
|
65
|
+
metadata: { label, sessionTtl: input.sessionTtl, asserters },
|
|
66
|
+
}),
|
|
67
|
+
)
|
|
68
|
+
const row = await terminals.getById(input.terminalId)
|
|
69
|
+
if (row === null) {
|
|
70
|
+
return fail(404, 'terminal not found')
|
|
71
|
+
}
|
|
72
|
+
if (!changed) {
|
|
73
|
+
return fail(409, 'terminal is revoked')
|
|
74
|
+
}
|
|
75
|
+
return toTerminalDto(row, await terminals.listAsserters(input.terminalId))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function checkedAsserters(c: AdminContext, requested: readonly TerminalAsserterDto[]): Promise<TerminalAsserter[]> {
|
|
79
|
+
const registered = await knownApps(c)
|
|
80
|
+
const seen = new Set<string>()
|
|
81
|
+
const asserters: TerminalAsserter[] = []
|
|
82
|
+
for (const { app, ceiling } of requested) {
|
|
83
|
+
if (seen.has(app)) {
|
|
84
|
+
return fail(400, `asserter listed twice: ${app}`)
|
|
85
|
+
}
|
|
86
|
+
seen.add(app)
|
|
87
|
+
if (!registered.includes(app)) {
|
|
88
|
+
return fail(400, `unknown app: ${app}`)
|
|
89
|
+
}
|
|
90
|
+
if (reachesCrossAppAuthority(c.services.config, app)) {
|
|
91
|
+
return fail(400, `a platform audience cannot assert a person on a terminal: ${app}`)
|
|
92
|
+
}
|
|
93
|
+
const catalog = await c.services.repositories.appSchema.listActionCatalog(app)
|
|
94
|
+
const unknown = ceiling.find((pattern) => !isActionAllowed(pattern, catalog))
|
|
95
|
+
if (unknown !== undefined) {
|
|
96
|
+
return fail(400, `unknown action pattern for ${app}: ${unknown}`)
|
|
97
|
+
}
|
|
98
|
+
asserters.push({ app, ceiling: [...new Set(ceiling)] })
|
|
99
|
+
}
|
|
100
|
+
return asserters
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Revoke a terminal and every session issued on it. Repeating it answers the same; an unknown terminal is a 404. */
|
|
104
|
+
export async function revokeTerminalUseCase(c: AdminContext, input: TerminalIdInput): Promise<OkResponse> {
|
|
105
|
+
const { terminals } = c.services.repositories
|
|
106
|
+
const row = await terminals.getById(input.terminalId)
|
|
107
|
+
if (row === null) {
|
|
108
|
+
return fail(404, 'terminal not found')
|
|
109
|
+
}
|
|
110
|
+
await terminals.revoke(
|
|
111
|
+
input.terminalId,
|
|
112
|
+
adminEvent(c, { action: 'iam.terminal.revoke', resourceType: 'terminal', resourceId: input.terminalId, metadata: { label: row.label } }),
|
|
113
|
+
)
|
|
114
|
+
return { ok: true }
|
|
115
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// User administration (ADR-0058): who may manage the people of applications without administering IAM, and where that
|
|
2
|
+
// authority stops. Every rule here applies only to a caller without `iam.admin`.
|
|
3
|
+
|
|
4
|
+
import { IAM_APP_ID, type IamAdministrationScope, iamAdministrationScope } from '@fabrika/iam-contract'
|
|
5
|
+
import { type PrincipalRow } from '../db'
|
|
6
|
+
import { normalizeEmailIdentity } from '../email-identity'
|
|
7
|
+
import type { Services } from '../services'
|
|
8
|
+
import { type AdminContext, fail } from './context'
|
|
9
|
+
import type { ResolvedAdmin } from './router'
|
|
10
|
+
|
|
11
|
+
/** What the caller administers: the installation, or the people of some applications (`null` = every application). */
|
|
12
|
+
export type Administration = IamAdministrationScope
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The caller's administration, or null when it holds none. A user administrator is a person signed in to the console,
|
|
16
|
+
* as for `appPeople`: a `px_` key administers only with `iam.admin`.
|
|
17
|
+
*/
|
|
18
|
+
export function administrationOf(admin: Pick<ResolvedAdmin, 'permissions' | 'type' | 'audience' | 'session'>): Administration | null {
|
|
19
|
+
const administration = iamAdministrationScope(admin.permissions)
|
|
20
|
+
if (administration?.kind !== 'users') return administration
|
|
21
|
+
return admin.type === 'user' && admin.audience !== IAM_APP_ID && admin.session ? administration : null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** True when `administration` may write a grant at `app`: never cross-app, never an installation app, only in scope. */
|
|
25
|
+
export function managesApp(services: Pick<Services, 'config'>, administration: Administration, app: string | null): boolean {
|
|
26
|
+
if (administration.kind === 'installation') return true
|
|
27
|
+
if (app === null || services.config.platformAudiences.has(app)) return false
|
|
28
|
+
return administration.apps === null || administration.apps.includes(app)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Refuse a grant write at `app` that the caller's administration does not reach. */
|
|
32
|
+
export function requireManagedApp(c: AdminContext, app: string | null): void {
|
|
33
|
+
if (managesApp(c.services, c.administration, app)) return
|
|
34
|
+
if (app === null) return fail(403, 'a user administrator grants at one application, never across applications')
|
|
35
|
+
if (c.services.config.platformAudiences.has(app)) {
|
|
36
|
+
return fail(403, `'${app}' is one of the installation's own apps; only an installation administrator grants there`)
|
|
37
|
+
}
|
|
38
|
+
return fail(403, `'${app}' is outside the applications you administer`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Refuse a user administrator a grant write on a service principal: an API key's grants are edited like the rest of `apiKeys.*`. */
|
|
42
|
+
export function requireGrantablePrincipal(c: AdminContext, principal: Pick<PrincipalRow, 'type'>): void {
|
|
43
|
+
if (c.administration.kind === 'installation' || principal.type !== 'service') return
|
|
44
|
+
return fail(403, "an API key's grants are changed only by an installation administrator")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Refuse to act on `principal` unless the caller could have made every grant it holds. That keeps an installation
|
|
49
|
+
* administrator, a deployer, a console admin, a bootstrap mailbox and every other user administrator out of reach.
|
|
50
|
+
*/
|
|
51
|
+
export async function requireActableTarget(c: AdminContext, principal: PrincipalRow): Promise<void> {
|
|
52
|
+
if (!(await canManagePrincipal(c, principal))) {
|
|
53
|
+
return fail(403, 'this person holds access outside the applications you administer; only an installation administrator acts on them')
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Refuse a user administrator an invitation for a mailbox in `IAM_BOOTSTRAP_ADMINS`. The row it would create decides how
|
|
59
|
+
* that installation administrator signs in, and `requireActableTarget` keeps the row out of the caller's reach afterwards.
|
|
60
|
+
*/
|
|
61
|
+
export function requireInvitableMailbox(c: AdminContext, mailbox: string): void {
|
|
62
|
+
if (c.administration.kind === 'installation' || !c.services.config.bootstrapAdmins.has(mailbox)) return
|
|
63
|
+
return fail(403, "this mailbox is one of the installation's bootstrap administrators; only an installation administrator invites it")
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The same rule serves UI capabilities and the write guard. */
|
|
67
|
+
export function canManagePrincipal(c: AdminContext, principal: PrincipalRow): Promise<boolean> {
|
|
68
|
+
return c.administration.kind === 'installation' ? Promise.resolve(true) : isActable(c.services, c.administration, principal)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function isActable(services: Services, administration: Administration, principal: PrincipalRow): Promise<boolean> {
|
|
72
|
+
if (principal.type !== 'user') return false
|
|
73
|
+
if (principal.email !== null && services.config.bootstrapAdmins.has(normalizeEmailIdentity(principal.email))) return false
|
|
74
|
+
const grants = await services.repositories.grants.getActiveGrants(principal.id)
|
|
75
|
+
return grants.every((grant) => managesApp(services, administration, grant.app))
|
|
76
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { API_KEY_PREFIX, isOwnWorkloadName } from '@fabrika/auth-core'
|
|
2
|
+
import type { IssueWorkloadKeyRequest, IssueWorkloadKeyResponse, RevokeWorkloadKeysRequest, RevokeWorkloadKeysResponse } from '@fabrika/iam-contract'
|
|
3
|
+
import { uuidv7 } from '@fabrika/platform'
|
|
4
|
+
import { principalStatus } from '../db'
|
|
5
|
+
import { keyExpiryProblem } from '../issue'
|
|
6
|
+
import { unixNow } from '../repositories/sql'
|
|
7
|
+
import { generateToken, hashToken } from '../secret'
|
|
8
|
+
import { type AdminContext, adminEvent, fail } from './context'
|
|
9
|
+
|
|
10
|
+
/** An `app` workload is the consuming application of an export, named by its id. */
|
|
11
|
+
const APP_ID = /^[a-z0-9][a-z0-9-]{0,63}$/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Issue a fresh key for one workload (ADR-0022 "Principal kinds"): a job, a webhook, a public procedure, or the connector
|
|
15
|
+
* of an application that consumes this application's exports. The principal is created on first use; what the key
|
|
16
|
+
* may do is the app's reconciled declaration of the workload, never part of this request. The key the caller recorded
|
|
17
|
+
* as active (`keep`) stays valid, so the version still running keeps working until the caller has recorded the new one;
|
|
18
|
+
* every other key of the workload is an orphan of an attempt whose record failed, and is revoked.
|
|
19
|
+
*/
|
|
20
|
+
const workloadNameRule = {
|
|
21
|
+
job: 'a workload name is a contract name (a letter, then letters, digits or _, at most 64)',
|
|
22
|
+
webhook: 'a workload name is a contract name (a letter, then letters, digits or _, at most 64)',
|
|
23
|
+
public: 'a public procedure is named by its endpoint id (<mount>.<group>.<procedure>, at most 128)',
|
|
24
|
+
app: 'an app workload is named by the consuming application id',
|
|
25
|
+
} as const
|
|
26
|
+
|
|
27
|
+
export async function issueWorkloadKeyUseCase(c: AdminContext, input: IssueWorkloadKeyRequest): Promise<IssueWorkloadKeyResponse> {
|
|
28
|
+
if (input.kind === 'app' ? !APP_ID.test(input.name) : !isOwnWorkloadName(input.kind, input.name)) {
|
|
29
|
+
return fail(400, workloadNameRule[input.kind])
|
|
30
|
+
}
|
|
31
|
+
const expiryProblem = keyExpiryProblem(input.expiresAt, unixNow())
|
|
32
|
+
if (expiryProblem !== null) {
|
|
33
|
+
return fail(400, expiryProblem)
|
|
34
|
+
}
|
|
35
|
+
const { workloads, credentials } = c.services.repositories
|
|
36
|
+
const existing = await workloads.findPrincipal(input.app, input.kind, input.name)
|
|
37
|
+
if (existing !== null && principalStatus(existing) === 'disabled') {
|
|
38
|
+
return fail(409, 'the workload principal is disabled')
|
|
39
|
+
}
|
|
40
|
+
const previous = existing === null ? [] : (await credentials.listCredentialsForPrincipal(existing.id)).filter((row) => row.revoked_at === null)
|
|
41
|
+
const orphans = previous.filter((row) => row.id !== input.keep)
|
|
42
|
+
const key = `${API_KEY_PREFIX}${generateToken()}`
|
|
43
|
+
const credentialId = uuidv7()
|
|
44
|
+
// The principal on first use, the orphans' revocation, the key and the record are one transaction. The recorded count
|
|
45
|
+
// is the orphans this request saw.
|
|
46
|
+
const principalId = await workloads.issueKey(
|
|
47
|
+
{
|
|
48
|
+
app: input.app,
|
|
49
|
+
kind: input.kind,
|
|
50
|
+
name: input.name,
|
|
51
|
+
credentialId,
|
|
52
|
+
tokenHash: await hashToken(key),
|
|
53
|
+
issuedBy: c.admin.id,
|
|
54
|
+
expiresAt: input.expiresAt,
|
|
55
|
+
keep: input.keep,
|
|
56
|
+
},
|
|
57
|
+
adminEvent(c, {
|
|
58
|
+
action: 'iam.workload.key.issue',
|
|
59
|
+
resourceType: 'principal',
|
|
60
|
+
metadata: { app: input.app, kind: input.kind, name: input.name, expiresAt: input.expiresAt, kept: input.keep, revoked: orphans.length },
|
|
61
|
+
}),
|
|
62
|
+
)
|
|
63
|
+
return { key, credentialId, principalId, expiresAt: input.expiresAt }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Revoke every live key of one workload, so a consumer that stopped importing keeps no key at the owner. */
|
|
67
|
+
export async function revokeWorkloadKeysUseCase(c: AdminContext, input: RevokeWorkloadKeysRequest): Promise<RevokeWorkloadKeysResponse> {
|
|
68
|
+
const principalId = await c.services.repositories.workloads.findPrincipalId(input.app, input.kind, input.name)
|
|
69
|
+
if (principalId === null) {
|
|
70
|
+
return { revoked: 0 }
|
|
71
|
+
}
|
|
72
|
+
const live = (await c.services.repositories.credentials.listCredentialsForPrincipal(principalId)).filter((row) => row.revoked_at === null)
|
|
73
|
+
const { credentials, audit } = c.services.repositories
|
|
74
|
+
// Recorded only when this call revoked a key: revoking a workload with none left is not a change.
|
|
75
|
+
const revoked = await audit.writer.run(
|
|
76
|
+
[credentials.revokeCredentialsForPrincipalChange(principalId)],
|
|
77
|
+
adminEvent(c, {
|
|
78
|
+
action: 'iam.workload.key.revoke',
|
|
79
|
+
resourceType: 'principal',
|
|
80
|
+
resourceId: principalId,
|
|
81
|
+
metadata: { app: input.app, kind: input.kind, name: input.name, revoked: live.length },
|
|
82
|
+
}),
|
|
83
|
+
)
|
|
84
|
+
return { revoked: revoked.length }
|
|
85
|
+
}
|