mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-06 01:54:39 +00:00
testing oidc callback
This commit is contained in:
@@ -279,8 +279,20 @@ export default function SitesTable({ sites, orgId }: SitesTableProps) {
|
||||
}
|
||||
];
|
||||
|
||||
async function test() {
|
||||
const res = await api
|
||||
.post("/auth/org/home-lab/idp/1/oidc/generate-url")
|
||||
.then((res) => {
|
||||
if (res.data.data.redirectUrl) {
|
||||
window.location.href = res.data.data.redirectUrl;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button onClick={async () => await test()}>Test</Button>
|
||||
|
||||
<CreateSiteFormModal
|
||||
open={isCreateModalOpen}
|
||||
setOpen={setIsCreateModalOpen}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
"use client";
|
||||
|
||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||
import { createApiClient, formatAxiosError } from "@app/lib/api";
|
||||
import { ValidateOidcUrlCallbackResponse } from "@server/routers/idp";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type ValidateOidcTokenParams = {
|
||||
orgId: string;
|
||||
idpId: string;
|
||||
code: string | undefined;
|
||||
verifier: string | undefined;
|
||||
storedState: string | undefined;
|
||||
expectedState: string | undefined;
|
||||
};
|
||||
|
||||
export default function ValidateOidcToken(props: ValidateOidcTokenParams) {
|
||||
const { env } = useEnvContext();
|
||||
const api = createApiClient({ env });
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.code || !props.verifier) {
|
||||
setError("Missing code or verifier");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!props.storedState) {
|
||||
setError("Missing stored state");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (props.storedState !== props.expectedState) {
|
||||
setError("Invalid state");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
async function validate() {
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const res = await api.post<
|
||||
AxiosResponse<ValidateOidcUrlCallbackResponse>
|
||||
>(
|
||||
`/auth/org/${props.orgId}/idp/${props.idpId}/oidc/validate-callback`,
|
||||
{
|
||||
code: props.code,
|
||||
codeVerifier: props.verifier
|
||||
}
|
||||
);
|
||||
} catch (e) {
|
||||
setError(formatAxiosError(e, "Error validating OIDC token"));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
validate();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>Validating OIDC Token...</h1>
|
||||
{loading && <p>Loading...</p>}
|
||||
{!loading && <p>Token validated successfully!</p>}
|
||||
{error && <p>Error: {error}</p>}
|
||||
</>
|
||||
);
|
||||
}
|
||||
30
src/app/auth/org/[orgId]/idp/[idpId]/oidc/callback/page.tsx
Normal file
30
src/app/auth/org/[orgId]/idp/[idpId]/oidc/callback/page.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { cookies } from "next/headers";
|
||||
import ValidateOidcToken from "./ValidateOidcToken";
|
||||
|
||||
export default async function Page(props: {
|
||||
params: Promise<{ orgId: string; idpId: string }>;
|
||||
searchParams: Promise<{
|
||||
code: string;
|
||||
state: string;
|
||||
}>;
|
||||
}) {
|
||||
const params = await props.params;
|
||||
const searchParams = await props.searchParams;
|
||||
|
||||
const allCookies = await cookies();
|
||||
const stateCookie = allCookies.get("oidc_state")?.value;
|
||||
const verifier = allCookies.get("oidc_code_verifier")?.value;
|
||||
|
||||
return (
|
||||
<>
|
||||
<ValidateOidcToken
|
||||
orgId={params.orgId}
|
||||
idpId={params.idpId}
|
||||
code={searchParams.code}
|
||||
storedState={stateCookie}
|
||||
expectedState={searchParams.state}
|
||||
verifier={verifier}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user