Require Logto team claim for billing auth
Build and deploy Docker image / build (push) Successful in 3m31s
Build and deploy Docker image / build (push) Successful in 3m31s
This commit is contained in:
-30
@@ -7,10 +7,6 @@ const LOGTO_JWKS_URL =
|
|||||||
const LOGTO_ISSUER = process.env.LOGTO_ISSUER || "https://auth.optovia.ru/oidc";
|
const LOGTO_ISSUER = process.env.LOGTO_ISSUER || "https://auth.optovia.ru/oidc";
|
||||||
const LOGTO_BILLING_AUDIENCE =
|
const LOGTO_BILLING_AUDIENCE =
|
||||||
process.env.LOGTO_BILLING_AUDIENCE || "https://billing.optovia.ru";
|
process.env.LOGTO_BILLING_AUDIENCE || "https://billing.optovia.ru";
|
||||||
const TEAMS_USER_GRAPHQL_URL =
|
|
||||||
process.env.TEAMS_USER_GRAPHQL_URL ||
|
|
||||||
"https://teams.optovia.ru/graphql/user/";
|
|
||||||
const SESSION_TOKEN_PREFIX = "optovia-session:";
|
|
||||||
|
|
||||||
const jwks = createRemoteJWKSet(new URL(LOGTO_JWKS_URL));
|
const jwks = createRemoteJWKSet(new URL(LOGTO_JWKS_URL));
|
||||||
|
|
||||||
@@ -51,32 +47,6 @@ export async function m2mContext(): Promise<AuthContext> {
|
|||||||
|
|
||||||
export async function teamContext(req: FastifyRequest): Promise<AuthContext> {
|
export async function teamContext(req: FastifyRequest): Promise<AuthContext> {
|
||||||
const token = getBearerToken(req);
|
const token = getBearerToken(req);
|
||||||
if (token.startsWith(SESSION_TOKEN_PREFIX)) {
|
|
||||||
const response = await fetch(TEAMS_USER_GRAPHQL_URL, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"content-type": "application/json",
|
|
||||||
authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
query: `query BillingSessionMe { me { id activeTeamId } }`,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const body = (await response.json()) as {
|
|
||||||
data?: { me?: { id?: string; activeTeamId?: string } };
|
|
||||||
};
|
|
||||||
const me = body.data?.me;
|
|
||||||
if (!me?.id || !me.activeTeamId) {
|
|
||||||
throw new GraphQLError("Unauthorized", {
|
|
||||||
extensions: { code: "UNAUTHENTICATED" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
userId: me.id,
|
|
||||||
teamUuid: me.activeTeamId,
|
|
||||||
scopes: ["teams:member"],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const { payload } = await jwtVerify(token, jwks, {
|
const { payload } = await jwtVerify(token, jwks, {
|
||||||
issuer: LOGTO_ISSUER,
|
issuer: LOGTO_ISSUER,
|
||||||
audience: LOGTO_BILLING_AUDIENCE,
|
audience: LOGTO_BILLING_AUDIENCE,
|
||||||
|
|||||||
Reference in New Issue
Block a user