Skip to content
Merged
23 changes: 23 additions & 0 deletions .changeset/auth-switches-read-declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/plugin-auth': patch
---

fix(plugin-auth)!: Setup → Authentication's switches show what the deployment runs when nothing is saved

Clause-②: yes (narrowing) — narrowed: with nothing stored, `SettingsService.get('auth', …)` and `GET /api/settings/auth` answer `email_password_enabled`, `signup_enabled` and `require_email_verification` with the deployment's declaration (still `source: 'default'`), where they answered the manifest default; so on a cloud-managed runtime (`OS_CLOUD_MANAGED=1`) whose deployment declares `emailAndPassword.enabled: false`, a save of `email_password_enabled: true` with nothing stored answers `400 SETTINGS_VALIDATION` (`invalid_value` at the key) and saves nothing in the batch, where it saved a row that was not applied; and a save that sets one of these keys to its manifest default where the declaration differs now asks the key's write predicates. Widened: a save that sets one of these keys to the declaration with nothing stored is no change and asks no predicate; on a deployment that declares `emailAndPassword.enabled: false` with nothing stored, the ten `auth` keys visible only while `email_password_enabled !== false` (`signup_enabled`, `require_email_verification`, `password_min_length`, `password_max_length`, `password_reject_breached`, `password_require_complexity`, `password_min_classes`, `password_history_count`, `password_expiry_days`, `mfa_required`) are hidden, so a save of one of them is no longer judged for `required`, options or bounds (for example `password_min_length: 2` is now stored where it was refused `400 SETTINGS_VALIDATION`), as under a stored `email_password_enabled: false`; a save that turns `email_password_enabled` on re-judges them; `AuthManager.resolveDisableSignUp(declared)` (public method). The Setup → Authentication live-status banner no longer states the declared-off, nothing-saved case (the switch says it): its `declaredOffLabel` / `declaredOffText` copy is removed, and the cloud-managed statement is always a `warning`.
Metadata-protocol: none
Skills-docs: none — no skill under `skills/**` teaches the auth settings page or these switches; `content/docs/protocol/kernel/config-resolution.mdx` states the declared default.

<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed, renamed or re-shaped: these are settings values, runtime state, not authorable metadata, and the settings manifest is unchanged. What moves is the value a key with nothing stored resolves to, and the save of it that a write predicate is now asked about; no stored row is read differently, so there is nothing for `objectstack migrate meta` to rewrite and no tombstone. -->

**BREAKING** for a reader of `GET /api/settings/auth` or `SettingsService.get('auth', …)` on a deployment whose own configuration differs from the manifest default for one of these three keys, with nothing saved: the value answered is now what runs (for example `email_password_enabled: { value: false, source: 'default' }` where the deployment declares `emailAndPassword.enabled: false`), not the manifest default. A saved value and an `OS_AUTH_*` override still win, and a clear returns to the declaration.

What each switch reads with nothing saved:

- `email_password_enabled` — `emailAndPassword.enabled`, on unless declared `false`.
- `signup_enabled` — off where self-registration is forced off (SSO-only, cloud-managed) or turned off by `OS_DISABLE_SIGNUP` or a declared `emailAndPassword.disableSignUp: true`; else on.
- `require_email_verification` — the declared `emailAndPassword.requireEmailVerification` under the audience posture in force: on under `email_domain`, on under `open` unless declared `false`, else as declared (off by default).

On a cloud-managed runtime whose deployment declares email and password off, the switch now reads off with nothing saved, and turning it on is refused at the save door (before, the save was stored and not applied, because `true` was the value the settings service already resolved). Self-hosted deployments keep taking a saved value as before.

What to do: nothing to migrate. A client that read `source: 'default'` as "the manifest default" should read it as "nothing is saved; this is the deployment's default".
14 changes: 14 additions & 0 deletions .changeset/settings-declared-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@objectstack/service-settings': patch
---

feat(service-settings): a deployment's declared default answers the bottom rung of the settings cascade

Clause-②: yes (widening) — widened: `SettingsService.registerDeclaredDefault(namespace, key, provider)` and the exported type `SettingsDeclaredDefault`. A key with a registered provider resolves, while nothing is stored and no `OS_*` override is in force, to what the provider answers instead of the manifest `default`, still `source: 'default'`; a key or namespace with no provider resolves exactly as before.
Metadata-protocol: none

A manifest `default` is the console's default. Where a package's runtime takes a key from its own configuration while nothing is stored (plugin options, a host declaration), the settings page read the manifest default while something else ran, and a save of that default was "no change" to the key's write predicates.

