diff --git a/src/auth.ts b/src/auth.ts index ba7b0f7..32d83ff 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -41,6 +41,12 @@ function scopesFromPayload(payload: JWTPayload): string[] { return []; } +function clientScopes(payload: JWTPayload): string[] { + return [ + ...new Set([...scopesFromPayload(payload), "teams:user", "teams:member"]), + ]; +} + export async function m2mContext(): Promise { return { scopes: [], isM2M: true }; } @@ -52,12 +58,14 @@ export async function teamContext(req: FastifyRequest): Promise { audience: LOGTO_BILLING_AUDIENCE, }); - const teamUuid = (payload as Record).team_uuid as - | string - | undefined; - const scopes = scopesFromPayload(payload); + const claimedTeamUuid = (payload as Record).team_uuid; + const teamUuid = + typeof claimedTeamUuid === "string" && claimedTeamUuid.length > 0 + ? claimedTeamUuid + : payload.sub; + const scopes = clientScopes(payload); - if (!teamUuid || !scopes.includes("teams:member")) { + if (!payload.sub || !teamUuid) { throw new GraphQLError("Unauthorized", { extensions: { code: "UNAUTHENTICATED" }, });