feat: OAuth PKCE (#25313)

* add public client

* implement PKCE

* pass codeChallenge and codeChallengeMethod to handler

* fixes for secure oauth flow

* fix type error

* clean up refresh token endpoint

* only support S256

* fix type error

* remove comment

* add tests

* fix type errors in route.test.ts

* add missing support for refresh token

* add e2e test for public client refresh tokens

* allow pkce for confidential clients

* fix type error

* fix e2e

* fix option pkce for confidential clients

* e2e test improvements

* fix test

* remove only

* add delay

* fix e2e tests

* remove only

* don't skip pkce if codeChallenge is set

* add service functions for token endpoint

* use service function in refreshToken endpoint

* use repository

* remove return types

* e2e test fixes

* fix e2e test

* remove .only in e2e test

* remove pause

* fix error responses in token endpoints

* adjust tests to new error responses

* fix error responses

* e2e improvements

* redirect on error

* adjust tests

* Update apps/web/modules/auth/oauth2/authorize-view.tsx

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: CarinaWolli <[email protected]>
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-11-26 17:02:42 +01:00
1 parent c95b0833c1
commit 4e0798577a
19 files changed
+2034 -110

No files matched your search

@@ -24,6 +24,8 @@ export default function Authorize() {
const client_id = (searchParams?.get("client_id") as string) || "";
const state = searchParams?.get("state") as string;
const scope = searchParams?.get("scope") as string;
const code_challenge = searchParams?.get("code_challenge") as string;
const code_challenge_method = searchParams?.get("code_challenge_method") as string;
const queryString = searchParams?.toString();
@@ -46,6 +48,30 @@ export default function Authorize() {
onSuccess: (data) => {
window.location.href = `${client?.redirectUri}?code=${data.authorizationCode}&state=${state}`;
},
onError: (error) => {
let oauthError = "server_error";
if (error.data?.code === "BAD_REQUEST") {
oauthError = "invalid_request";
} else if (error.data?.code === "UNAUTHORIZED") {
oauthError = "unauthorized_client";
}
const errorParams = new URLSearchParams({
error: oauthError,
});
if (error.message) {
errorParams.append("error_description", error.message);
}
if (state) {
errorParams.append("state", state);
}
if (client?.redirectUri) {
window.location.href = `${client.redirectUri}${client.redirectUri.includes("?") ? "&" : "?"}${errorParams.toString()}`;
}
},
});
const mappedProfiles = data
@@ -144,7 +170,7 @@ export default function Authorize() {
<div>
<Icon name="info" className="mr-1 mt-0.5 h-4 w-4" />
</div>
<div className="ml-1 ">
<div className="ml-1">
<div className="mb-1 text-sm font-medium">
{t("allow_client_to_do", { clientName: client.name })}
</div>
@@ -175,6 +201,8 @@ export default function Authorize() {
teamSlug: selectedAccount?.value.startsWith("team/")
? selectedAccount?.value.substring(5)
: undefined, // team account starts with /team/<slug>
codeChallenge: code_challenge || undefined,
codeChallengeMethod: (code_challenge_method as "S256") || undefined,
});
}}
data-testid="allow-button">
+29 -6
View File
@@ -8,6 +8,8 @@ import { trpc } from "@calcom/trpc";
import { Avatar } from "@calcom/ui/components/avatar";
import { Button } from "@calcom/ui/components/button";
import { Form } from "@calcom/ui/components/form";
import { Label } from "@calcom/ui/components/form";
import { Switch } from "@calcom/ui/components/form";
import { TextField } from "@calcom/ui/components/form";
import { Icon } from "@calcom/ui/components/icon";
import { ImageUploader } from "@calcom/ui/components/image-uploader";
@@ -18,10 +20,16 @@ type FormValues = {
name: string;
redirectUri: string;
logo: string;
enablePkce: boolean;
};
export default function OAuthView() {
const oAuthForm = useForm<FormValues>();
const oAuthForm = useForm<FormValues>({
defaultValues: {
logo: "",
enablePkce: false,
},
});
const [clientSecret, setClientSecret] = useState("");
const [clientId, setClientId] = useState("");
const [logo, setLogo] = useState("");
@@ -29,12 +37,12 @@ export default function OAuthView() {
const mutation = trpc.viewer.oAuth.addClient.useMutation({
onSuccess: async (data) => {
setClientSecret(data.clientSecret);
setClientSecret(data.clientSecret || "");
setClientId(data.clientId);
showToast(`Successfully added ${data.name} as new client`, "success");
},
onError: (error) => {
showToast(`Adding clientfailed: ${error.message}`, "error");
showToast(`Adding client failed: ${error.message}`, "error");
},
});
@@ -48,6 +56,7 @@ export default function OAuthView() {
name: values.name,
redirectUri: values.redirectUri,
logo: values.logo,
enablePkce: values.enablePkce,
});
}}>
<div className="">
@@ -68,6 +77,20 @@ export default function OAuthView() {
placeholder=""
required
/>
<div className="mb-5 mt-5">
<Label className="text-emphasis mb-2 block text-sm font-medium">Authentication Mode</Label>
<div className="flex items-center space-x-3">
<Switch
checked={oAuthForm.watch("enablePkce")}
onCheckedChange={(checked) => oAuthForm.setValue("enablePkce", checked)}
/>
<span className="text-default text-sm">
Use PKCE (recommended for mobile/SPA applications)
</span>
</div>
</div>
<div className="mb-5 mt-5 flex items-center">
<Avatar
alt=""
@@ -98,7 +121,6 @@ export default function OAuthView() {
<div className="mb-2 font-medium">{t("client_id")}</div>
<div className="flex">
<code className="bg-subtle text-default w-full truncate rounded-md rounded-r-none py-[6px] pl-2 pr-2 align-middle font-mono">
{" "}
{clientId}
</code>
<Tooltip side="top" content="Copy to Clipboard">
@@ -119,7 +141,6 @@ export default function OAuthView() {
<div className="mb-2 mt-4 font-medium">{t("client_secret")}</div>
<div className="flex">
<code className="bg-subtle text-default w-full truncate rounded-md rounded-r-none py-[6px] pl-2 pr-2 align-middle font-mono">
{" "}
{clientSecret}
</code>
<Tooltip side="top" content="Copy to Clipboard">
@@ -139,7 +160,9 @@ export default function OAuthView() {
<div className="text-subtle text-sm">{t("copy_client_secret_info")}</div>
</>
) : (
<></>
<div className="text-subtle mt-4 text-sm">
This client uses PKCE authentication (no client secret required).
</div>
)}
<Button
onClick={() => {