A package now states that value: `settings.registerDeclaredDefault('auth', 'email_password_enabled', () => declared)`. It is asked on each use and never recorded, and it answers every place the cascade read the manifest default: `get`, `getMany`, `getNamespace` (and `GET /api/settings/:namespace`), the change test that decides whether a write predicate is asked, the value a cleared key falls back to, and the type an `OS_*` value is coerced to. `undefined` or `null` means "nothing declared" (the manifest default stands); a provider that throws is reported once at `warn` and the manifest default answers. A stored row and an `OS_*` override still win. ADR-0007's rung 4 is amended to match.

What to do: nothing for existing namespaces.
17 changes: 14 additions & 3 deletions content/docs/protocol/kernel/config-resolution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,21 @@ ObjectStack replaces the chain with one resolver and one declared order.
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ 5. DEFAULT the specifier's `default` in the manifest │
│ 5. DEFAULT what the deployment declares for the key, else │
│ the specifier's `default` in the manifest │
└─────────────────────────────────────────────────────────────┘
```

The bottom rung answers what the key takes in the running system while
nothing is stored. A package whose runtime takes a key from its own
configuration when nothing is stored (plugin options, a host declaration)
states that value with `registerDeclaredDefault(namespace, key, provider)`;
otherwise the manifest `default` answers. Either way the answer reports
`source: 'default'`: nothing is stored, and a clear returns to it. The auth
plugin states `email_password_enabled`, `signup_enabled` and
`require_email_verification` this way, so Setup → Authentication shows what
the deployment runs, not a console default it never applies.

Two rules follow from the walk, and both are enforced in `SettingsService`:

- **First non-null entry wins.** Values are *not* merged — no deep merge, no
Expand Down Expand Up @@ -251,8 +262,8 @@ OS_CRM_API_KEY=sk_live_...
OS_AI_LOG_PROMPTS=true
```

The raw string is coerced by the **type of the specifier's default**
(`coerceEnvValue`):
The raw string is coerced by the **type of the key's default** — the bottom
rung above (`coerceEnvValue`):

```bash
OS_HTTP_TIMEOUT=30000 # default is a number → 30000
Expand Down
3 changes: 2 additions & 1 deletion content/docs/references/system/settings-manifest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Resolution order (handled by `SettingsService.get`):
1. process.env override (source='env', locked=true)
2. sys_setting scope=tenant
3. sys_setting scope=user
4. manifest specifier.default
4. what the running deployment declares for the key, else the manifest
specifier.default (source='default'; ADR-0007 rung 4 as amended)

<Callout type="info">
**Source:** `packages/spec/src/system/settings-manifest.zod.ts`
Expand Down
6 changes: 4 additions & 2 deletions docs/adr/0007-settings-manifest-and-kv-store.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ADR-0007: Settings — Manifest + K/V Store + Resolver (Apple-style)

**Status**: Accepted — backend implemented; UI pending (objectui) (proposed 2026-05-20 · calibrated 2026-06-12) · **Amended (2026-11-05, #30385)** — D6: a clear (`null`) of an env-pinned key is not refused; it clears the stored value beneath the override and changes nothing in force. · **Amended (2026-11-08, #30208)** — §Specifier set gains `map` (keyed rows with typed value columns); `json` stays for raw config and is no longer the home for maps.
**Status**: Accepted — backend implemented; UI pending (objectui) (proposed 2026-05-20 · calibrated 2026-06-12) · **Amended (2026-11-05, #30385)** — D6: a clear (`null`) of an env-pinned key is not refused; it clears the stored value beneath the override and changes nothing in force. · **Amended (2026-11-08, #30208)** — §Specifier set gains `map` (keyed rows with typed value columns); `json` stays for raw config and is no longer the home for maps. · **Amended (2026-11-28, #38098)** — §Resolution rung 4: what the running deployment declares for a key (`registerDeclaredDefault`), else the manifest `specifier.default`; still `source='default'`.
**Deciders**: ObjectStack Protocol Architects
**Builds on**: [ADR-0005](./0005-metadata-customization-overlay.md) (Metadata Customization Overlay), [ADR-0006 v4](./0006-project-environment-split.v4.md) (Project/Environment Split — this record was originally built on v3, superseded by v4)
**Consumers**: `@objectstack/spec`, `@objectstack/platform-objects`, new `@objectstack/service-settings`, `@objectstack/plugin-auth` (Setup app), `objectui` (Settings renderer)
Expand Down Expand Up @@ -77,7 +77,7 @@ ObjectStack's protocol-first DNA matches Apple's: a small set of declarative spe
1. process.env override (source='env', locked=true) ← highest
2. sys_setting WHERE scope='tenant' ← tenant
3. sys_setting WHERE scope='user' AND user_id=ctx.user_id ← user
4. manifest specifier.default ← default
4. deployment-declared default, else manifest specifier.default ← default
returns: { value, source, locked }

RUNTIME WRITE SettingsService.set(ns, key, value, scope, ctx)
Expand All @@ -91,6 +91,8 @@ ObjectStack's protocol-first DNA matches Apple's: a small set of declarative spe
POST /api/settings/:namespace/test → { ok, message, details } (optional, manifest-declared)
```

