|
| 1 | +import { GetOrganizations200ResponseOneOfInner, ManagementClient } from "auth0"; |
| 2 | +import jwt from "jsonwebtoken"; |
| 3 | + |
| 4 | +import { auth0 } from "@/lib/auth0"; |
| 5 | + |
| 6 | +import { AsyncCache } from "./AsyncCache"; |
| 7 | +import { Auth0OrgName } from "./types"; |
| 8 | + |
| 9 | +let AUTH0_MANAGEMENT_CLIENT: ManagementClient | undefined; |
| 10 | + |
| 11 | +export function getAuth0ManagementClient() { |
| 12 | + if (AUTH0_MANAGEMENT_CLIENT == null) { |
| 13 | + // eslint-disable-next-line turbo/no-undeclared-env-vars |
| 14 | + const { AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET } = process.env; |
| 15 | + |
| 16 | + if (AUTH0_DOMAIN == null) { |
| 17 | + throw new Error("AUTH0_DOMAIN is not defined"); |
| 18 | + } |
| 19 | + if (AUTH0_CLIENT_ID == null) { |
| 20 | + throw new Error("AUTH0_CLIENT_ID is not defined"); |
| 21 | + } |
| 22 | + if (AUTH0_CLIENT_SECRET == null) { |
| 23 | + throw new Error("AUTH0_CLIENT_SECRET is not defined"); |
| 24 | + } |
| 25 | + |
| 26 | + AUTH0_MANAGEMENT_CLIENT = new ManagementClient({ |
| 27 | + domain: AUTH0_DOMAIN, |
| 28 | + clientId: AUTH0_CLIENT_ID, |
| 29 | + clientSecret: AUTH0_CLIENT_SECRET, |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + return AUTH0_MANAGEMENT_CLIENT; |
| 34 | +} |
| 35 | + |
| 36 | +export async function getCurrentSession() { |
| 37 | + const session = await auth0.getSession(); |
| 38 | + if (session == null) { |
| 39 | + throw new Error("Not authenticated"); |
| 40 | + } |
| 41 | + if (session.idToken == null) { |
| 42 | + throw new Error("idToken is not present on session"); |
| 43 | + } |
| 44 | + if (typeof session.idToken !== "string") { |
| 45 | + throw new Error( |
| 46 | + `idToken is of type ${typeof session.idToken} (expected string)` |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + const jwtPayload = jwt.decode(session.idToken); |
| 51 | + if (jwtPayload == null) { |
| 52 | + throw new Error("JWT payload is not defined"); |
| 53 | + } |
| 54 | + if (typeof jwtPayload !== "object") { |
| 55 | + throw new Error("JWT payload is not an object"); |
| 56 | + } |
| 57 | + if (jwtPayload?.sub == null) { |
| 58 | + throw new Error("JWT payload does not include 'sub'"); |
| 59 | + } |
| 60 | + |
| 61 | + return { session, userId: jwtPayload.sub }; |
| 62 | +} |
| 63 | + |
| 64 | +export async function getCurrentOrgId() { |
| 65 | + const session = await auth0.getSession(); |
| 66 | + |
| 67 | + if (session?.user.org_id == null) { |
| 68 | + throw new Error("org_id is not defined"); |
| 69 | + } |
| 70 | + |
| 71 | + return session.user.org_id; |
| 72 | +} |
| 73 | + |
| 74 | +const ORGANIZATIONS_CACHE = new AsyncCache< |
| 75 | + Auth0OrgName, |
| 76 | + GetOrganizations200ResponseOneOfInner |
| 77 | +>({ |
| 78 | + ttlInSeconds: 10, |
| 79 | +}); |
| 80 | + |
| 81 | +export async function getCurrentOrg() { |
| 82 | + const orgId = await getCurrentOrgId(); |
| 83 | + |
| 84 | + return await ORGANIZATIONS_CACHE.get(orgId, async () => { |
| 85 | + const { data: organization } = |
| 86 | + await getAuth0ManagementClient().organizations.get({ |
| 87 | + id: orgId, |
| 88 | + }); |
| 89 | + return organization; |
| 90 | + }); |
| 91 | +} |
0 commit comments