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.
This commit is contained in:
Maurice
2026-11-30 22:10:19 +01:00
parent 4d3f7fd355
commit c0b28dd4da
2 changed files with 29 additions and 5 deletions
+5 -5
View File
@@ -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) {
@@ -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');