Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
import { mutationOptions, type QueryClient } from "@tanstack/react-query";
import { client } from "@/lib/client";
import { identityProviderKeys } from "./queries";
const FALLBACK = "Could not change that identity provider";
function invalidateProviders(queryClient: QueryClient) {
return queryClient.invalidateQueries({ queryKey: identityProviderKeys.all });
}
/**
* What an administrator has to supply to register an identity provider.
*
* SAML wants the metadata XML their identity team gives them, which carries the entry point and the
* signing certificate. OIDC wants an issuer and a client, because there is no equivalent document to
* paste. Both want the domain, since that is what routes somebody to this provider rather than
* another.
*/
export type IdentityProviderInput =
| {
protocol: "saml";
providerId: string;
domain: string;
issuer: string;
entryPoint: string;
metadata: string;
}
| {
protocol: "oidc";
providerId: string;
domain: string;
issuer: string;
clientId: string;
clientSecret: string;
};
/** The body Better Auth's own route expects, which is not the shape the form collects. */
function registerBody(input: IdentityProviderInput) {
const common = {
providerId: input.providerId,
issuer: input.issuer,
domain: input.domain,
};
if (input.protocol === "saml") {
return {
...common,
samlConfig: {
entryPoint: input.entryPoint,
idpMetadata: { metadata: input.metadata },
},
};
}
return {
...common,
oidcConfig: {
clientId: input.clientId,
clientSecret: input.clientSecret,
// Let the provider describe itself rather than asking somebody to type six endpoints.
discoveryEndpoint: `${input.issuer.replace(/\/$/, "")}/.well-known/openid-configuration`,
},
};
}
export function registerIdentityProviderMutationOptions(
queryClient: QueryClient,
) {
return mutationOptions({
mutationFn: async (input: IdentityProviderInput): Promise<void> => {
await client("/api/auth/sso/register", {
method: "POST",
body: registerBody(input),
fallback: FALLBACK,
});
},
onSuccess: () => invalidateProviders(queryClient),
});
}
/**
* Remove one.
*
* Our own route, not Better Auth's `delete-provider`, which refuses unless the person asking is the
* one who registered it. That left a provider nobody could remove the moment the administrator who
* set it up had left, which is exactly when somebody needs to.
*/
export function deleteIdentityProviderMutationOptions(
queryClient: QueryClient,
) {
return mutationOptions({
mutationFn: async (providerId: string): Promise<void> => {
await client(
`/api/admin/identity-providers/${encodeURIComponent(providerId)}`,
{ method: "DELETE", fallback: FALLBACK },
);
},
onSuccess: () => invalidateProviders(queryClient),
});
}