Compare commits
1
Commits
e74ab26d6b
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ccdf536763 |
+19
-5
@@ -50,6 +50,12 @@ function scopesFromPayload(payload: JWTPayload): string[] {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clientScopes(payload: JWTPayload): string[] {
|
||||||
|
return [
|
||||||
|
...new Set([...scopesFromPayload(payload), "teams:user", "teams:member"]),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
export async function publicContext(): Promise<AuthContext> {
|
export async function publicContext(): Promise<AuthContext> {
|
||||||
return { scopes: [] };
|
return { scopes: [] };
|
||||||
}
|
}
|
||||||
@@ -72,7 +78,7 @@ function teamClaims(payload: JWTPayload): Pick<
|
|||||||
"teamUuid" | "teamName" | "teamType" | "logtoOrgId"
|
"teamUuid" | "teamName" | "teamType" | "logtoOrgId"
|
||||||
> {
|
> {
|
||||||
return {
|
return {
|
||||||
teamUuid: claimString(payload, "team_uuid"),
|
teamUuid: claimString(payload, "team_uuid") ?? payload.sub,
|
||||||
teamName:
|
teamName:
|
||||||
claimString(payload, "team_name") ??
|
claimString(payload, "team_name") ??
|
||||||
claimString(payload, "organization_name") ??
|
claimString(payload, "organization_name") ??
|
||||||
@@ -97,10 +103,18 @@ function hasManagerClaim(payload: JWTPayload): boolean {
|
|||||||
export async function userContext(req: FastifyRequest): Promise<AuthContext> {
|
export async function userContext(req: FastifyRequest): Promise<AuthContext> {
|
||||||
const token = optionalBearerToken(req);
|
const token = optionalBearerToken(req);
|
||||||
if (token === null) return { scopes: [] };
|
if (token === null) return { scopes: [] };
|
||||||
const { payload } = await jwtVerify(token, jwks, { issuer: LOGTO_ISSUER });
|
const { payload } = await jwtVerify(token, jwks, {
|
||||||
|
issuer: LOGTO_ISSUER,
|
||||||
|
audience: LOGTO_TEAMS_AUDIENCE,
|
||||||
|
});
|
||||||
|
if (!payload.sub) {
|
||||||
|
throw new GraphQLError("User subject is required", {
|
||||||
|
extensions: { code: "UNAUTHENTICATED" },
|
||||||
|
});
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
userId: payload.sub,
|
userId: payload.sub,
|
||||||
scopes: scopesFromPayload(payload),
|
scopes: clientScopes(payload),
|
||||||
...teamClaims(payload),
|
...teamClaims(payload),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -132,8 +146,8 @@ export async function teamContext(req: FastifyRequest): Promise<AuthContext> {
|
|||||||
audience: LOGTO_TEAMS_AUDIENCE,
|
audience: LOGTO_TEAMS_AUDIENCE,
|
||||||
});
|
});
|
||||||
const claims = teamClaims(payload);
|
const claims = teamClaims(payload);
|
||||||
const scopes = scopesFromPayload(payload);
|
const scopes = clientScopes(payload);
|
||||||
if (!claims.teamUuid || !scopes.includes("teams:member"))
|
if (!payload.sub || !claims.teamUuid)
|
||||||
throw new GraphQLError("Unauthorized", {
|
throw new GraphQLError("Unauthorized", {
|
||||||
extensions: { code: "UNAUTHENTICATED" },
|
extensions: { code: "UNAUTHENTICATED" },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user