Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(auth): preserve a usable sign-in method (#263)

I'm Groot 🌳8e2adba2b5f6
diff --git a/apps/api/src/modules/god/__tests__/integration/god-oidc.test.ts b/apps/api/src/modules/god/__tests__/integration/god-oidc.test.ts
index 167b5886..be5535ad 100644
--- a/apps/api/src/modules/god/__tests__/integration/god-oidc.test.ts
+++ b/apps/api/src/modules/god/__tests__/integration/god-oidc.test.ts
@@ -21,6 +21,11 @@ const credentials = {
clientSecret: 'sh-secret',
};
+const googleCredentials = {
+ clientId: 'google-client',
+ clientSecret: 'google-secret',
+};
+
describe('god OIDC and password settings', () => {
beforeEach(resetDb);
@@ -165,5 +170,33 @@ describe('god OIDC and password settings', () => {
const config = await god.api['auth-config'].get();
expect(config.data).toMatchObject({ oidc: false, oidcLabel: '', emailPassword: true });
});
+
+ it('refuses to disable the only usable OIDC provider', async () => {
+ const { god } = await setup();
+ await god.api.god['oidc-settings'].put({ ...credentials, enabled: true });
+ await god.api.god['auth-settings'].put({ emailPassword: false });
+
+ const res = await god.api.god['oidc-settings'].put({ enabled: false });
+
+ expect(res.status).toBe(400);
+ expect(res.error!.value).toMatchObject({
+ error: 'Enable password sign-in or another single sign-on provider first',
+ });
+ expect((await god.api.god['oidc-settings'].get()).data).toMatchObject({ enabled: true });
+ });
+
+ it('refuses to disable the only usable Google provider', async () => {
+ const { god } = await setup();
+ await god.api.god['google-settings'].put({ ...googleCredentials, enabled: true });
+ await god.api.god['auth-settings'].put({ emailPassword: false });
+
+ const res = await god.api.god['google-settings'].put({ enabled: false });
+
+ expect(res.status).toBe(400);
+ expect(res.error!.value).toMatchObject({
+ error: 'Enable password sign-in or another single sign-on provider first',
+ });
+ expect((await god.api.god['google-settings'].get()).data).toMatchObject({ enabled: true });
+ });
});
});
diff --git a/apps/api/src/modules/god/index.ts b/apps/api/src/modules/god/index.ts
index 7acf701d..83c7c090 100644
--- a/apps/api/src/modules/god/index.ts
+++ b/apps/api/src/modules/god/index.ts
@@ -95,6 +95,16 @@ async function hasSsoProvider(): Promise<boolean> {
return (await hasConfiguredOidc()) || (await hasConfiguredGoogle());
}
+async function assertUsableSignInMethod(
+ nextProviderUsable: boolean,
+ otherProviderUsable: boolean,
+): Promise<void> {
+ const auth = await getAuthSettings();
+ if (!auth.emailPassword && !nextProviderUsable && !otherProviderUsable) {
+ throw new HttpError(400, 'Enable password sign-in or another single sign-on provider first');
+ }
+}
+
export const godRoutes = new Elysia({ name: 'god', detail: { tags: ['God'] } })
.use(authContext)
// Every route in this plugin is instance administration, so the role check runs
@@ -180,13 +190,18 @@ export const godRoutes = new Elysia({ name: 'god', detail: { tags: ['God'] } })
'/god/google-settings',
async ({ body }) => {
const current = await getGoogleSettings();
+ const enabled = body.enabled ?? current.enabled;
const clientId = body.clientId ?? current.clientId;
const hasClientSecret = (body.clientSecret?.length ?? 0) > 0 || current.hasClientSecret;
// Turning it on without credentials would only offer a button that fails at
// Google, so the same rule as the mail-dependent options applies here.
- if (body.enabled && (clientId.length === 0 || !hasClientSecret)) {
+ if (enabled && (clientId.length === 0 || !hasClientSecret)) {
throw new HttpError(400, 'Add the Google client ID and secret first');
}
+ await assertUsableSignInMethod(
+ enabled && clientId.length > 0 && hasClientSecret,
+ await hasConfiguredOidc(),
+ );
const next = await setGoogleSettings(body);
return { ...next, redirectUri: GOOGLE_REDIRECT_URI };
},
@@ -216,17 +231,19 @@ export const godRoutes = new Elysia({ name: 'god', detail: { tags: ['God'] } })
'/god/oidc-settings',
async ({ body }) => {
const current = await getOidcSettings();
+ const enabled = body.enabled ?? current.enabled;
const discoveryUrl = body.discoveryUrl ?? current.discoveryUrl;
const clientId = body.clientId ?? current.clientId;
const hasClientSecret = (body.clientSecret?.length ?? 0) > 0 || current.hasClientSecret;
// Turning it on without credentials would only offer a button that fails at
// the provider, the same rule the Google settings apply.
- if (
- body.enabled &&
- (discoveryUrl.length === 0 || clientId.length === 0 || !hasClientSecret)
- ) {
+ if (enabled && (discoveryUrl.length === 0 || clientId.length === 0 || !hasClientSecret)) {
throw new HttpError(400, 'Add the discovery URL, client ID and secret first');
}
+ await assertUsableSignInMethod(
+ enabled && discoveryUrl.length > 0 && clientId.length > 0 && hasClientSecret,
+ await hasConfiguredGoogle(),
+ );
const next = await setOidcSettings(body);
return { ...next, redirectUri: OIDC_REDIRECT_URI };
},