Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat: custom logout url for automatic logout from idp

tiran133e6cae2a0c803
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 9f1794dbb..ea5ea8071 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -2,7 +2,7 @@ import { dirname } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { serve } from "@hono/node-server";
import type { Session, User } from "better-auth/types";
-import { eq } from "drizzle-orm";
+import { and, eq } from "drizzle-orm";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { Hono } from "hono";
import { cors } from "hono/cors";
@@ -431,6 +431,40 @@ export function createApp() {
return next();
});
+ const oauthIdTokenApi = api.get(
+ "/oauth/id-token",
+ describeRoute({
+ operationId: "getOAuthIdToken",
+ tags: ["Authentication"],
+ description:
+ "Get the id_token for the current user's custom OAuth account",
+ responses: {
+ 200: {
+ description: "The id_token if available",
+ content: {
+ "application/json": {
+ schema: resolver(v.object({ idToken: v.nullable(v.string()) })),
+ },
+ },
+ },
+ },
+ }),
+ async (c) => {
+ const userId = c.get("userId");
+ const [account] = await db
+ .select({ idToken: schema.accountTable.idToken })
+ .from(schema.accountTable)
+ .where(
+ and(
+ eq(schema.accountTable.userId, userId),
+ eq(schema.accountTable.providerId, "custom"),
+ ),
+ )
+ .limit(1);
+ return c.json({ idToken: account?.idToken ?? null });
+ },
+ );
+
const projectApi = api.route("/project", project);
const taskApi = api.route("/task", task);
const columnApi = api.route("/column", column);
@@ -497,6 +531,7 @@ export function createApp() {
timeEntryApi,
workflowRuleApi,
workspaceApi,
+ oauthIdTokenApi,
};
}
@@ -573,6 +608,7 @@ const {
timeEntryApi,
workflowRuleApi,
workspaceApi,
+ oauthIdTokenApi,
} = createdApp;
const isMainModule =
@@ -607,6 +643,7 @@ export type AppType =
| typeof invitationApi
| typeof workspaceApi
| typeof publicProjectApi
- | typeof invitationPublicApi;
+ | typeof invitationPublicApi
+ | typeof oauthIdTokenApi;
export default app;
diff --git a/apps/api/src/schemas.ts b/apps/api/src/schemas.ts
index ccec52f74..a46f5c81a 100644
--- a/apps/api/src/schemas.ts
+++ b/apps/api/src/schemas.ts
@@ -253,4 +253,5 @@ export const configSchema = v.object({
hasDiscordSignIn: v.nullable(v.boolean()),
hasCustomOAuth: v.nullable(v.boolean()),
hasGuestAccess: v.nullable(v.boolean()),
+ customOAuthLogoutUrl: v.nullable(v.string()),
});
diff --git a/apps/api/src/utils/get-settings.ts b/apps/api/src/utils/get-settings.ts
index 9f5e845a5..a0f20863d 100644
--- a/apps/api/src/utils/get-settings.ts
+++ b/apps/api/src/utils/get-settings.ts
@@ -27,6 +27,7 @@ function getSettings() {
Boolean(process.env.CUSTOM_OAUTH_CLIENT_ID) &&
Boolean(process.env.CUSTOM_OAUTH_CLIENT_SECRET),
hasGuestAccess: process.env.DISABLE_GUEST_ACCESS !== "true",
+ customOAuthLogoutUrl: process.env.CUSTOM_OAUTH_LOGOUT_URL || null,
};
}
diff --git a/apps/docs/core/social-providers/custom-oauth.mdx b/apps/docs/core/social-providers/custom-oauth.mdx
index 54a45e38b..bb3286996 100644
--- a/apps/docs/core/social-providers/custom-oauth.mdx
+++ b/apps/docs/core/social-providers/custom-oauth.mdx
@@ -29,6 +29,7 @@ To configure a custom OAuth/OIDC provider, you need to set the following environ
| `CUSTOM_OAUTH_SCOPES` | Comma-separated list of OAuth scopes | `profile,email` |
| `CUSTOM_OAUTH_RESPONSE_TYPE` | OAuth response type | `code` |
| `CUSTOM_AUTH_PKCE` | Enable/disable PKCE (Proof Key for Code Exchange) | `true` |
+| `CUSTOM_OAUTH_LOGOUT_URL` | Logout endpoint URL (optional) | `https://id.example.com/api/oidc/logout` |
## Setup Steps
@@ -54,6 +55,7 @@ CUSTOM_OAUTH_CLIENT_SECRET=your-client-secret
CUSTOM_OAUTH_AUTHORIZATION_URL=https://your-idp.com/authorize
CUSTOM_OAUTH_TOKEN_URL=https://your-idp.com/oauth/token
CUSTOM_OAUTH_USER_INFO_URL=https://your-idp.com/oauth/userinfo
+CUSTOM_OAUTH_LOGOUT_URL=https://your-idp.com/oauth/logout
# Optional: Use discovery URL for automatic configuration
CUSTOM_OAUTH_DISCOVERY_URL=https://your-idp.com/.well-known/openid-configuration
@@ -94,6 +96,7 @@ CUSTOM_OAUTH_SCOPES=profile,email
CUSTOM_OAUTH_CLIENT_ID=kaneo-client
CUSTOM_OAUTH_CLIENT_SECRET=your-secret-here
CUSTOM_OAUTH_DISCOVERY_URL=https://keycloak.example.com/realms/your-realm/.well-known/openid-configuration
+CUSTOM_OAUTH_LOGOUT_URL=https://keycloak.example.com/realms/your-realm/protocol/openid-connect/logout
CUSTOM_OAUTH_SCOPES=openid,profile,email
```
diff --git a/apps/web/src/components/user-avatar.tsx b/apps/web/src/components/user-avatar.tsx
index baac7edc9..273fb6632 100644
--- a/apps/web/src/components/user-avatar.tsx
+++ b/apps/web/src/components/user-avatar.tsx
@@ -14,13 +14,17 @@ import {
} from "@/components/ui/menu";
import { Separator } from "@/components/ui/separator";
import useSignOut from "@/hooks/mutations/use-sign-out";
+import useGetConfig from "@/hooks/queries/config/use-get-config";
import { toast } from "@/lib/toast";
import useProjectStore from "@/store/project";
export function UserAvatar() {
const { t } = useTranslation();
const { user } = useAuth();
- const { mutateAsync: signOut, isPending } = useSignOut();
+ const { data: config } = useGetConfig();
+ const { mutateAsync: signOut, isPending } = useSignOut(
+ config?.customOAuthLogoutUrl,
+ );
const queryClient = useQueryClient();
const { setProject } = useProjectStore();
const navigate = useNavigate();
diff --git a/apps/web/src/fetchers/oauth/get-id-token.ts b/apps/web/src/fetchers/oauth/get-id-token.ts
new file mode 100644
index 000000000..083227f34
--- /dev/null
+++ b/apps/web/src/fetchers/oauth/get-id-token.ts
@@ -0,0 +1,18 @@
+import { client } from "@kaneo/libs";
+import type { InferResponseType } from "hono/client";
+
+export type GetIdTokenResponse = InferResponseType<
+ (typeof client)["oauth"]["id-token"]["$get"]
+>;
+
+export async function getIdToken() {
+ const response = await client.oauth["id-token"].$get();
+
+ if (!response.ok) {
+ const error = await response.text();
+ throw new Error(error);
+ }
+
+ const data = await response.json();
+ return data;
+}
diff --git a/apps/web/src/hooks/mutations/use-sign-out.ts b/apps/web/src/hooks/mutations/use-sign-out.ts
index eec9d6d1e..ffa042db9 100644
--- a/apps/web/src/hooks/mutations/use-sign-out.ts
+++ b/apps/web/src/hooks/mutations/use-sign-out.ts
@@ -1,16 +1,39 @@
import { useMutation } from "@tanstack/react-query";
import { useNavigate } from "@tanstack/react-router";
+import { getIdToken } from "@/fetchers/oauth/get-id-token";
import { authClient } from "@/lib/auth-client";
-function useSignOut() {
+function useSignOut(idpLogoutUrl?: string | null) {
const navigate = useNavigate();
return useMutation({
mutationFn: async () => {
+ let idToken: string | null = null;
+
+ if (idpLogoutUrl) {
+ try {
+ const data = await getIdToken();
+ idToken = data.idToken;
+ } catch {
+ // If we can't get the id_token, proceed without it
+ }
+ }
+
const result = await authClient.signOut({
fetchOptions: {
onSuccess: () => {
- navigate({ to: "/auth/sign-in" });
+ if (idpLogoutUrl) {
+ const redirectUri = `${window.location.origin}/auth/sign-in`;
+ const url = new URL(idpLogoutUrl);
+ url.searchParams.set("post_logout_redirect_uri", redirectUri);
+ if (idToken) {
+ url.searchParams.set("id_token_hint", idToken);
+ }
+ window.location.href = url.toString();
+ console.log("Redirecting to IDP logout URL:", url.toString());
+ } else {
+ navigate({ to: "/auth/sign-in" });
+ }
},
},
});