@fabrika/iam 0.9.0-dev.31 → 0.9.0-dev.33
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 +11 -3
- package/migrations/0032_second_factor.sql +29 -0
- package/migrations-postgres/0032_second_factor.sql +29 -0
- package/package.json +11 -10
- package/src/admin/api-keys.ts +15 -5
- package/src/admin/grant-support.ts +10 -0
- package/src/admin/grants.ts +5 -2
- package/src/admin/handlers.ts +3 -0
- package/src/admin/principals.ts +2 -5
- package/src/admin/rpc.ts +8 -0
- package/src/admin/second-factor.ts +39 -0
- package/src/admin/sessions.ts +4 -4
- package/src/auth/failure.ts +5 -0
- package/src/auth/http.ts +1 -1
- package/src/auth/invitations.ts +11 -3
- package/src/auth/login.ts +3 -2
- package/src/auth/oidc.ts +1 -1
- package/src/auth/password.ts +6 -6
- package/src/auth/routes.ts +11 -3
- package/src/auth/second-factor-challenge.ts +114 -0
- package/src/auth/second-factor.ts +276 -0
- package/src/auth/sessions.ts +72 -21
- package/src/auth.ts +19 -4
- package/src/catalog/cs.ts +32 -0
- package/src/catalog/en.ts +33 -0
- package/src/catalog/index.ts +28 -0
- package/src/catalog/uk.ts +33 -0
- package/src/data-key.ts +92 -0
- package/src/db.ts +12 -1
- package/src/env.ts +12 -0
- package/src/node/runtime.ts +7 -0
- package/src/oauth.ts +7 -3
- package/src/oidc-session.ts +25 -0
- package/src/repositories/second-factors.ts +165 -0
- package/src/repositories/sessions.ts +17 -9
- package/src/second-factor-pages.ts +129 -0
- package/src/services.ts +11 -2
- package/src/signing.ts +13 -5
- package/src/tokens.ts +18 -11
- package/src/totp.ts +132 -0
- package/types/src/admin/api-keys.d.ts +3 -3
- package/types/src/admin/grant-support.d.ts +2 -0
- package/types/src/admin/handlers.d.ts +3 -0
- package/types/src/admin/second-factor.d.ts +5 -0
- package/types/src/auth/failure.d.ts +10 -0
- package/types/src/auth/http.d.ts +1 -1
- package/types/src/auth/invitations.d.ts +2 -1
- package/types/src/auth/password.d.ts +1 -1
- package/types/src/auth/second-factor-challenge.d.ts +34 -0
- package/types/src/auth/second-factor.d.ts +19 -0
- package/types/src/auth/sessions.d.ts +22 -9
- package/types/src/auth.d.ts +9 -3
- package/types/src/catalog/index.d.ts +28 -0
- package/types/src/data-key.d.ts +19 -0
- package/types/src/db.d.ts +4 -1
- package/types/src/env.d.ts +12 -0
- package/types/src/oidc-session.d.ts +12 -0
- package/types/src/repositories/second-factors.d.ts +68 -0
- package/types/src/repositories/sessions.d.ts +7 -3
- package/types/src/second-factor-pages.d.ts +42 -0
- package/types/src/services.d.ts +4 -0
- package/types/src/signing.d.ts +4 -4
- package/types/src/tokens.d.ts +5 -2
- package/types/src/totp.d.ts +32 -0
|
@@ -2,8 +2,9 @@ import { type AuthenticationMethod, type PrincipalRow, type SessionRow } from '.
|
|
|
2
2
|
import { type RequestContext } from '../env';
|
|
3
3
|
import { type Services } from '../services';
|
|
4
4
|
import { type Handoff } from './handoff';
|
|
5
|
+
import { type AuthEnv } from './oidc-flight';
|
|
5
6
|
/** The caller's IAM session, if the browser already holds a valid one on this host. */
|
|
6
|
-
export declare function currentSession(request: Request, services: Services): Promise<
|
|
7
|
+
export declare function currentSession(request: Request, services: Services): Promise<import("../db").SignedInSessionRow | null>;
|
|
7
8
|
/** A login a handoff may continue: the browser's IAM session and its principal, an active user. */
|
|
8
9
|
export interface CurrentLogin {
|
|
9
10
|
readonly session: SessionRow;
|
|
@@ -11,12 +12,8 @@ export interface CurrentLogin {
|
|
|
11
12
|
}
|
|
12
13
|
/** The caller's IAM login when its principal is an active user, else null — which signs in afresh. */
|
|
13
14
|
export declare function currentLogin(request: Request, services: Services): Promise<CurrentLogin | null>;
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
* be logged opens no session and answers 503. A principal that administers at IAM, the installation or the people of
|
|
17
|
-
* an application (ADR-0058), is logged `privileged`.
|
|
18
|
-
*/
|
|
19
|
-
export declare function issueSession(request: Request, services: Services, ctx: RequestContext, input: {
|
|
15
|
+
/** A login a proven first factor opens. */
|
|
16
|
+
export interface LoginInput {
|
|
20
17
|
principalId: string;
|
|
21
18
|
idpSub?: string | null;
|
|
22
19
|
email?: string | null;
|
|
@@ -29,13 +26,29 @@ export declare function issueSession(request: Request, services: Services, ctx:
|
|
|
29
26
|
clearOidc?: boolean;
|
|
30
27
|
/** When set, land on the app's callback with a code instead of on `redirect` (ADR-0022). */
|
|
31
28
|
handoff?: Handoff | null;
|
|
32
|
-
|
|
29
|
+
/** Set the known-device cookie once the login is recorded: a password login's (`auth/known-device.ts`). */
|
|
30
|
+
rememberDevice?: boolean;
|
|
31
|
+
/** Set only by the second-factor form, after the principal's code or recovery code was accepted. */
|
|
32
|
+
secondFactor?: 'verified';
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Open a login: the session and its `auth_log` row are written in one batch, before the answer. A login that cannot
|
|
36
|
+
* be logged opens no session and answers 503. A principal that administers at IAM, the installation or the people of
|
|
37
|
+
* an application (ADR-0058), is logged `privileged`.
|
|
38
|
+
*
|
|
39
|
+
* A principal with a confirmed second factor gets no session here until its code is verified: the answer is the code
|
|
40
|
+
* form and a challenge cookie holding this login (`second-factor-challenge.ts`). Every password, OIDC, password-action
|
|
41
|
+
* and invitation login passes through here, so none skips the factor; only the local-dev bypass does, which no
|
|
42
|
+
* installation outside `ENVIRONMENT=local` serves. A terminal's assertion (`identification.ts`) opens no session here
|
|
43
|
+
* and asks no factor: it reaches only the asserting application, never IAM's administration.
|
|
44
|
+
*/
|
|
45
|
+
export declare function issueSession(request: Request, services: Services, env: AuthEnv, ctx: RequestContext, input: LoginInput): Promise<Response>;
|
|
33
46
|
/**
|
|
34
47
|
* Answer a refused login after its `deny` row is in `auth_log`. A refusal that cannot be logged answers 503 instead,
|
|
35
48
|
* so no login attempt goes unrecorded.
|
|
36
49
|
*/
|
|
37
50
|
/** The fixed `auth_log.reason` of a refused login. A reason never carries what the caller submitted. */
|
|
38
|
-
export type LoginRefusal = 'password_invalid' | 'password_throttled' | 'password_malformed' | 'handoff_refused' | 'login_cross_origin' | 'oidc_invalid_state' | 'oidc_code_exchange_failed' | 'oidc_invalid_id_token' | 'oidc_not_admitted' | 'oidc_disabled' | 'oidc_unknown_principal' | 'oidc_external_principal' | 'local_login_refused';
|
|
51
|
+
export type LoginRefusal = 'password_invalid' | 'password_throttled' | 'password_malformed' | 'handoff_refused' | 'login_cross_origin' | 'oidc_invalid_state' | 'oidc_code_exchange_failed' | 'oidc_invalid_id_token' | 'oidc_not_admitted' | 'oidc_disabled' | 'oidc_unknown_principal' | 'oidc_external_principal' | 'local_login_refused' | 'second_factor_invalid' | 'second_factor_throttled' | 'second_factor_expired';
|
|
39
52
|
export declare function refuseLogin(services: Services, request: Request, entry: {
|
|
40
53
|
requestId: string;
|
|
41
54
|
principalId: string | null;
|
package/types/src/auth.d.ts
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
import { type PermissionEntry, type PrincipalType, type TokenPrincipalType } from '@fabrika/auth-core';
|
|
2
|
-
import type {
|
|
2
|
+
import type { SignedInSessionRow } from './db';
|
|
3
3
|
import type { Env } from './env';
|
|
4
4
|
import type { Config, Services } from './services';
|
|
5
5
|
/** Fixed identity of the local-dev login (`LOCAL_DEV_LOGIN`, `auth/login.ts`). */
|
|
6
6
|
export declare const LOCAL_DEV_ADMIN_ID = "local-dev-admin";
|
|
7
7
|
export declare const LOCAL_DEV_ADMIN_EMAIL = "admin@local.test";
|
|
8
8
|
/**
|
|
9
|
-
* May this session still be used, given what the installation enables NOW?
|
|
9
|
+
* May this session still be used at `now` (unix seconds), given what the installation enables NOW?
|
|
10
10
|
*
|
|
11
11
|
* **Checked at USE, not at creation, and that is the point.** Turning a method off changes what the
|
|
12
12
|
* next login may do and nothing about what was already issued. Sessions live 30 days, so an
|
|
13
13
|
* installation that moved to password-only kept honouring every OIDC login for a month — and, for the
|
|
14
14
|
* bypass, every one of those is a global admin.
|
|
15
15
|
*
|
|
16
|
+
* An OIDC login is usable for `sessionCapSeconds` after it signed in; then the provider is asked again, so a person
|
|
17
|
+
* blocked there loses access within the cap. Measured here, from the login's sign-in, so it also ends logins issued
|
|
18
|
+
* before the cap was lowered, and the app sessions derived from them.
|
|
19
|
+
*
|
|
16
20
|
* The bypass carries `local_dev` as its own method rather than an exemption
|
|
17
21
|
* here, so this stays a total function over the enum: a method added later has to answer the question.
|
|
18
22
|
*/
|
|
19
|
-
export declare function sessionUsable(session: Pick<
|
|
23
|
+
export declare function sessionUsable(session: Pick<SignedInSessionRow, 'authentication_method' | 'expires_at' | 'signed_in_at'>, config: Pick<Config, 'authentication' | 'localDevLogin'>, now: number): boolean;
|
|
24
|
+
/** When a session ends by its own clock: its expiry, or earlier for an OIDC login, its sign-in plus the installation's cap. */
|
|
25
|
+
export declare function sessionEndsAt(session: Pick<SignedInSessionRow, 'authentication_method' | 'expires_at' | 'signed_in_at'>, config: Pick<Config, 'authentication'>): number;
|
|
20
26
|
/**
|
|
21
27
|
* Fixed identity for the SEEDED PROVISIONING KEY below. A stable `service` principal with this id is
|
|
22
28
|
* seeded into `principals` (migration `0001_init.sql`) so the audit the provisioning
|
|
@@ -67,6 +67,34 @@ export interface PageText {
|
|
|
67
67
|
readonly enrolledTitle: string;
|
|
68
68
|
readonly enrolledText: (label: string) => string;
|
|
69
69
|
};
|
|
70
|
+
/** The second factor (TOTP): the code a sign-in asks for, and enrolling an authenticator app. */
|
|
71
|
+
readonly secondFactor: {
|
|
72
|
+
readonly challengeTitle: string;
|
|
73
|
+
readonly challengeText: string;
|
|
74
|
+
readonly codeLabel: string;
|
|
75
|
+
readonly submit: string;
|
|
76
|
+
readonly invalidCode: string;
|
|
77
|
+
/** Too many wrong codes for this account; the next attempt waits. */
|
|
78
|
+
readonly throttled: string;
|
|
79
|
+
/** The data key is unset or does not open the secret: only a recovery code signs in. */
|
|
80
|
+
readonly appCodesUnavailable: string;
|
|
81
|
+
readonly startOver: string;
|
|
82
|
+
readonly enrolTitle: string;
|
|
83
|
+
readonly enrolText: string;
|
|
84
|
+
readonly start: string;
|
|
85
|
+
readonly scanText: string;
|
|
86
|
+
readonly secretLabel: string;
|
|
87
|
+
readonly confirmLabel: string;
|
|
88
|
+
readonly confirm: string;
|
|
89
|
+
readonly enrolledTitle: string;
|
|
90
|
+
/** `enrolledOn` is an ISO date; `left` the recovery codes not used yet. */
|
|
91
|
+
readonly enrolledText: (enrolledOn: string, left: number) => string;
|
|
92
|
+
readonly recoveryTitle: string;
|
|
93
|
+
readonly recoveryText: string;
|
|
94
|
+
readonly continue: string;
|
|
95
|
+
readonly unavailableTitle: string;
|
|
96
|
+
readonly unavailableText: string;
|
|
97
|
+
};
|
|
70
98
|
readonly invitation: {
|
|
71
99
|
readonly title: string;
|
|
72
100
|
readonly text: string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** AES-256: the installation's key and each data key. */
|
|
2
|
+
export declare const DATA_KEY_BYTES = 32;
|
|
3
|
+
/** The sealed pieces as stored: all base64, never a plaintext value or data key. */
|
|
4
|
+
export interface SealedValue {
|
|
5
|
+
readonly ciphertext: string;
|
|
6
|
+
readonly valueIv: string;
|
|
7
|
+
readonly wrappedDek: string;
|
|
8
|
+
readonly dekIv: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Parse `FABRIKA_IAM_DATA_KEY` (base64 of 32 random bytes) once at boot. Unset or empty is null: nothing can be sealed,
|
|
12
|
+
* so TOTP enrolment is unavailable. Anything else that is not 32 bytes fails the boot; a short key would be a weak key
|
|
13
|
+
* nobody notices.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseDataKey(raw: string | undefined): Uint8Array<ArrayBuffer> | null;
|
|
16
|
+
/** Seal `value` for `owner` (the associated data of both layers). */
|
|
17
|
+
export declare function sealValue(dataKey: Uint8Array<ArrayBuffer>, owner: string, value: Uint8Array<ArrayBuffer>): Promise<SealedValue>;
|
|
18
|
+
/** Open a value `sealValue` sealed for `owner`. A wrong key, another owner or any tampered piece throws. */
|
|
19
|
+
export declare function openValue(dataKey: Uint8Array<ArrayBuffer>, owner: string, sealed: SealedValue): Promise<Uint8Array<ArrayBuffer>>;
|
package/types/src/db.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { MembershipTransportKeyRepository } from './repositories/membership-tran
|
|
|
12
12
|
import { OAuthRepository } from './repositories/oauth';
|
|
13
13
|
import { PasswordPolicyRepository } from './repositories/password-policy';
|
|
14
14
|
import { PrincipalRepository } from './repositories/principals';
|
|
15
|
+
import { SecondFactorRepository } from './repositories/second-factors';
|
|
15
16
|
import { SessionRepository } from './repositories/sessions';
|
|
16
17
|
import { TerminalRepository } from './repositories/terminals';
|
|
17
18
|
import { WorkloadRepository } from './repositories/workloads';
|
|
@@ -27,7 +28,8 @@ export { MembershipTransportKeyRepository, type MembershipTransportKeyRow } from
|
|
|
27
28
|
export { type OAuthCodeRow, type OAuthGrantRow, type OAuthRefreshTokenRow, OAuthRepository } from './repositories/oauth';
|
|
28
29
|
export { PasswordPolicyRepository, type PasswordPolicyRow } from './repositories/password-policy';
|
|
29
30
|
export { PrincipalRepository, type PrincipalRow, type PrincipalStatus, principalStatus } from './repositories/principals';
|
|
30
|
-
export {
|
|
31
|
+
export { SecondFactorRepository, type SecondFactorRow, type SecondFactorStatus } from './repositories/second-factors';
|
|
32
|
+
export { type AuthenticationMethod, SessionRepository, type SessionRow, type SessionTerminal, sessionTerminal, type SignedInSessionRow, } from './repositories/sessions';
|
|
31
33
|
export { DEFAULT_TERMINAL_SESSION_TTL_SECONDS, MAX_TERMINAL_SESSION_TTL_SECONDS, type TerminalAsserter, type TerminalIdentificationRow, type TerminalProfile, TerminalRepository, type TerminalRow, } from './repositories/terminals';
|
|
32
34
|
export { type AppWorkloadRow, type WorkloadPrincipalRow, WorkloadRepository } from './repositories/workloads';
|
|
33
35
|
/** IAM persistence capabilities selected together by a runtime composition root. */
|
|
@@ -48,6 +50,7 @@ export interface IamRepositories {
|
|
|
48
50
|
membershipTransport: MembershipTransportKeyRepository;
|
|
49
51
|
brand: InstallationBrandRepository;
|
|
50
52
|
passwordPolicy: PasswordPolicyRepository;
|
|
53
|
+
secondFactors: SecondFactorRepository;
|
|
51
54
|
}
|
|
52
55
|
/**
|
|
53
56
|
* The portable repository bundle. A composition root may replace one complete capability. `dialect` picks how a
|
package/types/src/env.d.ts
CHANGED
|
@@ -51,6 +51,11 @@ export interface Env {
|
|
|
51
51
|
* `FABRIKA_IAM_AUDIT_RETENTION_DAYS`. A value that is not a whole number of days fails the boot.
|
|
52
52
|
*/
|
|
53
53
|
AUDIT_RETENTION_DAYS?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Hours an OIDC login stays usable after its sign-in; unset means 12. Deploy var `FABRIKA_IAM_OIDC_SESSION_HOURS`.
|
|
56
|
+
* Anything but a whole number of hours from 1 to 720 fails the boot.
|
|
57
|
+
*/
|
|
58
|
+
OIDC_SESSION_HOURS?: string;
|
|
54
59
|
/** `local` / `stage` / `prod`. */
|
|
55
60
|
ENVIRONMENT: string;
|
|
56
61
|
/** Explicit local-only human login bypass. Ignored unless `ENVIRONMENT=local`. */
|
|
@@ -100,6 +105,13 @@ export interface Env {
|
|
|
100
105
|
* `vars`. Rotate by changing the env value.
|
|
101
106
|
*/
|
|
102
107
|
FABRIKA_IAM_PROVISIONING_KEY: string;
|
|
108
|
+
/**
|
|
109
|
+
* **Secret.** Base64 of 32 random bytes: the key that seals the one value IAM must read back, a person's TOTP secret
|
|
110
|
+
* (`src/data-key.ts`). Unset makes TOTP enrolment unavailable; a value that is not 32 bytes fails the boot. Losing it
|
|
111
|
+
* makes every enrolled authenticator unreadable, so those people sign in with a recovery code or an administrator's
|
|
112
|
+
* reset. Never placed in `vars`.
|
|
113
|
+
*/
|
|
114
|
+
FABRIKA_IAM_DATA_KEY?: string;
|
|
103
115
|
/**
|
|
104
116
|
* OIDC provider issuer URL (e.g. `https://accounts.google.com`, an Auth0/Okta/Keycloak/Entra
|
|
105
117
|
* tenant). IAM discovers the endpoints from `${OIDC_ISSUER}/.well-known/openid-configuration`
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How long an OIDC login lasts before IAM sends its holder back to the provider, in hours. The provider is where an
|
|
3
|
+
* organization blocks a person, so this bounds how long a person blocked there keeps fabrika access.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_OIDC_SESSION_HOURS = 12;
|
|
6
|
+
/** The longest cap: the lifetime of every IAM login (`SESSION_TTL_SECONDS`, 30 days). */
|
|
7
|
+
export declare const MAX_OIDC_SESSION_HOURS: number;
|
|
8
|
+
/**
|
|
9
|
+
* Parse the cap into seconds. Unset or empty means the default; anything but a whole number of hours from 1 to 720 is
|
|
10
|
+
* refused, so a typo fails the boot instead of capping at a value nobody chose.
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseOidcSessionSeconds(raw: string | undefined, name?: string): number;
|
|
@@ -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
|
-
|
|
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<
|
|
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<
|
|
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.
|
|
@@ -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;
|
package/types/src/services.d.ts
CHANGED
|
@@ -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;
|
package/types/src/signing.d.ts
CHANGED
|
@@ -11,17 +11,17 @@
|
|
|
11
11
|
*
|
|
12
12
|
* One Signer per isolate, memoised like the `JwtValidator` (jose imports the key once).
|
|
13
13
|
*/
|
|
14
|
-
import { type AccessTokenClaims, type Jwks } from '@fabrika/auth-core';
|
|
14
|
+
import { type AccessTokenClaims, type AccessTokenPayload, type Jwks } from '@fabrika/auth-core';
|
|
15
15
|
import { type JWK, type JWTPayload } from 'jose';
|
|
16
16
|
import type { Env } from './env';
|
|
17
17
|
export declare class Signer {
|
|
18
18
|
private readonly keys;
|
|
19
19
|
private constructor();
|
|
20
20
|
/**
|
|
21
|
-
* Sign
|
|
22
|
-
* picks the matching public JWK. The claims already carry `iss`/`aud`/`exp` (built in core).
|
|
21
|
+
* Sign an access token payload (`accessTokenPayload`) with the ACTIVE key. The `kid` goes in the protected header so
|
|
22
|
+
* the SDK picks the matching public JWK. The claims already carry `iss`/`aud`/`exp` (built in core).
|
|
23
23
|
*/
|
|
24
|
-
sign(
|
|
24
|
+
sign(payload: AccessTokenPayload): Promise<string>;
|
|
25
25
|
/**
|
|
26
26
|
* Sign an OAuth resource access token (`buildResourceAccessClaims`). `typ: at+jwt` (RFC 9068) is what
|
|
27
27
|
* keeps it from passing as a native app token, whose header carries no `typ`.
|
package/types/src/tokens.d.ts
CHANGED
|
@@ -108,6 +108,9 @@ export declare function boundedToken(signed: Promise<string>, who: {
|
|
|
108
108
|
subject: string;
|
|
109
109
|
bytes?: number;
|
|
110
110
|
}): Promise<string | null>;
|
|
111
|
-
/**
|
|
112
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Sign an access token with the isolate's active signing key, in the compact layout only when the entries layout is
|
|
113
|
+
* above `bound` (`signAccessClaims`); `boundedToken` refuses one still above it. Shared by every mint front.
|
|
114
|
+
*/
|
|
115
|
+
export declare function signAccessToken(services: Services, env: MintEnv, params: Omit<Parameters<typeof buildAccessClaims>[0], 'iss'>, bound: number): Promise<string>;
|
|
113
116
|
export {};
|
|
@@ -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>;
|