*Amended 2026-11-28 (#38098): rung 4 read "manifest specifier.default". A manifest default is the console's default; where a package's runtime takes a key from its own configuration while nothing is stored (plugin options, a host declaration), the manifest default is a value nothing applies, so the page read one thing while another ran, and a save of it was "no change" to the key's write predicates. The package now states that value (`SettingsService.registerDeclaredDefault(namespace, key, provider)`, asked on each use, never recorded); rung 4 answers it, else the manifest default. The answer stays `source='default'` (the published enum admits no other value, nothing is stored, and a clear returns to it); a stored row and an `OS_*` override still win.*

### Design principles (binding)

1. **One manifest, many namespaces.** Each plugin exports any number of `SettingsManifest`s. A manifest is a declarative tree of `Specifier`s — never a React component, never a SQL DDL.
Expand Down
3 changes: 2 additions & 1 deletion packages/platform-objects/src/system/sys-setting.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { ObjectSchema, Field } from '@objectstack/spec/data';
* 2. sys_platform_setting (source='global')
* 3. sys_setting WHERE scope='tenant' (source='tenant')
* 4. sys_setting WHERE scope='user' (source='user')
* 5. manifest specifier.default (source='default')
* 5. deployment-declared default, else
* manifest specifier.default (source='default')
*
* Encryption: rows with `encrypted=true` store ciphertext in `value_enc`
* and leave `value` null. The plain value is never written to audit log
Expand Down
15 changes: 13 additions & 2 deletions packages/plugins/plugin-auth/src/auth-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5863,6 +5863,18 @@ export class AuthManager {
return this.resolveSsoOnly() || isCloudManaged();
}

/**
* Whether self-registration runs off for a given declared
* `emailAndPassword.disableSignUp`: forced off ({@link signUpForcedOff}),
* else the deployment env (`readDisableSignUpEnv`), else the declaration,
* else on. The one formula `getPublicConfig()` answers with, and the one the
* auth plugin states as `auth.signup_enabled`'s declared default (the value
* the switch shows while nothing is stored), so the two cannot disagree.
*/
public resolveDisableSignUp(declared: boolean | undefined): boolean {
return this.signUpForcedOff() ? true : (readDisableSignUpEnv() ?? declared ?? false);
}

/**
* The tenancy posture ACTUALLY IN FORCE on this deployment — the one fact
* both `organization/create`'s gate and `/auth/config`'s
Expand Down Expand Up @@ -6053,7 +6065,6 @@ export class AuthManager {
// lock registration policy; otherwise we fall back to configured/settings
// `emailAndPassword.disableSignUp` (default `false`).
const emailPasswordConfig: Partial<EmailAndPasswordConfig> = this.config.emailAndPassword ?? {};
const disableSignUpFromEnv = readDisableSignUpEnv();
// SSO-only ("enforced") hides the local password form + self-registration.
// `enabled` stays true (break-glass), but signup is forced off and the UI
// suppresses the password form via `features.ssoEnforced` below. A
Expand All @@ -6070,7 +6081,7 @@ export class AuthManager {
const audience = this.getAudience();
const emailPassword = {
enabled: emailPasswordConfig.enabled !== false, // Default to true
disableSignUp: this.signUpForcedOff() ? true : (disableSignUpFromEnv ?? emailPasswordConfig.disableSignUp ?? false),
disableSignUp: this.resolveDisableSignUp(emailPasswordConfig.disableSignUp),
// Mirrors the wiring in createAuthInstance() through the SAME resolver:
// a self-registration-permitting posture forces verification ON, except
// an `open` deployment's explicit opt-out (#20389) — the advertised flag
Expand Down
34 changes: 26 additions & 8 deletions packages/plugins/plugin-auth/src/auth-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ import {
type SignInMethodFloorDeps,
} from './sign-in-method-floor.js';
import { describeEmailPasswordLiveStatus, type AuthLiveStatus } from './auth-settings-live-status.js';
import { registerAuthDeclaredDefaults } from './auth-settings-declared-defaults.js';
import { judgePlatformAdmin, judgeHeldSession, isPlatformAdminUser, type PlatformAdminActor } from './platform-admin-gate.js';
import {
decideDemoStaffEnvironmentGate,
Expand Down Expand Up @@ -265,6 +266,12 @@ interface SettingsReadSurface {
namespace: string,
provider: (input: { ctx: Record<string, unknown>; locale?: string }) => Promise<AuthLiveStatus | undefined>,
): void;
/**
* State what a key takes in the running system while nothing is stored
* (`SettingsService.registerDeclaredDefault`). Optional, like `subscribe`.
* See auth-settings-declared-defaults.ts.
*/
registerDeclaredDefault?(namespace: string, key: string, provider: () => unknown): void;
}

