@fabrika/iam 0.9.0-dev.32 → 0.9.0-dev.34

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.
Files changed (81) hide show
  1. package/fabrika.config.ts +47 -7
  2. package/migrations/0032_second_factor.sql +29 -0
  3. package/migrations/0033_oauth_grant_resources.sql +6 -0
  4. package/migrations/0034_oauth_device_authorizations.sql +24 -0
  5. package/migrations-postgres/0032_second_factor.sql +29 -0
  6. package/migrations-postgres/0033_oauth_grant_resources.sql +6 -0
  7. package/migrations-postgres/0034_oauth_device_authorizations.sql +24 -0
  8. package/package.json +12 -10
  9. package/src/admin/api-keys.ts +15 -5
  10. package/src/admin/grant-support.ts +10 -0
  11. package/src/admin/grants.ts +5 -2
  12. package/src/admin/handlers.ts +3 -0
  13. package/src/admin/principals.ts +2 -5
  14. package/src/admin/rpc.ts +8 -0
  15. package/src/admin/second-factor.ts +39 -0
  16. package/src/admin/sessions.ts +4 -4
  17. package/src/app.ts +1 -1
  18. package/src/auth/failure.ts +5 -0
  19. package/src/auth/http.ts +1 -1
  20. package/src/auth/invitations.ts +11 -3
  21. package/src/auth/login.ts +3 -2
  22. package/src/auth/oidc.ts +1 -1
  23. package/src/auth/password.ts +6 -6
  24. package/src/auth/routes.ts +11 -3
  25. package/src/auth/second-factor-challenge.ts +114 -0
  26. package/src/auth/second-factor.ts +276 -0
  27. package/src/auth/sessions.ts +72 -21
  28. package/src/auth.ts +19 -4
  29. package/src/catalog/cs.ts +32 -0
  30. package/src/catalog/en.ts +33 -0
  31. package/src/catalog/index.ts +28 -0
  32. package/src/catalog/uk.ts +33 -0
  33. package/src/data-key.ts +92 -0
  34. package/src/db.ts +12 -1
  35. package/src/env.ts +15 -2
  36. package/src/node/runtime.ts +7 -0
  37. package/src/oauth-client-auth.ts +6 -2
  38. package/src/oauth-config.ts +5 -5
  39. package/src/oauth-device-page.ts +173 -0
  40. package/src/oauth-device.ts +265 -0
  41. package/src/oauth-http.ts +53 -7
  42. package/src/oauth.ts +105 -37
  43. package/src/oidc-session.ts +25 -0
  44. package/src/repositories/audit.ts +4 -2
  45. package/src/repositories/oauth.ts +171 -4
  46. package/src/repositories/second-factors.ts +165 -0
  47. package/src/repositories/sessions.ts +17 -9
  48. package/src/second-factor-pages.ts +129 -0
  49. package/src/services.ts +11 -2
  50. package/src/tokens.ts +3 -3
  51. package/src/totp.ts +132 -0
  52. package/types/src/admin/api-keys.d.ts +3 -3
  53. package/types/src/admin/grant-support.d.ts +2 -0
  54. package/types/src/admin/handlers.d.ts +3 -0
  55. package/types/src/admin/second-factor.d.ts +5 -0
  56. package/types/src/auth/failure.d.ts +10 -0
  57. package/types/src/auth/http.d.ts +1 -1
  58. package/types/src/auth/invitations.d.ts +2 -1
  59. package/types/src/auth/password.d.ts +1 -1
  60. package/types/src/auth/second-factor-challenge.d.ts +34 -0
  61. package/types/src/auth/second-factor.d.ts +19 -0
  62. package/types/src/auth/sessions.d.ts +22 -9
  63. package/types/src/auth.d.ts +9 -3
  64. package/types/src/catalog/index.d.ts +28 -0
  65. package/types/src/data-key.d.ts +19 -0
  66. package/types/src/db.d.ts +4 -1
  67. package/types/src/env.d.ts +15 -2
  68. package/types/src/oauth-config.d.ts +3 -3
  69. package/types/src/oauth-device-page.d.ts +7 -0
  70. package/types/src/oauth-device.d.ts +51 -0
  71. package/types/src/oauth-http.d.ts +5 -2
  72. package/types/src/oauth.d.ts +34 -8
  73. package/types/src/oidc-session.d.ts +12 -0
  74. package/types/src/repositories/audit.d.ts +2 -1
  75. package/types/src/repositories/oauth.d.ts +48 -1
  76. package/types/src/repositories/second-factors.d.ts +68 -0
  77. package/types/src/repositories/sessions.d.ts +7 -3
  78. package/types/src/rpc-inputs.d.ts +23 -23
  79. package/types/src/second-factor-pages.d.ts +42 -0
  80. package/types/src/services.d.ts +4 -0
  81. package/types/src/totp.d.ts +32 -0
