@fabrika/iam 0.0.0-stage → 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 +70 -5
- 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
package/src/db.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { SqlDatabase } from '@fabrika/platform'
|
|
2
|
+
import { PasswordRepository } from './password-db'
|
|
3
|
+
import { AppSchemaRepository } from './repositories/app-schema'
|
|
4
|
+
import { AuditRepository, AuditWriter, type SqlDialect } from './repositories/audit'
|
|
5
|
+
import { InstallationBrandRepository } from './repositories/brand'
|
|
6
|
+
import { CredentialRepository } from './repositories/credentials'
|
|
7
|
+
import { DeviceAuthorizationRepository } from './repositories/device'
|
|
8
|
+
import { GrantRepository } from './repositories/grants'
|
|
9
|
+
import { HandoffRepository } from './repositories/handoff'
|
|
10
|
+
import { MembershipRepository } from './repositories/membership'
|
|
11
|
+
import { MembershipTransportKeyRepository } from './repositories/membership-transport'
|
|
12
|
+
import { OAuthRepository } from './repositories/oauth'
|
|
13
|
+
import { PasswordPolicyRepository } from './repositories/password-policy'
|
|
14
|
+
import { PrincipalRepository } from './repositories/principals'
|
|
15
|
+
import { SessionRepository } from './repositories/sessions'
|
|
16
|
+
import { TerminalRepository } from './repositories/terminals'
|
|
17
|
+
import { WorkloadRepository } from './repositories/workloads'
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
type AppActionRow,
|
|
21
|
+
type AppMembersRow,
|
|
22
|
+
AppSchemaRepository,
|
|
23
|
+
type AppScopeRow,
|
|
24
|
+
type DelegatedEndpointRow,
|
|
25
|
+
type LinkMountRow,
|
|
26
|
+
type RoleRow,
|
|
27
|
+
} from './repositories/app-schema'
|
|
28
|
+
export {
|
|
29
|
+
type AuditedChange,
|
|
30
|
+
type AuditEventInput,
|
|
31
|
+
type AuditEventRow,
|
|
32
|
+
auditEventStatement,
|
|
33
|
+
auditLabelOf,
|
|
34
|
+
AuditRepository,
|
|
35
|
+
AuditWriter,
|
|
36
|
+
type AuthLogInput,
|
|
37
|
+
type AuthLogKind,
|
|
38
|
+
type AuthLogRow,
|
|
39
|
+
authLogStatement,
|
|
40
|
+
type SqlChange,
|
|
41
|
+
type SqlDialect,
|
|
42
|
+
} from './repositories/audit'
|
|
43
|
+
export { type InstallationBrandImages, InstallationBrandRepository, type InstallationBrandRow } from './repositories/brand'
|
|
44
|
+
export { type CredentialGrantRow, CredentialRepository, type CredentialRow } from './repositories/credentials'
|
|
45
|
+
export { DeviceAuthorizationRepository, type DeviceAuthorizationRow, type DeviceReceiptRow } from './repositories/device'
|
|
46
|
+
export { type DelegatedAuthority, ExternalGrantError, GrantRepository, type GrantRow } from './repositories/grants'
|
|
47
|
+
export { type AuthCodeRow, HandoffRepository } from './repositories/handoff'
|
|
48
|
+
export {
|
|
49
|
+
type DelegationGrantRow,
|
|
50
|
+
type InvitationAcceptance,
|
|
51
|
+
type InvitationRow,
|
|
52
|
+
type InviterCheck,
|
|
53
|
+
type MemberGrantRow,
|
|
54
|
+
MembershipRepository,
|
|
55
|
+
type MembershipRepositoryOptions,
|
|
56
|
+
type NewPrincipalAcceptance,
|
|
57
|
+
type NewPrincipalEvents,
|
|
58
|
+
} from './repositories/membership'
|
|
59
|
+
export { MembershipTransportKeyRepository, type MembershipTransportKeyRow } from './repositories/membership-transport'
|
|
60
|
+
export { type OAuthCodeRow, type OAuthGrantRow, type OAuthRefreshTokenRow, OAuthRepository } from './repositories/oauth'
|
|
61
|
+
export { PasswordPolicyRepository, type PasswordPolicyRow } from './repositories/password-policy'
|
|
62
|
+
export { PrincipalRepository, type PrincipalRow, type PrincipalStatus, principalStatus } from './repositories/principals'
|
|
63
|
+
export { type AuthenticationMethod, SessionRepository, type SessionRow, type SessionTerminal, sessionTerminal } from './repositories/sessions'
|
|
64
|
+
export {
|
|
65
|
+
DEFAULT_TERMINAL_SESSION_TTL_SECONDS,
|
|
66
|
+
MAX_TERMINAL_SESSION_TTL_SECONDS,
|
|
67
|
+
type TerminalAsserter,
|
|
68
|
+
type TerminalIdentificationRow,
|
|
69
|
+
type TerminalProfile,
|
|
70
|
+
TerminalRepository,
|
|
71
|
+
type TerminalRow,
|
|
72
|
+
} from './repositories/terminals'
|
|
73
|
+
export { type AppWorkloadRow, type WorkloadPrincipalRow, WorkloadRepository } from './repositories/workloads'
|
|
74
|
+
|
|
75
|
+
/** IAM persistence capabilities selected together by a runtime composition root. */
|
|
76
|
+
export interface IamRepositories {
|
|
77
|
+
device: DeviceAuthorizationRepository
|
|
78
|
+
principals: PrincipalRepository
|
|
79
|
+
grants: GrantRepository
|
|
80
|
+
appSchema: AppSchemaRepository
|
|
81
|
+
credentials: CredentialRepository
|
|
82
|
+
sessions: SessionRepository
|
|
83
|
+
passwords: PasswordRepository
|
|
84
|
+
handoff: HandoffRepository
|
|
85
|
+
membership: MembershipRepository
|
|
86
|
+
oauth: OAuthRepository
|
|
87
|
+
audit: AuditRepository
|
|
88
|
+
workloads: WorkloadRepository
|
|
89
|
+
terminals: TerminalRepository
|
|
90
|
+
membershipTransport: MembershipTransportKeyRepository
|
|
91
|
+
brand: InstallationBrandRepository
|
|
92
|
+
passwordPolicy: PasswordPolicyRepository
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The portable repository bundle. A composition root may replace one complete capability. `dialect` picks how a
|
|
97
|
+
* guarded change writes its record (`AuditWriter`); everything else is the same SQL on both engines.
|
|
98
|
+
*/
|
|
99
|
+
export function createIamRepositories(
|
|
100
|
+
db: SqlDatabase,
|
|
101
|
+
replacements: Partial<IamRepositories> = {},
|
|
102
|
+
dialect: SqlDialect = 'sqlite',
|
|
103
|
+
): IamRepositories {
|
|
104
|
+
const writer = new AuditWriter(db, dialect)
|
|
105
|
+
return {
|
|
106
|
+
device: replacements.device ?? new DeviceAuthorizationRepository(db),
|
|
107
|
+
principals: replacements.principals ?? new PrincipalRepository(db, writer),
|
|
108
|
+
grants: replacements.grants ?? new GrantRepository(db, writer),
|
|
109
|
+
appSchema: replacements.appSchema ?? new AppSchemaRepository(db, writer),
|
|
110
|
+
credentials: replacements.credentials ?? new CredentialRepository(db, writer),
|
|
111
|
+
sessions: replacements.sessions ?? new SessionRepository(db, writer),
|
|
112
|
+
passwords: replacements.passwords ?? new PasswordRepository(db, writer),
|
|
113
|
+
handoff: replacements.handoff ?? new HandoffRepository(db),
|
|
114
|
+
membership: replacements.membership ?? new MembershipRepository(db, { dialect }),
|
|
115
|
+
oauth: replacements.oauth ?? new OAuthRepository(db, writer),
|
|
116
|
+
audit: replacements.audit ?? new AuditRepository(db, writer),
|
|
117
|
+
workloads: replacements.workloads ?? new WorkloadRepository(db),
|
|
118
|
+
terminals: replacements.terminals ?? new TerminalRepository(db, writer),
|
|
119
|
+
membershipTransport: replacements.membershipTransport ?? new MembershipTransportKeyRepository(db),
|
|
120
|
+
brand: replacements.brand ?? new InstallationBrandRepository(db, writer),
|
|
121
|
+
passwordPolicy: replacements.passwordPolicy ?? new PasswordPolicyRepository(db, writer),
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The bundle on Postgres: the same statements, recorded through one statement each, and the acceptance's row lock. */
|
|
126
|
+
export function createPostgresIamRepositories(db: SqlDatabase): IamRepositories {
|
|
127
|
+
return createIamRepositories(db, {}, 'postgres')
|
|
128
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S8 of the authorization model, shared by issue and mint: a credential one principal delegates to another (a share
|
|
3
|
+
* link, an anonymous key, a service or device key) carries no more than its issuer still holds, and no longer.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { type PermissionEntry, permits, type Scope } from '@fabrika/auth-core'
|
|
7
|
+
import { IAM_ACTIONS, IAM_APP_ID } from '@fabrika/iam-contract'
|
|
8
|
+
import { PROVISIONING_ADMIN_ID } from './auth'
|
|
9
|
+
import { principalStatus } from './db'
|
|
10
|
+
import { authorityDeadline, resolveAuthorityAt } from './resolve'
|
|
11
|
+
import type { Services } from './services'
|
|
12
|
+
|
|
13
|
+
/** An action the issuer must still hold on at least one of `on` (`null` is application-wide). */
|
|
14
|
+
export interface DelegatedAction {
|
|
15
|
+
action: string
|
|
16
|
+
on: readonly (Scope | null)[]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* S8 at every mint of an anonymous credential: its issuer exists, is enabled and still holds each delegated action, and
|
|
21
|
+
* the token ends when the first of them loses its support. A job issuer holds its current declaration, app-wide, and
|
|
22
|
+
* only for the mount it is declared to issue; the provisioning administrator is the installation's named exception. A credential without an issuer, or whose issuer lost any of it, mints
|
|
23
|
+
* nothing (`issuer_unauthorized`, reported as `invalid_key`).
|
|
24
|
+
*/
|
|
25
|
+
export async function issuerAuthority(
|
|
26
|
+
services: Services,
|
|
27
|
+
issuerId: string | null,
|
|
28
|
+
app: string,
|
|
29
|
+
mount: string | null,
|
|
30
|
+
required: readonly DelegatedAction[],
|
|
31
|
+
): Promise<{ ok: true; expiresAt: number | null } | { ok: false; reason: 'issuer_unauthorized' }> {
|
|
32
|
+
const refused: { ok: false; reason: 'issuer_unauthorized' } = { ok: false, reason: 'issuer_unauthorized' }
|
|
33
|
+
const issuer = issuerId === null ? null : await services.repositories.principals.getPrincipalById(issuerId)
|
|
34
|
+
if (issuer === null || principalStatus(issuer) === 'disabled') {
|
|
35
|
+
return refused
|
|
36
|
+
}
|
|
37
|
+
if (issuer.id === PROVISIONING_ADMIN_ID) {
|
|
38
|
+
// The named exception: the seeded installation administrator holds every action, and its issuance was audited.
|
|
39
|
+
return { ok: true, expiresAt: null }
|
|
40
|
+
}
|
|
41
|
+
const workload = await services.repositories.workloads.getByPrincipal(issuer.id)
|
|
42
|
+
if (workload !== null) {
|
|
43
|
+
const declaration = workload.kind === 'job' && workload.app === app
|
|
44
|
+
? await services.repositories.workloads.declaration(app, 'job', workload.name)
|
|
45
|
+
: null
|
|
46
|
+
if (declaration === null || mount === null || declaration.issues !== mount) {
|
|
47
|
+
return refused
|
|
48
|
+
}
|
|
49
|
+
return required.every(({ action }) => declaration.actions.includes(action)) ? { ok: true, expiresAt: null } : refused
|
|
50
|
+
}
|
|
51
|
+
const authority = await resolveAuthorityAt(services, issuer, app)
|
|
52
|
+
const now = Math.floor(Date.now() / 1000)
|
|
53
|
+
let expiresAt: number | null = null
|
|
54
|
+
for (const { action, on } of required) {
|
|
55
|
+
// Per action the latest support among the chain links it is held on, then the earliest across actions.
|
|
56
|
+
let latest: number | null | undefined
|
|
57
|
+
for (const scope of on) {
|
|
58
|
+
if (!permits(authority.permissions, action, scope ?? undefined)) continue
|
|
59
|
+
const until = authorityDeadline(authority, [{ action, scope, source: 'grant' }], now)
|
|
60
|
+
latest = latest === undefined ? until : latest === null || until === null ? null : Math.max(latest, until)
|
|
61
|
+
}
|
|
62
|
+
if (latest === undefined) {
|
|
63
|
+
return refused
|
|
64
|
+
}
|
|
65
|
+
if (latest !== null && (expiresAt === null || latest < expiresAt)) {
|
|
66
|
+
expiresAt = latest
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { ok: true, expiresAt }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A principal-bound key whose issuer is another principal (a service or device key): what the issuer still covers of
|
|
74
|
+
* `permissions` at `app`, and until when. The named exceptions keep the whole set: the provisioning administrator, and
|
|
75
|
+
* an issuer that still holds `iam.admin` (the installation administrator, until its own grant of it ends). A missing
|
|
76
|
+
* or disabled issuer refuses the key.
|
|
77
|
+
*/
|
|
78
|
+
export async function issuerCoverage(
|
|
79
|
+
services: Services,
|
|
80
|
+
issuerId: string,
|
|
81
|
+
app: string,
|
|
82
|
+
permissions: PermissionEntry[],
|
|
83
|
+
): Promise<{ ok: true; permissions: PermissionEntry[]; expiresAt: number | null } | { ok: false; reason: 'issuer_unauthorized' }> {
|
|
84
|
+
const refused: { ok: false; reason: 'issuer_unauthorized' } = { ok: false, reason: 'issuer_unauthorized' }
|
|
85
|
+
if (issuerId === PROVISIONING_ADMIN_ID) {
|
|
86
|
+
return { ok: true, permissions, expiresAt: null }
|
|
87
|
+
}
|
|
88
|
+
const issuer = await services.repositories.principals.getPrincipalById(issuerId)
|
|
89
|
+
if (issuer === null || principalStatus(issuer) === 'disabled') {
|
|
90
|
+
return refused
|
|
91
|
+
}
|
|
92
|
+
const now = Math.floor(Date.now() / 1000)
|
|
93
|
+
const installation = await resolveAuthorityAt(services, issuer, IAM_APP_ID)
|
|
94
|
+
if (permits(installation.permissions, IAM_ACTIONS.ADMIN)) {
|
|
95
|
+
return { ok: true, permissions, expiresAt: authorityDeadline(installation, [{ action: IAM_ACTIONS.ADMIN, scope: null, source: 'grant' }], now) }
|
|
96
|
+
}
|
|
97
|
+
const authority = await resolveAuthorityAt(services, issuer, app)
|
|
98
|
+
const covered = permissions.filter((entry) => permits(authority.permissions, entry.action, entry.scope ?? undefined))
|
|
99
|
+
return { ok: true, permissions: covered, expiresAt: authorityDeadline(authority, covered, now) }
|
|
100
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { readBoundedText } from './bounded-body'
|
|
2
|
+
import { cancelDeviceAuthorization, exchangeDeviceAuthorization, pollDeviceAuthorization, startDeviceAuthorization } from './device'
|
|
3
|
+
import { parseJsonOrNull } from './json'
|
|
4
|
+
import type { Services } from './services'
|
|
5
|
+
|
|
6
|
+
export async function handleDeviceAuthorization(
|
|
7
|
+
request: Request,
|
|
8
|
+
services: Services,
|
|
9
|
+
clientAddress: string | null,
|
|
10
|
+
requestId: string,
|
|
11
|
+
): Promise<Response> {
|
|
12
|
+
const headers = { 'cache-control': 'no-store' }
|
|
13
|
+
if (request.method !== 'POST') return Response.json({ error: 'method not allowed' }, { status: 405, headers: { ...headers, allow: 'POST' } })
|
|
14
|
+
const body = await readDeviceJson(request)
|
|
15
|
+
if (body === null) return Response.json({ error: 'invalid JSON body' }, { status: 400, headers })
|
|
16
|
+
try {
|
|
17
|
+
switch (new URL(request.url).pathname) {
|
|
18
|
+
case '/auth/device/start':
|
|
19
|
+
return Response.json(await startDeviceAuthorization(services, body, clientAddress), { headers })
|
|
20
|
+
case '/auth/device/poll':
|
|
21
|
+
return Response.json(await pollDeviceAuthorization(services, body), { headers })
|
|
22
|
+
case '/auth/device/cancel':
|
|
23
|
+
return Response.json(await cancelDeviceAuthorization(services, body), { headers })
|
|
24
|
+
case '/auth/device/exchange':
|
|
25
|
+
return Response.json(await exchangeDeviceAuthorization(services, body, requestId), { headers })
|
|
26
|
+
default:
|
|
27
|
+
return Response.json({ error: 'not found' }, { status: 404, headers })
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
console.error('Device authorization storage unavailable')
|
|
31
|
+
return Response.json({ error: 'unavailable' }, { status: 503, headers })
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function readDeviceJson(request: Request): Promise<unknown> {
|
|
36
|
+
if (request.headers.get('content-type')?.split(';')[0]?.trim().toLowerCase() !== 'application/json' || !request.body) return null
|
|
37
|
+
const body = await readBoundedText(request, 16_384)
|
|
38
|
+
return body.ok ? parseJsonOrNull(body.text) : null
|
|
39
|
+
}
|
package/src/device.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import {
|
|
2
|
+
API_KEY_PREFIX,
|
|
3
|
+
type ApproveDeviceAuthorizationResult,
|
|
4
|
+
type CancelDeviceAuthorizationResult,
|
|
5
|
+
type DenyDeviceAuthorizationResult,
|
|
6
|
+
type DeviceAuthorizationCaller,
|
|
7
|
+
type DeviceAuthorizationConsent,
|
|
8
|
+
type DeviceAuthorizationReceipt,
|
|
9
|
+
type DeviceAuthorizationStatus,
|
|
10
|
+
type ExchangeDeviceAuthorizationResult,
|
|
11
|
+
type InspectDeviceAuthorizationInput,
|
|
12
|
+
type InspectDeviceAuthorizationResult,
|
|
13
|
+
type ListOwnDeviceAuthorizationsResult,
|
|
14
|
+
parseApproveDeviceAuthorizationInput,
|
|
15
|
+
parseDeviceAuthorizationConsent,
|
|
16
|
+
parseDeviceAuthorizationProof,
|
|
17
|
+
parseDeviceAuthorizationReceipt,
|
|
18
|
+
parseDeviceAuthorizationRequest,
|
|
19
|
+
parseExchangeDeviceAuthorizationInput,
|
|
20
|
+
parseInspectDeviceAuthorizationInput,
|
|
21
|
+
parseListOwnDeviceAuthorizationsInput,
|
|
22
|
+
parseStartDeviceAuthorizationInput,
|
|
23
|
+
type PermissionEntry,
|
|
24
|
+
permits,
|
|
25
|
+
type PollDeviceAuthorizationResult,
|
|
26
|
+
type StartDeviceAuthorizationResult,
|
|
27
|
+
} from '@fabrika/auth-core'
|
|
28
|
+
import { uuidv7 } from '@fabrika/platform'
|
|
29
|
+
import { contractPatternsProblem } from './contract-rules'
|
|
30
|
+
import { auditLabelOf } from './db'
|
|
31
|
+
import type { Env } from './env'
|
|
32
|
+
import { pkceChallenge } from './oidc'
|
|
33
|
+
import type { DeviceAuthorizationRow, DeviceReceiptRow } from './repositories/device'
|
|
34
|
+
import { unixNow } from './repositories/sql'
|
|
35
|
+
import { authorityDeadline, resolveAuthorityAt, resolvePermissionsAt } from './resolve'
|
|
36
|
+
import { generateToken, hashToken, timingSafeEqualHex } from './secret'
|
|
37
|
+
import type { Services } from './services'
|
|
38
|
+
import { getSigner, verifyAccessToken } from './signing'
|
|
39
|
+
|
|
40
|
+
async function human(services: Services, env: Env, input: DeviceAuthorizationCaller) {
|
|
41
|
+
if (!services.config.adminAudiences.has(input.app) || input.credential.startsWith(API_KEY_PREFIX)) return null
|
|
42
|
+
const claims = await verifyAccessToken(await getSigner(env), input.credential, { issuer: services.config.issuer, audience: input.app })
|
|
43
|
+
// A token the proxy minted from a `px_` key carries that key's narrowed grants, but approval measures live permissions.
|
|
44
|
+
if (claims?.ptype !== 'user' || claims.session !== true) return null
|
|
45
|
+
return activeHuman(services, claims.sub)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* An active internal user. A device key is a new service principal holding frozen inline grants, so an external principal
|
|
50
|
+
* (ADR-0051), whose authority is only its live external roles, never approves one.
|
|
51
|
+
*/
|
|
52
|
+
async function activeHuman(services: Services, id: string) {
|
|
53
|
+
const principal = await services.repositories.principals.getPrincipalById(id)
|
|
54
|
+
return principal?.type === 'user' && principal.affiliation === 'internal' && principal.disabled_at === null && principal.activated_at !== null
|
|
55
|
+
? principal
|
|
56
|
+
: null
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function requestFromRow(row: DeviceAuthorizationRow) {
|
|
60
|
+
const request = parseDeviceAuthorizationRequest({
|
|
61
|
+
attemptId: row.id,
|
|
62
|
+
app: row.app,
|
|
63
|
+
label: row.label,
|
|
64
|
+
permissions: JSON.parse(row.permissions),
|
|
65
|
+
keyLifetimeSeconds: row.key_lifetime_seconds,
|
|
66
|
+
expiresAt: row.expires_at,
|
|
67
|
+
})
|
|
68
|
+
if (!request) throw new Error('Invalid stored device request')
|
|
69
|
+
return request
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function consentFromRow(row: DeviceAuthorizationRow): DeviceAuthorizationConsent {
|
|
73
|
+
const consent = parseDeviceAuthorizationConsent({
|
|
74
|
+
...requestFromRow(row),
|
|
75
|
+
consentId: row.consent_id,
|
|
76
|
+
scope: { type: row.scope_type, value: row.scope_value },
|
|
77
|
+
keyExpiresAt: row.key_expires_at,
|
|
78
|
+
})
|
|
79
|
+
if (!consent) throw new Error('Invalid stored device consent')
|
|
80
|
+
return consent
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function receiptFromRow(row: DeviceReceiptRow): DeviceAuthorizationReceipt {
|
|
84
|
+
const receipt = parseDeviceAuthorizationReceipt({
|
|
85
|
+
attemptId: row.id,
|
|
86
|
+
consent: consentFromRow(row),
|
|
87
|
+
credentialId: row.credential_id,
|
|
88
|
+
principalId: row.principal_id,
|
|
89
|
+
issuedAt: row.issued_at,
|
|
90
|
+
revokedAt: row.revoked_at,
|
|
91
|
+
})
|
|
92
|
+
if (!receipt) throw new Error('Invalid stored device receipt')
|
|
93
|
+
return receipt
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function status(row: DeviceReceiptRow): DeviceAuthorizationStatus {
|
|
97
|
+
if (row.status === 'issued') return { status: 'issued', receipt: receiptFromRow(row) }
|
|
98
|
+
if (row.expires_at <= unixNow()) return { status: 'expired' }
|
|
99
|
+
if (row.status === 'approved') {
|
|
100
|
+
const consent = consentFromRow(row)
|
|
101
|
+
return consent.keyExpiresAt <= unixNow() ? { status: 'expired' } : { status: 'approved', consent }
|
|
102
|
+
}
|
|
103
|
+
if (row.status === 'pending') return { status: 'pending', expiresAt: row.expires_at }
|
|
104
|
+
return { status: row.status }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function validProof(verifier: string, challenge: string): Promise<boolean> {
|
|
108
|
+
return timingSafeEqualHex(await hashToken(await pkceChallenge(verifier)), await hashToken(challenge))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function inspectedRow(services: Services, input: InspectDeviceAuthorizationInput) {
|
|
112
|
+
const row = await services.repositories.device.get(input.attemptId)
|
|
113
|
+
return row && row.app === input.app && await validProof(input.approvalVerifier, row.approval_challenge) ? row : null
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function startDeviceAuthorization(
|
|
117
|
+
services: Services,
|
|
118
|
+
raw: unknown,
|
|
119
|
+
clientAddress: string | null,
|
|
120
|
+
): Promise<StartDeviceAuthorizationResult> {
|
|
121
|
+
const input = parseStartDeviceAuthorizationInput(raw)
|
|
122
|
+
if (!input) return { ok: false, reason: 'invalid_input' }
|
|
123
|
+
if (!services.config.adminAudiences.has(input.app)) return { ok: false, reason: 'not_allowed' }
|
|
124
|
+
const throttle = await services.repositories.passwords.recordLoginFailure({
|
|
125
|
+
loginKeyHash: await hashToken(`device-start:${clientAddress === null ? 'installation' : `client:${clientAddress}`}`),
|
|
126
|
+
windowSeconds: 600,
|
|
127
|
+
maxAttempts: 21,
|
|
128
|
+
blockSeconds: 600,
|
|
129
|
+
})
|
|
130
|
+
if (throttle.blocked_until !== null && throttle.blocked_until > unixNow()) return { ok: false, reason: 'rate_limited' }
|
|
131
|
+
return { ok: true, ...await services.repositories.device.start(input) }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function pollDeviceAuthorization(services: Services, raw: unknown): Promise<PollDeviceAuthorizationResult> {
|
|
135
|
+
const input = parseDeviceAuthorizationProof(raw)
|
|
136
|
+
if (!input) return { ok: false, reason: 'invalid_attempt' }
|
|
137
|
+
const row = await services.repositories.device.get(input.attemptId)
|
|
138
|
+
if (!row || !await validProof(input.redemptionVerifier, row.redemption_challenge)) return { ok: false, reason: 'invalid_attempt' }
|
|
139
|
+
if (!await services.repositories.device.poll(row.id, row.redemption_challenge)) return { ok: false, reason: 'slow_down' }
|
|
140
|
+
const current = await services.repositories.device.get(row.id)
|
|
141
|
+
return current ? { ok: true, ...status(current) } : { ok: false, reason: 'invalid_attempt' }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function cancelDeviceAuthorization(services: Services, raw: unknown): Promise<CancelDeviceAuthorizationResult> {
|
|
145
|
+
const input = parseDeviceAuthorizationProof(raw)
|
|
146
|
+
if (!input) return { ok: false, reason: 'invalid_attempt' }
|
|
147
|
+
const row = await services.repositories.device.get(input.attemptId)
|
|
148
|
+
if (!row || !await validProof(input.redemptionVerifier, row.redemption_challenge)) return { ok: false, reason: 'invalid_attempt' }
|
|
149
|
+
await services.repositories.device.terminate(row.id, 'cancelled')
|
|
150
|
+
const current = await services.repositories.device.get(row.id)
|
|
151
|
+
return current?.status === 'issued' ? { ok: false, reason: 'already_issued' } : { ok: true }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export async function inspectDeviceAuthorization(services: Services, env: Env, raw: unknown): Promise<InspectDeviceAuthorizationResult> {
|
|
155
|
+
const input = parseInspectDeviceAuthorizationInput(raw)
|
|
156
|
+
if (!input) return { ok: false, reason: 'invalid_input' }
|
|
157
|
+
if (!await human(services, env, input)) return { ok: false, reason: 'not_allowed' }
|
|
158
|
+
const row = await inspectedRow(services, input)
|
|
159
|
+
if (!row) return { ok: false, reason: 'invalid_attempt' }
|
|
160
|
+
if (row.expires_at <= unixNow()) return { ok: false, reason: 'expired' }
|
|
161
|
+
if (row.status !== 'pending' && row.status !== 'approved') return { ok: false, reason: 'conflict' }
|
|
162
|
+
return { ok: true, request: requestFromRow(row) }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export async function approveDeviceAuthorization(services: Services, env: Env, raw: unknown): Promise<ApproveDeviceAuthorizationResult> {
|
|
166
|
+
const input = parseApproveDeviceAuthorizationInput(raw)
|
|
167
|
+
if (!input) return { ok: false, reason: 'invalid_input' }
|
|
168
|
+
const principal = await human(services, env, input)
|
|
169
|
+
if (!principal) return { ok: false, reason: 'not_allowed' }
|
|
170
|
+
const row = await inspectedRow(services, input)
|
|
171
|
+
if (!row) return { ok: false, reason: 'invalid_attempt' }
|
|
172
|
+
if (row.expires_at <= unixNow()) return { ok: false, reason: 'expired' }
|
|
173
|
+
const authority = await resolveAuthorityAt(services, principal, row.app)
|
|
174
|
+
const request = requestFromRow(row)
|
|
175
|
+
if (!request.permissions.every((action) => permits(authority.permissions, action, input.scope))) return { ok: false, reason: 'not_allowed' }
|
|
176
|
+
// The key's grant is written by raw SQL at exchange (`repositories/device.ts`), so the write rule is checked here.
|
|
177
|
+
if (await contractPatternsProblem(services.repositories.appSchema, row.app, request.permissions) !== null) {
|
|
178
|
+
return { ok: false, reason: 'not_allowed' }
|
|
179
|
+
}
|
|
180
|
+
// S8: the key ends no later than the approver's grants behind every action it carries.
|
|
181
|
+
const carried = request.permissions.map((action): PermissionEntry => ({ action, scope: input.scope, source: 'grant' }))
|
|
182
|
+
const deadline = authorityDeadline(authority, carried, unixNow())
|
|
183
|
+
const requested = unixNow() + row.key_lifetime_seconds
|
|
184
|
+
const keyExpiresAt = deadline === null ? requested : Math.min(requested, deadline)
|
|
185
|
+
if (keyExpiresAt <= unixNow()) return { ok: false, reason: 'not_allowed' }
|
|
186
|
+
const consent: DeviceAuthorizationConsent = {
|
|
187
|
+
consentId: uuidv7(),
|
|
188
|
+
app: row.app,
|
|
189
|
+
label: row.label,
|
|
190
|
+
permissions: request.permissions,
|
|
191
|
+
scope: input.scope,
|
|
192
|
+
keyExpiresAt,
|
|
193
|
+
}
|
|
194
|
+
if (await services.repositories.device.approve(row.id, principal.id, consent)) return { ok: true, consent }
|
|
195
|
+
const current = await services.repositories.device.get(row.id)
|
|
196
|
+
if (
|
|
197
|
+
current?.status === 'approved' && current.approver_id === principal.id && current.scope_type === input.scope.type
|
|
198
|
+
&& current.scope_value === input.scope.value
|
|
199
|
+
) {
|
|
200
|
+
return { ok: true, consent: consentFromRow(current) }
|
|
201
|
+
}
|
|
202
|
+
return { ok: false, reason: 'conflict' }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export async function denyDeviceAuthorization(services: Services, env: Env, raw: unknown): Promise<DenyDeviceAuthorizationResult> {
|
|
206
|
+
const input = parseInspectDeviceAuthorizationInput(raw)
|
|
207
|
+
if (!input) return { ok: false, reason: 'invalid_input' }
|
|
208
|
+
const principal = await human(services, env, input)
|
|
209
|
+
if (!principal) return { ok: false, reason: 'not_allowed' }
|
|
210
|
+
const row = await inspectedRow(services, input)
|
|
211
|
+
if (!row) return { ok: false, reason: 'invalid_attempt' }
|
|
212
|
+
if (row.expires_at <= unixNow()) return { ok: false, reason: 'expired' }
|
|
213
|
+
if (row.status !== 'pending' && row.status !== 'denied') return { ok: false, reason: 'conflict' }
|
|
214
|
+
await services.repositories.device.deny(row.id)
|
|
215
|
+
const current = await services.repositories.device.get(row.id)
|
|
216
|
+
return current?.status === 'denied' ? { ok: true } : { ok: false, reason: 'conflict' }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export async function listOwnDeviceAuthorizations(services: Services, env: Env, raw: unknown): Promise<ListOwnDeviceAuthorizationsResult> {
|
|
220
|
+
const input = parseListOwnDeviceAuthorizationsInput(raw)
|
|
221
|
+
if (!input) return { ok: false, reason: 'invalid_input' }
|
|
222
|
+
const principal = await human(services, env, input)
|
|
223
|
+
if (!principal) return { ok: false, reason: 'not_allowed' }
|
|
224
|
+
const limit = input.limit ?? 50
|
|
225
|
+
const rows = await services.repositories.device.list(principal.id, input.app, input.before, limit + 1)
|
|
226
|
+
const items = rows.slice(0, limit).map(receiptFromRow)
|
|
227
|
+
return { ok: true, items, nextCursor: rows.length > limit ? items.at(-1)?.attemptId ?? null : null }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export async function exchangeDeviceAuthorization(services: Services, raw: unknown, requestId: string): Promise<ExchangeDeviceAuthorizationResult> {
|
|
231
|
+
const input = parseExchangeDeviceAuthorizationInput(raw)
|
|
232
|
+
if (!input) return { ok: false, reason: 'invalid_attempt' }
|
|
233
|
+
const row = await services.repositories.device.get(input.attemptId)
|
|
234
|
+
if (!row || !await validProof(input.redemptionVerifier, row.redemption_challenge)) return { ok: false, reason: 'invalid_attempt' }
|
|
235
|
+
const currentStatus = status(row)
|
|
236
|
+
if (currentStatus.status === 'issued') return { ok: false, reason: 'already_issued', receipt: currentStatus.receipt }
|
|
237
|
+
if (currentStatus.status !== 'approved') return { ok: false, reason: currentStatus.status }
|
|
238
|
+
const consent = currentStatus.consent
|
|
239
|
+
if (consent.consentId !== input.consentId) return { ok: false, reason: 'consent_mismatch' }
|
|
240
|
+
const principal = row.approver_id === null ? null : await activeHuman(services, row.approver_id)
|
|
241
|
+
const permissions = principal
|
|
242
|
+
? await resolvePermissionsAt(services, principal, row.app)
|
|
243
|
+
: []
|
|
244
|
+
if (
|
|
245
|
+
!principal || !services.config.adminAudiences.has(row.app) || !consent.permissions.every((action) => permits(permissions, action, consent.scope))
|
|
246
|
+
|| await contractPatternsProblem(services.repositories.appSchema, row.app, consent.permissions) !== null
|
|
247
|
+
) {
|
|
248
|
+
await services.repositories.device.terminate(row.id, 'authority_lost')
|
|
249
|
+
const recovered = await services.repositories.device.get(row.id)
|
|
250
|
+
return recovered?.status === 'issued'
|
|
251
|
+
? { ok: false, reason: 'already_issued', receipt: receiptFromRow(recovered) }
|
|
252
|
+
: { ok: false, reason: 'authority_lost' }
|
|
253
|
+
}
|
|
254
|
+
if (consent.keyExpiresAt <= unixNow()) return { ok: false, reason: 'expired' }
|
|
255
|
+
const token = `${API_KEY_PREFIX}${generateToken()}`
|
|
256
|
+
const issued = await services.repositories.device.issue({
|
|
257
|
+
attemptId: row.id,
|
|
258
|
+
consentId: consent.consentId,
|
|
259
|
+
tokenHash: await hashToken(token),
|
|
260
|
+
requestId,
|
|
261
|
+
approverLabel: auditLabelOf(principal),
|
|
262
|
+
})
|
|
263
|
+
if (issued) return { ok: true, token, receipt: receiptFromRow({ ...issued, revoked_at: null }) }
|
|
264
|
+
const recovered = await services.repositories.device.get(row.id)
|
|
265
|
+
if (!recovered) return { ok: false, reason: 'invalid_attempt' }
|
|
266
|
+
const latest = status(recovered)
|
|
267
|
+
if (latest.status === 'issued') return { ok: false, reason: 'already_issued', receipt: latest.receipt }
|
|
268
|
+
return { ok: false, reason: latest.status === 'approved' ? 'expired' : latest.status }
|
|
269
|
+
}
|