feat: add OpenBao + Zitadel OIDC device-code token integration
This commit is contained in:
1 parent
d399919690
commit
9188bfc277
9 files changed
+1232
-15
No files matched your search
@@ -0,0 +1,133 @@
|
||||
import { openExternalUrl, useRpc } from "@getpaseo/plugin/client";
|
||||
import { useToast } from "@getpaseo/plugin/client/ui";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import {
|
||||
type OpenBaoStatus,
|
||||
openbaoAuthStartRpc,
|
||||
openbaoLogoutRpc,
|
||||
openbaoPullRpc,
|
||||
openbaoStatusRpc,
|
||||
} from "../shared/contracts";
|
||||
|
||||
interface UseOpenBaoAuthOptions {
|
||||
onConnected?: () => void | Promise<void>;
|
||||
}
|
||||
|
||||
export function useOpenBaoAuth(options: UseOpenBaoAuthOptions = {}) {
|
||||
const { onConnected } = options;
|
||||
const toast = useToast();
|
||||
const fetchStatus = useRpc(openbaoStatusRpc);
|
||||
const startAuth = useRpc(openbaoAuthStartRpc);
|
||||
const pullRpc = useRpc(openbaoPullRpc);
|
||||
const logoutRpc = useRpc(openbaoLogoutRpc);
|
||||
|
||||
const [status, setStatus] = useState<OpenBaoStatus | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
const refreshStatus = useCallback(async () => {
|
||||
try {
|
||||
const next = await fetchStatus({});
|
||||
setStatus(next);
|
||||
return next;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}, [fetchStatus]);
|
||||
|
||||
useEffect(() => {
|
||||
void refreshStatus();
|
||||
}, [refreshStatus]);
|
||||
|
||||
const pendingStatus = status?.pendingAuth?.status;
|
||||
useEffect(() => {
|
||||
if (pendingStatus !== "pending") return;
|
||||
let cancelled = false;
|
||||
const timer = setInterval(() => {
|
||||
void (async () => {
|
||||
const next = await refreshStatus();
|
||||
if (cancelled || !next) return;
|
||||
const flow = next.pendingAuth;
|
||||
if (flow?.status === "complete") {
|
||||
toast.show(
|
||||
next.tokenPresent
|
||||
? `Connected to OpenBao as ${next.displayName ?? "Zitadel user"} and loaded token.`
|
||||
: `Signed in as ${next.displayName ?? "Zitadel user"}, but token pull failed.`,
|
||||
{ variant: next.tokenPresent ? "success" : "error" },
|
||||
);
|
||||
await onConnected?.();
|
||||
} else if (flow?.status === "error" && flow.error) {
|
||||
toast.error(flow.error);
|
||||
}
|
||||
})();
|
||||
}, 2000);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearInterval(timer);
|
||||
};
|
||||
}, [pendingStatus, refreshStatus, onConnected, toast]);
|
||||
|
||||
const startLogin = useCallback(async () => {
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await startAuth({});
|
||||
if (!res.ok) {
|
||||
toast.error(res.error);
|
||||
return null;
|
||||
}
|
||||
await refreshStatus();
|
||||
if (res.pendingAuth.verificationUriComplete) {
|
||||
await openExternalUrl(res.pendingAuth.verificationUriComplete).catch(() => undefined);
|
||||
}
|
||||
return res.pendingAuth;
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : String(error));
|
||||
return null;
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}, [startAuth, refreshStatus, toast]);
|
||||
|
||||
const pullNow = useCallback(async () => {
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await pullRpc({});
|
||||
if (!res.ok) {
|
||||
toast.error(res.error);
|
||||
await refreshStatus();
|
||||
return false;
|
||||
}
|
||||
setStatus(res.status);
|
||||
toast.show("Pulled Fly token from OpenBao.", { variant: "success" });
|
||||
await onConnected?.();
|
||||
return true;
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : String(error));
|
||||
return false;
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}, [pullRpc, refreshStatus, onConnected, toast]);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
setBusy(true);
|
||||
try {
|
||||
await logoutRpc({});
|
||||
await refreshStatus();
|
||||
toast.show("Signed out of OpenBao.");
|
||||
await onConnected?.();
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : String(error));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}, [logoutRpc, refreshStatus, onConnected, toast]);
|
||||
|
||||
return {
|
||||
status,
|
||||
busy,
|
||||
refreshStatus,
|
||||
startLogin,
|
||||
pullNow,
|
||||
logout,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user