Skip to content

Commit

Permalink
feat: fga-based workos rbac (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Nov 4, 2024
1 parent d8cb555 commit fae7cab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
25 changes: 23 additions & 2 deletions packages/ui/docs-bundle/src/server/auth/getAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ export async function getAuthState(
const session = fernToken != null ? await getSessionFromToken(fernToken) : undefined;
const workosUserInfo = await toSessionUserInfo(session);
if (workosUserInfo.user) {
// TODO: should this be stored in the session itself?
const roles = await getWorkosRbacRoles(authConfig.organization, workosUserInfo.user.email);
return {
domain,
host,
authed: true,
ok: true,
user: toFernUser(workosUserInfo),
user: toFernUser(workosUserInfo, roles),
partner: authConfig.partner,
};
}
Expand All @@ -116,12 +118,14 @@ export async function getAuthState(
if (setFernToken) {
setFernToken(await encryptSession(updatedSession));
}
// TODO: should this be stored in the session itself?
const roles = await getWorkosRbacRoles(authConfig.organization, updatedSession.user.email);
return {
domain,
host,
authed: true,
ok: true,
user: toFernUser(await toSessionUserInfo(updatedSession)),
user: toFernUser(await toSessionUserInfo(updatedSession), roles),
partner: authConfig.partner,
};
}
Expand Down Expand Up @@ -161,3 +165,20 @@ function getAuthorizationUrl(authConfig: AuthEdgeConfig, host: string, pathname?

return undefined;
}

export async function getWorkosRbacRoles(org: string, email: string): Promise<string[]> {
try {
// TODO: use `rbac.ferndocs.dev` for staging, and `rbac.ferndocs.com` for production, once available
const roles = await fetch(
`https://rbac.ferndocs.dev/${encodeURIComponent(org)}/users/${encodeURIComponent(email)}/roles`,
).then((res) => res.json());
if (Array.isArray(roles)) {
return roles.filter((role) => typeof role === "string");
}
return [];
} catch (error) {
// eslint-disable-next-line no-console
console.error(`Error fetching RBAC roles for ${org}/${email}: ${error}`);
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { FernUser } from "@fern-ui/fern-docs-auth";
import { compact } from "es-toolkit/array";
import { NoWorkOSUserInfo, WorkOSUserInfo } from "./interfaces";

export function toFernUser({ user }: WorkOSUserInfo | NoWorkOSUserInfo): FernUser {
export function toFernUser({ user }: WorkOSUserInfo | NoWorkOSUserInfo, roles?: string[]): FernUser {
return {
email: user?.email,
name: compact([user?.firstName, user?.lastName]).join(" ") || user?.email?.split("@")[0],
roles,
};
}

0 comments on commit fae7cab

Please sign in to comment.