Skip to content

Commit 5cc78ec

Browse files
committed
fix(plugin-auth,service-settings): judge the sign-in floor on the whole save, and count a provider only with its credential pair
A write predicate is now handed the whole write as batch, so the auth sign-in floor judges the state after the save: email and password off together with Google off is refused at both keys, and Google filled in within the same save is accepted. A social or OIDC provider counts only with a client ID and secret. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CXhZcHuALPAjkDCiiT
1 parent 249ec4c commit 5cc78ec

7 files changed

Lines changed: 316 additions & 74 deletions

File tree

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
---
22
'@objectstack/plugin-auth': patch
3+
'@objectstack/service-settings': patch
34
---
45

56
fix(plugin-auth)!: saving the auth settings refuses to turn off the last way to sign in (#30202)
67

7-
Clause-②: no (narrowing) — a save an administrator could make before is now refused at the settings save door with the existing `SETTINGS_VALIDATION` / `invalid_value` field error: email and password off while nothing else signs anyone in, and, while email and password is already off, the Google change that removes the last provider. No new error code, export or settings key. Nothing is widened.
8+
Clause-②: yes (narrowing) — a save an administrator could make before is now refused at the settings save door with the existing `SETTINGS_VALIDATION` / `invalid_value` field error: a save after which email and password is off and nothing else signs anyone in, judged on the whole save, including the Google change that removes the last provider while email and password is off. No new error code or settings key. The widening half: `SettingsWritePredicateInput`, exported from `@objectstack/service-settings`, gains an optional `batch` field, the whole write a predicate is asked about.
89
Metadata-protocol: none
9-
Skills-docs: none — no file under `skills/**` teaches `auth.email_password_enabled`; `content/docs/deployment/environment-variables.mdx` already names the `OS_AUTH_EMAIL_PASSWORD_ENABLED` override used for recovery below.
10+
Skills-docs: none — no file under `skills/**` teaches `auth.email_password_enabled` or the settings write predicate; `content/docs/deployment/environment-variables.mdx` already names the `OS_AUTH_EMAIL_PASSWORD_ENABLED` override used for recovery below.
1011

11-
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped: auth.email_password_enabled and the auth.google_* keys keep their declarations, and the save door now refuses only the change that leaves no way to sign in. A value stored before is not rewritten (the existing OS_AUTH_EMAIL_PASSWORD_ENABLED override recovers it), so there is nothing at rest for a migration to rewrite. -->
12+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped: auth.email_password_enabled and the auth.google_* keys keep their declarations, and the save door now refuses only the save that leaves no way to sign in. The predicate input's new batch field is optional and additive. A value stored before is not rewritten (the existing OS_AUTH_EMAIL_PASSWORD_ENABLED override recovers it), so there is nothing at rest for a migration to rewrite. -->
1213

13-
**BREAKING** accept-set narrowing at the auth settings save door (`PUT /api/settings/auth`, `SettingsService.setMany('auth', …)`): a change that would leave no way to sign in is refused, where it was saved before.
14+
**BREAKING** accept-set narrowing at the auth settings save door (`PUT /api/settings/auth`, `SettingsService.setMany('auth', …)`): a save that would leave no way to sign in is refused, where it was saved before.
1415

1516
- **Before.** Setup → Settings → Authentication, "Enable email/password login" off, save: "Settings saved", with no social provider and SSO off. After the next sign-out nobody could sign in, the administrator included.
16-
- **Now.** `PUT /api/settings/auth` with `email_password_enabled: false` answers `400 SETTINGS_VALIDATION`, with a field error at `email_password_enabled` (`code: invalid_value`, `constraint.lastSignInMethod: email_password`) whose message, in the caller's language (en, zh-CN, ja-JP, es-ES), says to set up and save another sign-in method first. Nothing in that batch is written, and email and password stays on.
17-
- **What counts as another way in.** A social or OIDC provider in force; enterprise SSO switched on with at least one identity provider registered (the switch alone does not count); phone-number sign-in; a host sign-in handoff the host declared. `ssoOnlyMode` alone, magic link and passkeys do not count.
17+
- **Now.** A save after which email and password is off and no other way in is in force answers `400 SETTINGS_VALIDATION`. The field error sits at `email_password_enabled` (`code: invalid_value`, `constraint.lastSignInMethod: email_password`), and its message, in the caller's language (en, zh-CN, ja-JP, es-ES), says to keep or set up another sign-in method before or in that save. Nothing in that batch is written, and email and password stays on.
18+
- **The whole save is judged.** `{ email_password_enabled: false, google_enabled: false }` (or with `google_client_secret` cleared) on a deployment signing in with email and password plus Google is refused, with a field error at each of the two keys. Google filled in (switch, client ID and secret) in the same save that turns email and password off is accepted, and so is the switch alone over a client ID and secret already stored.
19+
- **What counts as another way in.** A social or OIDC provider in force whose client ID and client secret are both set; enterprise SSO switched on with at least one identity provider registered (the switch alone does not count); phone-number sign-in; a host sign-in handoff the host declared. `ssoOnlyMode` alone, magic link, passkeys, and a provider listed without its credentials do not count.
1820
- **The other direction.** While email and password is off, `google_enabled: false`, or clearing `google_client_id` / `google_client_secret` when no environment variable or stack config supplies it, is refused (`constraint.lastSignInMethod: google`) if Google is the last way in.
1921
- **Already locked out.** Set `OS_AUTH_EMAIL_PASSWORD_ENABLED=true` and restart: the environment override wins over the stored `false`, and email and password sign-in comes back. Then set up another method before turning it off again.
22+
- **For a write predicate's author (`@objectstack/service-settings`).** `SettingsWritePredicateInput.batch` holds every key the write sets in the namespace, the asked key among them, with the value it would store (`null` clears). `setMany` always passes it, frozen. A rule over several keys judges the state after the whole write from it instead of one key against the rest as they stand. Existing predicates need no change.

‎packages/plugins/plugin-auth/src/auth-plugin.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ import {
108108
reportIfNoSignInAccountExists,
109109
type SignInPathConfigView,
110110
} from './boot-sign-in-reachability.js';
111-
import { registerSignInMethodFloor } from './sign-in-method-floor.js';
111+
import { providerCredentialIndex, registerSignInMethodFloor } from './sign-in-method-floor.js';
112112
import { judgePlatformAdmin, isPlatformAdminUser, type PlatformAdminActor } from './platform-admin-gate.js';
113113
import {
114114
decideDemoStaffEnvironmentGate,
@@ -532,6 +532,8 @@ export class AuthPlugin implements Plugin {
532532
private allowListUndeterminedWarned = false;
533533
private allowListUntrustedHeaderWarned = false;
534534
private configuredSocialProviders: SocialProviderConfig | undefined;
535+
/** Whether each provider declared at boot holds a credential pair (the sign-in floor's count). */
536+
private bootProviderCredentials: ReadonlyMap<string, boolean> = new Map();
535537
// ADR-0092 D6 — the EFFECTIVE better-auth secondaryStorage (host-supplied or
536538
// the kernel-cache adapter wired in init). The identity write guard's
537539
// session-snapshot refresh reads through this; undefined = refresh no-ops.
@@ -729,6 +731,7 @@ export class AuthPlugin implements Plugin {
729731
this.configuredSocialProviders = authConfig.socialProviders
730732
? { ...authConfig.socialProviders }
731733
: undefined;
734+
this.bootProviderCredentials = providerCredentialIndex(authConfig.socialProviders, authConfig.oidcProviders);
732735

733736
// [ADR-0105 D8] Lazy handle to plugin-security's `invitation-placement`
734737
// service. LAZY on purpose: plugin-security starts after plugin-auth, so
@@ -2064,6 +2067,9 @@ export class AuthPlugin implements Plugin {
20642067
},
20652068
hostSignInHandoff: this.options.hostSignInHandoff === true,
20662069
stackConfiguredGoogle: () => !!this.configuredSocialProviders?.google,
2070+
// Not declared at boot = the Google the settings added, which the apply
2071+
// adds only with a client ID and secret.
2072+
providerHasCredentials: ({ id, type }) => this.bootProviderCredentials.get(`${type}:${id}`) ?? true,
20672073
env: () => ((globalThis as any)?.process?.env ?? {}) as Record<string, string | undefined>,
20682074
},
20692075
ctx.logger,

‎packages/plugins/plugin-auth/src/sign-in-method-floor.test.ts‎

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
* provider is refused, and the same change is accepted where something
2323
* else keeps a way in;
2424
* 5. recovery: a stored `false` from before this check is overridden by
25-
* `OS_AUTH_EMAIL_PASSWORD_ENABLED=true` (control: without it, it rules).
25+
* `OS_AUTH_EMAIL_PASSWORD_ENABLED=true` (control: without it, it rules);
26+
* 6. the whole save is judged, not each key against the rest as they stand:
27+
* email and password off together with Google off (or its secret cleared)
28+
* is refused at both keys, and Google filled in, or switched on over a
29+
* stored credential pair, in the same save as email and password off is
30+
* accepted;
31+
* 7. a provider listed without a credential pair is no way in.
2632
*/
2733

2834
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -39,7 +45,7 @@ import {
3945
import { AuthPlugin } from './auth-plugin.js';
4046
import type { AuthManager } from './auth-manager.js';
4147
import { SSO_PROVIDER_OBJECT } from './sso-client-secret.js';
42-
import { SIGN_IN_FLOOR_COPY } from './sign-in-method-floor.js';
48+
import { SIGN_IN_FLOOR_COPY, providerCredentialIndex } from './sign-in-method-floor.js';
4349

4450
const SECRET = 'test-secret-at-least-32-chars-long';
4551

@@ -316,6 +322,85 @@ describe('the auth settings save keeps a way to sign in', () => {
316322
expect(googleOn(alone.manager)).toBe(true);
317323
});
318324

325+
// ── 6. The whole save, not one key at a time ──────────────────────────────
326+
327+
it('refuses email and password off together with Google off, or its secret cleared, at both keys', async () => {
328+
const { settings, manager } = await boot();
329+
await saveGoogle(settings, manager);
330+
331+
for (const [googleKey, value] of [['google_enabled', false], ['google_client_secret', null]] as const) {
332+
const refusal = await refusalOf(settings.setMany('auth', { email_password_enabled: false, [googleKey]: value }));
333+
expect(refusal.fields).toEqual(
334+
expect.arrayContaining([
335+
expect.objectContaining({ field: 'email_password_enabled', constraint: { lastSignInMethod: 'email_password' } }),
336+
expect.objectContaining({ field: googleKey, constraint: { lastSignInMethod: 'google' } }),
337+
]),
338+
);
339+
expect(refusal.fields).toHaveLength(2);
340+
}
341+
expect(await settings.get('auth', 'email_password_enabled')).toMatchObject({ value: true, source: 'default' });
342+
expect(await settings.get('auth', 'google_enabled')).toMatchObject({ value: true });
343+
expect(await settings.get('auth', 'google_client_secret')).toMatchObject({ value: GOOGLE.google_client_secret });
344+
expect(passwordOn(manager)).toBe(true);
345+
expect(googleOn(manager)).toBe(true);
346+
});
347+
348+
it('accepts Google filled in, in the same save that turns email and password off', async () => {
349+
const { settings, manager } = await boot();
350+
351+
await settings.setMany('auth', { ...GOOGLE, email_password_enabled: false });
352+
await vi.waitFor(() => expect(passwordOn(manager)).toBe(false));
353+
expect(googleOn(manager)).toBe(true);
354+
});
355+
356+
it('accepts Google switched on over a stored credential pair, in the same save', async () => {
357+
const { settings, manager } = await boot();
358+
await settings.setMany('auth', { ...GOOGLE, google_enabled: false });
359+
await vi.waitFor(() => expect(googleOn(manager)).toBe(false));
360+
361+
// Only the switch rides in this save: the pair is read as stored.
362+
await settings.setMany('auth', { google_enabled: true, email_password_enabled: false });
363+
await vi.waitFor(() => expect(passwordOn(manager)).toBe(false));
364+
expect(googleOn(manager)).toBe(true);
365+
});
366+
367+
it('refuses Google switched on with half a credential pair, in the same save', async () => {
368+
const { settings, manager } = await boot();
369+
const refusal = await refusalOf(
370+
settings.setMany('auth', { google_enabled: true, google_client_id: 'only-the-id', email_password_enabled: false }),
371+
);
372+
expect(refusal.fields).toEqual([
373+
expect.objectContaining({ field: 'email_password_enabled', constraint: { lastSignInMethod: 'email_password' } }),
374+
]);
375+
expect(passwordOn(manager)).toBe(true);
376+
});
377+
378+
// ── 7. A provider without a credential pair ──────────────────────────────
379+
380+
it('a stack-config provider listed without a credential pair is no way in', async () => {
381+
const { settings, manager } = await boot({
382+
pluginOptions: { socialProviders: { github: { clientId: 'gh-id', clientSecret: ' ' } } },
383+
});
384+
expect(manager.getPublicConfig().socialProviders.map((p: { id: string }) => p.id)).toContain('github');
385+
386+
await refusalOf(settings.setMany('auth', { email_password_enabled: false }));
387+
expect(passwordOn(manager)).toBe(true);
388+
});
389+
390+
it('indexes each boot provider, social and OIDC, by whether it holds both credentials', () => {
391+
const index = providerCredentialIndex(
392+
{ github: { clientId: 'id', clientSecret: 'secret' }, gitlab: { clientId: 'id', clientSecret: '' } },
393+
[{ providerId: 'okta', clientId: 'id', clientSecret: 'secret' }, { providerId: 'azure', clientId: '', clientSecret: 's' }],
394+
);
395+
expect(Object.fromEntries(index)).toEqual({
396+
'social:github': true,
397+
'social:gitlab': false,
398+
'oidc:okta': true,
399+
'oidc:azure': false,
400+
});
401+
expect(providerCredentialIndex(undefined, undefined).size).toBe(0);
402+
});
403+
319404
// ── 5. Recovery for a deployment that already stored the dead end ─────────
320405

321406
it('OS_AUTH_EMAIL_PASSWORD_ENABLED=true overrides a stored false (control: without it, the stored false rules)', async () => {

0 commit comments

Comments
 (0)