From c0b28dd4dafe054a0501758ebbb5fc08bed88f99 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 30 Nov 2026 22:10:19 +0100 Subject: [PATCH] fix(auth): honor password registration policy during setup Apply the resolved authentication toggles before creating a password account. Keep setup and invitations available when password sign-in permits them. --- server/src/nest/auth/auth.service.ts | 10 ++++----- server/tests/unit/nest/auth.service.test.ts | 24 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/server/src/nest/auth/auth.service.ts b/server/src/nest/auth/auth.service.ts index 205f3d50b..f5e621f40 100644 --- a/server/src/nest/auth/auth.service.ts +++ b/server/src/nest/auth/auth.service.ts @@ -427,11 +427,11 @@ export class AuthService { if (validInvite.expires_at && new Date(validInvite.expires_at) < new Date()) throw new DomainError(410, 'Invite link has expired'); } - if (userCount > 0 && !validInvite) { - const toggles = await this.resolveAuthToggles(); - if (!toggles.password_registration) { - throw new DomainError(403, 'Password registration is disabled. Contact your administrator.'); - } + const toggles = await this.resolveAuthToggles(); + // Setup and invitations still use the password sign-in policy. + const canUseClosedRegistration = toggles.password_login && (userCount === 0 || validInvite !== null); + if (!toggles.password_registration && !canUseClosedRegistration) { + throw new DomainError(403, 'Password registration is disabled. Contact your administrator.'); } if (!username || !email || !password) { diff --git a/server/tests/unit/nest/auth.service.test.ts b/server/tests/unit/nest/auth.service.test.ts index 236f7ee41..df18ceb7d 100644 --- a/server/tests/unit/nest/auth.service.test.ts +++ b/server/tests/unit/nest/auth.service.test.ts @@ -519,6 +519,30 @@ describe('registerUser — OIDC-only / registration-disabled', () => { expect(result.error).toMatch(/password registration is disabled/i); }); + it.each(['environment', 'legacy setting'])( + 'AUTH-DB-033b: honors the SSO-only registration policy from the %s', + async (source) => { + vi.stubEnv('OIDC_ONLY', source === 'environment' ? 'true' : ''); + try { + if (source === 'legacy setting') await appSetting('oidc_only', 'true'); + await appSetting('oidc_issuer', 'https://sso.example.test'); + await appSetting('oidc_client_id', 'test-client'); + + const credentials = { username: 'signup', email: 'signup@example.test', password: 'Secure123!' }; + expect(await asLegacyResult(svc.registerUser(credentials))).toMatchObject({ status: 403 }); + expect(await countRows(orm, Users, {})).toBe(0); + + const invite = createInviteToken(testDb); + const invited = await asLegacyResult(svc.registerUser({ ...credentials, invite_token: invite.token })); + expect(invited).toMatchObject({ status: 403 }); + expect(await countRows(orm, Users, {})).toBe(1); + expect(await findRow(orm, InviteTokens, { id: invite.id })).toMatchObject({ used_count: 0 }); + } finally { + vi.unstubAllEnvs(); + } + }, + ); + it('AUTH-DB-034: returns 403 when registration is disabled and no invite', async () => { createUser(testDb); // ensure userCount > 0 await appSetting('allow_registration', 'false');