- Enrich context with user roles, permissions, and resources from Logto. - Update setAuth function to handle JWT decoding and user data retrieval. - Refactor initGuard to utilize new auth structure. - Modify resource and role services to support new data types and error handling.
106 lines
3.5 KiB
TypeScript
106 lines
3.5 KiB
TypeScript
import axios from "axios";
|
|
import { GraphQLError } from "graphql";
|
|
import { Resource } from "../../../types/Resource";
|
|
import { GraphQLContext } from "../../context/types";
|
|
import { getLogtoAccessToken } from "../User/resolver";
|
|
|
|
function getAccessTokenFromContext(context: GraphQLContext): string | null {
|
|
return context.accessToken || null;
|
|
}
|
|
|
|
export async function listResources(context: GraphQLContext): Promise<Resource[]> {
|
|
const token = getAccessTokenFromContext(context) || (await getLogtoAccessToken());
|
|
try {
|
|
const response = await axios.get(`${process.env.LOGTO_ENDPOINT}/api/resources`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
return (response.data as Resource[]).map(resource => ({
|
|
...resource,
|
|
scopes: Array.isArray(resource.scopes) ? resource.scopes : [],
|
|
}));
|
|
} catch {
|
|
throw new GraphQLError("Erreur lors de la récupération des ressources", {
|
|
extensions: { code: "RESOURCES_FETCH_FAILED" },
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function getResource(id: string, context: GraphQLContext): Promise<Resource> {
|
|
const token = getAccessTokenFromContext(context) || (await getLogtoAccessToken());
|
|
try {
|
|
const response = await axios.get(`${process.env.LOGTO_ENDPOINT}/api/resources/${id}`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
const resource = response.data as Resource;
|
|
return {
|
|
...resource,
|
|
scopes: Array.isArray(resource.scopes) ? resource.scopes : [],
|
|
};
|
|
} catch {
|
|
throw new GraphQLError("Erreur lors de la récupération de la ressource", {
|
|
extensions: { code: "RESOURCE_FETCH_FAILED" },
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function createResource(
|
|
name: string,
|
|
indicator: string,
|
|
_scopes: string[], // ignoré
|
|
description: string | null,
|
|
context: GraphQLContext
|
|
): Promise<Resource> {
|
|
const token = getAccessTokenFromContext(context) || (await getLogtoAccessToken());
|
|
try {
|
|
const response = await axios.post(
|
|
`${process.env.LOGTO_ENDPOINT}/api/resources`,
|
|
{ name, indicator, description },
|
|
{ headers: { Authorization: `Bearer ${token}` } }
|
|
);
|
|
return response.data as Resource;
|
|
} catch {
|
|
throw new GraphQLError("Erreur lors de la création de la ressource", {
|
|
extensions: { code: "CREATE_RESOURCE_FAILED" },
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function updateResource(
|
|
id: string,
|
|
name: string,
|
|
indicator: string,
|
|
_scopes: string[], // ignoré
|
|
description: string | null,
|
|
context: GraphQLContext
|
|
): Promise<Resource> {
|
|
const token = getAccessTokenFromContext(context) || (await getLogtoAccessToken());
|
|
try {
|
|
const response = await axios.patch(
|
|
`${process.env.LOGTO_ENDPOINT}/api/resources/${id}`,
|
|
{ name, indicator, description },
|
|
{ headers: { Authorization: `Bearer ${token}` } }
|
|
);
|
|
return response.data as Resource;
|
|
} catch {
|
|
throw new GraphQLError("Erreur lors de la mise à jour de la ressource", {
|
|
extensions: { code: "UPDATE_RESOURCE_FAILED" },
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function deleteResource(id: string, context: GraphQLContext): Promise<boolean> {
|
|
const token = getAccessTokenFromContext(context) || (await getLogtoAccessToken());
|
|
try {
|
|
await axios.delete(`${process.env.LOGTO_ENDPOINT}/api/resources/${id}`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
});
|
|
return true;
|
|
} catch {
|
|
throw new GraphQLError("Erreur lors de la suppression de la ressource", {
|
|
extensions: { code: "DELETE_RESOURCE_FAILED" },
|
|
});
|
|
}
|
|
}
|
|
|
|
export type { Resource };
|