@@ -0,0 +1,68 @@
1
+ import { type SqlDatabase } from '@fabrika/platform';
2
+ import type { SealedValue } from '../data-key';
3
+ import { type AuditEventInput, AuditWriter } from './audit';
4
+ /** A row of `second_factors` (migration 0032): one TOTP authenticator per principal, its secret sealed. */
5
+ export interface SecondFactorRow {
6
+ principal_id: string;
7
+ ciphertext: string;
8
+ value_iv: string;
9
+ wrapped_dek: string;
10
+ dek_iv: string;
11
+ created_at: number;
12
+ /** NULL while the enrolment waits for its first valid code. Only a confirmed factor is asked for at sign-in. */
13
+ confirmed_at: number | null;
14
+ /** The time step of the last accepted code; a code of that step or an earlier one is never accepted again. */
15
+ last_step: number | null;
16
+ /** The confirmation that wrote the recovery codes; NULL until one did. */
17
+ confirmation_id: string | null;
18
+ }
19
+ /** What an administrator sees of a principal's second factor. */
20
+ export interface SecondFactorStatus {
21
+ readonly confirmedAt: number | null;
22
+ readonly recoveryCodesLeft: number;
23
+ }
24
+ export declare function sealedValueOf(row: SecondFactorRow): SealedValue;
25
+ /**
26
+ * A person's TOTP authenticator and recovery codes. Every write that touches more than one of a principal's rows locks
27
+ * the principal row first on Postgres (`principalLockStatements`), the order every writer of a principal's rows keeps.
28
+ */
29
+ export declare class SecondFactorRepository {
30
+ protected readonly db: SqlDatabase;
31
+ private readonly writer;
32
+ constructor(db: SqlDatabase, writer?: AuditWriter);
33
+ get(principalId: string): Promise<SecondFactorRow | null>;
34
+ /** Whether a sign-in of the principal needs its second factor. */
35
+ isConfirmed(principalId: string): Promise<boolean>;
36
+ status(principalId: string): Promise<SecondFactorStatus>;
37
+ /**
38
+ * Store a new secret that waits for its first code, replacing one that still waits. A confirmed factor is never
39
+ * replaced here (false): only an administrator's reset removes it. Nothing is recorded: an unconfirmed secret asks
40
+ * nobody for anything; its confirmation is the enrolment, and that is recorded.
41
+ */
42
+ begin(principalId: string, sealed: SealedValue): Promise<boolean>;
43
+ /**
44
+ * Confirm the waiting secret whose ciphertext the caller checked the code against, with its record, and replace the
45
+ * principal's recovery codes with `recoveryCodeHashes`, in one transaction. The codes are written only when the
46
+ * confirmation changed the row, so a secret replaced in between confirms nothing (false) and issues no code. The guard
47
+ * reads this attempt's own `confirmation_id`, never the timestamp: a second confirmation in the same second must not
48
+ * replace the codes the first one showed.
49
+ */
50
+ confirm(input: {
51
+ principalId: string;
52
+ ciphertext: string;
53
+ step: number;
54
+ recoveryCodeHashes: readonly string[];
55
+ }, audit: AuditEventInput): Promise<boolean>;
56
+ /**
57
+ * Accept a code of `step` for a confirmed factor: true only when no code of this step or a later one was accepted
58
+ * before. The guard is the statement itself, so two submissions of one code accept it once.
59
+ */
60
+ acceptStep(principalId: string, step: number): Promise<boolean>;
61
+ /** Spend one unused recovery code of a confirmed factor, with its record; false when the code is not one. */
62
+ spendRecoveryCode(principalId: string, codeHash: string, audit: AuditEventInput): Promise<boolean>;
63
+ /**
64
+ * Remove the principal's authenticator and recovery codes, with one record, so its next sign-in asks for no second
65
+ * factor and it may enrol again. False when it had neither, and nothing is recorded.
66
+ */
67
+ reset(principalId: string, audit: AuditEventInput): Promise<boolean>;
68
+ }
@@ -30,7 +30,11 @@ export interface SessionRow {
30
30
  /** The terminal's generation the login was asserted in; the session is valid only while it is still the terminal's. */
31
31
  terminal_generation: number | null;
32
32
  }