/** [#23367] The auth setting that records when the API-key lifecycle policy took effect. */
Expand Down Expand Up @@ -2502,8 +2509,10 @@ export class AuthPlugin implements Plugin {
* Bind the small open-source auth settings namespace to better-auth config.
*
* Only explicit settings values (stored or OS_AUTH_* env overrides) affect
* runtime config. Manifest defaults are UI defaults and do not mask code or
* deployment configuration.
* runtime config. A `source: 'default'` value is never applied: it is the
* manifest's UI default, or, for the keys in auth-settings-declared-defaults.ts,
* the deployment's own declaration read back, which the running config
* already holds.
*
* Call through {@link ensureAuthSettingsBound}, never directly.
*/
Expand All @@ -2522,6 +2531,16 @@ export class AuthPlugin implements Plugin {
// value is stored; the pass below would only apply it. See the module.
registerSignInMethodFloor(settings, this.signInMethodFloorDeps(ctx, this.authManager), ctx.logger);

// With nothing stored, the switches read what the deployment declares (the
// value the running config takes), not the manifest's console default.
// See auth-settings-declared-defaults.ts.
const declaringManager = this.authManager;
registerAuthDeclaredDefaults(settings, {
declared: () => this.declaredAuthOptions,
resolveDisableSignUp: (declared) => declaringManager.resolveDisableSignUp(declared),
audiencePosture: () => declaringManager.getAudience().posture,
}, ctx.logger);

const applySettings = async (): Promise<void> => {
if (!this.authManager) return;
const authManager = this.authManager;
Expand Down Expand Up @@ -2600,8 +2619,7 @@ export class AuthPlugin implements Plugin {
// off keeps it off: the host refuses every password door there, so a
// stored value that reads on would only advertise a form that signs
// nobody in. The save door refuses turning it on (sign-in-method-floor.ts);
// a row stored before that check, or a re-save of the manifest default
// the save door is never asked about, is not applied. An
// a row stored before that check is not applied. An
// `OS_AUTH_EMAIL_PASSWORD_ENABLED` value is the deployment's own
// declaration and still applies.
let emailPasswordHeldOff = false;
Expand Down Expand Up @@ -3175,8 +3193,8 @@ export class AuthPlugin implements Plugin {
const runPass = (): Promise<void> => (pass = applySettings());

// Setup → Authentication states what runs when the switch says otherwise:
// with nothing stored the switch shows the manifest default while the
// running config takes the deployment's declaration. See auth-settings-live-status.ts.
// a value stored on that a cloud-managed runtime does not apply, or any
// other disagreement. See auth-settings-live-status.ts.
if (typeof settings.registerLiveStatus === 'function') {
const floorDeps = this.signInMethodFloorDeps(ctx, this.authManager);
try {
Expand All @@ -3194,8 +3212,8 @@ export class AuthPlugin implements Plugin {
} catch (err: any) {
ctx.logger.warn(
'[auth] Setup → Authentication: the live status was NOT registered '
+ `(${String(err?.message ?? err)}), so where the deployment declares email and password sign-in off `
+ 'with nothing saved, the page shows the switch on with nothing saying it is off.',
+ `(${String(err?.message ?? err)}), so where a saved email and password value is not what runs `
+ '(a cloud-managed environment holding it off), the page shows the switch on with nothing saying it is off.',
);
}
}
Expand Down
Loading
Loading