33
- export interface ListedSessionRow extends SessionRow {
33
+ /** A session as a lookup returns it: with the time its login signed in, the parent's for a child session. */
34
+ export interface SignedInSessionRow extends SessionRow {
35
+ signed_in_at: number;
36
+ }
37
+ export interface ListedSessionRow extends SignedInSessionRow {
34
38
  parent_expires_at: number | null;
35
39
  parent_revoked_at: number | null;
36
40
  }
@@ -86,12 +90,12 @@ export declare class SessionRepository {
86
90
  * A session on a terminal is valid only while that terminal is enrolled, and so is a child whose parent is on one: a
87
91
  * revoked terminal ends them at once, with no sweep (ADR-0067).
88
92
  */
89
- getActiveSessionByHash(tokenHash: string): Promise<SessionRow | null>;
93
+ getActiveSessionByHash(tokenHash: string): Promise<SignedInSessionRow | null>;
90
94
  /**
91
95
  * The same validity rule as `getActiveSessionByHash`, addressed by id — how a handoff code reaches
92
96
  * the login it was issued against, which it holds by id rather than by a hash it must not store.
93
97
  */
94
- getActiveSessionById(id: string): Promise<SessionRow | null>;
98
+ getActiveSessionById(id: string): Promise<SignedInSessionRow | null>;
95
99
  /**
96
100
  * Whether the IAM login `parentSessionId` holds a live session bound to `app` — whether this login already chose that
97
101
  * application. The parent's own validity is the caller's: it reads the login it asks about first.
@@ -31,14 +31,14 @@ export declare const exchangeAuthCodeSchema: z.ZodCatch<z.ZodObject<{
31
31
  verifier: z.ZodString;
32
32
  requestId: z.ZodString;
33
33
  }, "strip", z.ZodTypeAny, {
34
- code: string;
35
34
  app: string;
36
35
  requestId: string;
36
+ code: string;
37
37
  verifier: string;
38
38
  }, {
39
- code: string;
40
39
  app: string;
41
40
  requestId: string;
41
+ code: string;
42
42
  verifier: string;
43
43
  }>>;
44
44
  export declare const issueKeySchema: z.ZodCatch<z.ZodEffects<z.ZodObject<{
@@ -154,8 +154,8 @@ export declare const issueKeySchema: z.ZodCatch<z.ZodEffects<z.ZodObject<{
154
154
  label: z.ZodEffects<z.ZodOptional<z.ZodNullable<z.ZodString>>, string | undefined, string | null | undefined>;
155
155
  expiresAt: z.ZodNumber;
156
156
  }, "strip", z.ZodTypeAny, {
157
- expiresAt: number;
158
157
  app: string;
158
+ expiresAt: number;
159
159
  credential: string | null;
160
160
  requestId: string;
161
161
  service?: {
@@ -176,8 +176,8 @@ export declare const issueKeySchema: z.ZodCatch<z.ZodEffects<z.ZodObject<{
176
176
  }[] | undefined;
177
177
  principalId?: string | undefined;
178
178
  }, {
179
- expiresAt: number;
180
179
  app: string;
180
+ expiresAt: number;
181
181
  credential: string | null;
182
182
  requestId: string;
183
183
  service?: {
@@ -220,8 +220,8 @@ export declare const issueKeySchema: z.ZodCatch<z.ZodEffects<z.ZodObject<{
220
220
  credential: string | null;
221
221
  requestId: string;
222
222
  }, {
223
- expiresAt: number;
224
223
  app: string;
224
+ expiresAt: number;
225
225
  credential: string | null;
226
226
  requestId: string;
227
227
  service?: {
@@ -248,13 +248,13 @@ export declare const revokeKeySchema: z.ZodCatch<z.ZodObject<{
248
248
  credential: z.ZodNullable<z.ZodString>;
249
249
  requestId: z.ZodString;
250
250
  }, "strip", z.ZodTypeAny, {
251
- id: string;
252
251
  app: string;
252
+ id: string;
253
253
  credential: string | null;
254
254
  requestId: string;
255
255
  }, {
256
- id: string;
257
256
  app: string;
257
+ id: string;
258
258
  credential: string | null;
259
259
  requestId: string;
260
260
  }>>;
@@ -394,15 +394,15 @@ export declare const assignMemberSchema: z.ZodCatch<z.ZodObject<{
394
394
  resend: z.ZodOptional<z.ZodBoolean>;
395
395
  locale: z.ZodOptional<z.ZodEnum<["en", "cs", "uk"]>>;
396
396
  }, "strict", z.ZodTypeAny, {
397
- message?: string | undefined;
398
- resend?: boolean | undefined;
399
397
  locale?: "en" | "cs" | "uk" | undefined;
400
- label?: string | undefined;
401
- }, {
402
398
  message?: string | undefined;
399
+ label?: string | undefined;
403
400
  resend?: boolean | undefined;
401
+ }, {
404
402
  locale?: "en" | "cs" | "uk" | undefined;
403
+ message?: string | undefined;
405
404
  label?: string | undefined;
405
+ resend?: boolean | undefined;
406
406
  }>>;
407
407
  app: z.ZodString;
408
408
  credential: z.ZodNullable<z.ZodString>;
@@ -423,10 +423,10 @@ export declare const assignMemberSchema: z.ZodCatch<z.ZodObject<{
423
423
  delegation: string;
424
424
  role: string;
425
425
  invitation?: {
426
- message?: string | undefined;
427
- resend?: boolean | undefined;
428
426
  locale?: "en" | "cs" | "uk" | undefined;
427
+ message?: string | undefined;
429
428
  label?: string | undefined;
429
+ resend?: boolean | undefined;
430
430
  } | undefined;
431
431
  }, {
432
432
  app: string;
@@ -444,10 +444,10 @@ export declare const assignMemberSchema: z.ZodCatch<z.ZodObject<{
444
444
  delegation: string;
445
445
  role: string;
446
446
  invitation?: {
447
- message?: string | undefined;
448
- resend?: boolean | undefined;
449
447
  locale?: "en" | "cs" | "uk" | undefined;
448
+ message?: string | undefined;
450
449
  label?: string | undefined;
450
+ resend?: boolean | undefined;
451
451
  } | undefined;
452
452
  }>>;
453
453
  export declare const revokeMemberSchema: z.ZodCatch<z.ZodObject<{
@@ -642,13 +642,13 @@ export declare const revokeLinkSchema: z.ZodCatch<z.ZodObject<{
642
642
  credential: z.ZodNullable<z.ZodString>;
643
643
  requestId: z.ZodString;
644
644
  }, "strip", z.ZodTypeAny, {
645
- id: string;
646
645
  app: string;
646
+ id: string;
647
647
  credential: string | null;
648
648
  requestId: string;
649
649
  }, {
650
- id: string;
651
650
  app: string;
651
+ id: string;
652
652
  credential: string | null;
653
653
  requestId: string;
654
654
  }>>;
@@ -658,13 +658,13 @@ export declare const inspectIdentificationSchema: z.ZodCatch<z.ZodObject<{
658
658
  credential: z.ZodNullable<z.ZodString>;
659
659
  requestId: z.ZodString;
660
660
  }, "strip", z.ZodTypeAny, {
661
- id: string;
662
661
  app: string;
662
+ id: string;
663
663
  credential: string | null;
664
664
  requestId: string;
665
665
  }, {
666
- id: string;
667
666
  app: string;
667
+ id: string;
668
668
  credential: string | null;
669
669
  requestId: string;
670
670
  }>>;
@@ -675,14 +675,14 @@ export declare const assertIdentitySchema: z.ZodCatch<z.ZodObject<{
675
675
  credential: z.ZodNullable<z.ZodString>;
676
676
  requestId: z.ZodString;
677
677
  }, "strip", z.ZodTypeAny, {
678
- id: string;
679
678
  app: string;
679
+ id: string;
680
680
  principalId: string;
681
681
  credential: string | null;
682
682
  requestId: string;
683
683
  }, {
684
- id: string;
685
684
  app: string;
685
+ id: string;
686
686
  principalId: string;
687
687
  credential: string | null;
688
688
  requestId: string;
@@ -693,13 +693,13 @@ export declare const cancelIdentificationSchema: z.ZodCatch<z.ZodObject<{
693
693
  credential: z.ZodNullable<z.ZodString>;
694
694
  requestId: z.ZodString;
695
695
  }, "strip", z.ZodTypeAny, {
696
- id: string;
697
696
  app: string;
697
+ id: string;
698
698
  credential: string | null;
699
699
  requestId: string;
700
700
  }, {
701
- id: string;
702
701
  app: string;
702
+ id: string;
703
703
  credential: string | null;
704
704
  requestId: string;
705
705
  }>>;
@@ -0,0 +1,42 @@
1
+ import type { Locale } from '@fabrika/auth-core';
2
+ import { SECOND_FACTOR_ENROL_PATH } from '@fabrika/iam-contract';
3
+ import type { InstallationBrand } from '@fabrika/proxy-core/brand';
4
+ import { type AppPresentation } from './app-presentation';
5
+ export { SECOND_FACTOR_ENROL_PATH };
6
+ /** Where a sign-in waiting for its second factor posts the code. */
7
+ export declare const SECOND_FACTOR_PATH = "/auth/second-factor";
8
+ /** Why the code form is shown again. */
9
+ export type ChallengeProblem = 'invalid' | 'throttled' | 'app_codes_unavailable';
10
+ /**
11
+ * The code a sign-in asks for after its first factor. The pending sign-in rides in the challenge cookie, never in the
12
+ * form. A verified code answers the login's own 302, to the handoff's registered origin, which `form-action` admits.
13
+ */
14
+ export declare function secondFactorChallengePage(input: {
15
+ readonly presentation: AppPresentation;
16
+ readonly redirectOrigin?: string;
17
+ readonly problem?: ChallengeProblem;
18
+ }): Response;
19
+ export interface EnrolPageBase {
20
+ readonly brand: InstallationBrand | null;
21
+ readonly locale: Locale;
22
+ }
23
+ /** The start of an enrolment: what two-step sign-in asks of the person, and the button that issues a key. */
24
+ export declare function secondFactorIntroPage(input: EnrolPageBase): Response;
25
+ /** The key to enrol, as a QR code and as text, and the form that confirms it with the first code. */
26
+ export declare function secondFactorEnrolPage(input: EnrolPageBase & {
27
+ readonly qrCode: string;
28
+ readonly key: string;
29
+ readonly invalidCode?: boolean;
30
+ }): Response;
31
+ /** The recovery codes, shown once, at the confirmed enrolment. `landing` is where the person continues. */
32
+ export declare function recoveryCodesPage(input: EnrolPageBase & {
33
+ readonly codes: readonly string[];
34
+ readonly landing: string;
35
+ }): Response;
36
+ /** An authenticator is already enrolled: since when, and how many recovery codes are left. */
37
+ export declare function secondFactorEnrolledPage(input: EnrolPageBase & {
38
+ readonly confirmedAt: number;
39
+ readonly recoveryCodesLeft: number;
40
+ }): Response;
41
+ /** The installation holds no data key, so no authenticator can be enrolled. */
42
+ export declare function secondFactorUnavailablePage(input: EnrolPageBase): Response;
@@ -16,6 +16,8 @@ export interface Services {
16
16
  readonly oidc: OidcClient | null;
17
17
  readonly passwordHasher: PasswordHasher;
18
18
  readonly email: EmailSender | null;
19
+ /** `FABRIKA_IAM_DATA_KEY`, which seals TOTP secrets (`src/data-key.ts`); null when unset, and TOTP enrolment is unavailable. */
20
+ readonly dataKey: Uint8Array<ArrayBuffer> | null;
19
21
  readonly config: Config;
20
22
  }
21
23
  export interface Config {
@@ -46,6 +48,8 @@ export interface Config {
46
48
  readonly authentication: {
47
49
  readonly oidc: {
48
50
  readonly enabled: boolean;
51
+ /** How long after its sign-in an OIDC login is usable (`OIDC_SESSION_HOURS`); then the provider is asked again. */
52
+ readonly sessionCapSeconds: number;
49
53
  };
50
54
  readonly password: {
51
55
  readonly enabled: boolean;
@@ -0,0 +1,32 @@
1
+ export declare const TOTP_PERIOD_SECONDS = 30;
2
+ export declare const TOTP_DIGITS = 6;
3
+ /** Recovery codes issued at enrolment; each signs in once in place of a code. */
4
+ export declare const RECOVERY_CODE_COUNT = 10;
5
+ export declare function generateTotpSecret(): Uint8Array<ArrayBuffer>;
6
+ /** RFC 4648 base32 without padding: what an authenticator app expects in `secret=` and what a person types. */
7
+ export declare function base32(bytes: Uint8Array): string;
8
+ /** The time step `unixSeconds` falls in. */
9
+ export declare function totpStep(unixSeconds: number): number;
10
+ /** The code of `step` (RFC 4226 dynamic truncation), zero-padded to six digits. */
11
+ export declare function totpCode(secret: Uint8Array<ArrayBuffer>, step: number): Promise<string>;
12
+ /** Six digits, as a person types them: spaces are dropped, anything else is not a code. */
13
+ export declare function parseTotpCode(input: string): string | null;
14
+ /**
15
+ * The step `code` belongs to, within the accepted drift of `now` and after `lastStep`, or null. A step at or before
16
+ * `lastStep` is refused, so an accepted code — and every earlier one — is never accepted again.
17
+ */
18
+ export declare function matchingStep(secret: Uint8Array<ArrayBuffer>, code: string, unixSeconds: number, lastStep: number | null): Promise<number | null>;
19
+ /** The URI an authenticator app enrols from, as a QR code or typed in. `issuer` names the installation, `account` the person. */
20
+ export declare function otpauthUri(input: {
21
+ readonly issuer: string;
22
+ readonly account: string;
23
+ readonly secret: Uint8Array;
24
+ }): string;
25
+ /** The URI as a QR code: an SVG `data:` URI, which the page CSP's `img-src data:` admits and which runs no script. */
26
+ export declare function qrCodeDataUri(text: string): string;
27
+ /** Fresh recovery codes, grouped by four for reading: `abcd-efgh-ijkl-mnop`. */
28
+ export declare function generateRecoveryCodes(): string[];
29
+ /** A recovery code as typed: case, spaces and dashes do not matter; anything else is not one. */
30
+ export declare function parseRecoveryCode(input: string): string | null;
31
+ /** The stored form of a recovery code: its hash, bound to its principal, so no code is stored and none spends elsewhere. */
32
+ export declare function recoveryCodeHash(principalId: string, code: string): Promise<string>;