feat: auth role

This commit is contained in:
Methapon2001 2023-11-24 13:49:08 +07:00
parent bd8290b6b1
commit 34c3f27418
No known key found for this signature in database
GPG key ID: 849924FEF46BD132

View file

@ -17,7 +17,7 @@ const jwtVerify = createVerifier({
export function expressAuthentication(
request: express.Request,
securityName: string,
_scopes?: string[],
scopes?: string[],
) {
return new Promise(async (resolve, reject) => {
if (securityName !== "bearerAuth") reject(new Error("Unknown authentication method."));
@ -34,6 +34,12 @@ export function expressAuthentication(
return reject(new HttpError(HttpStatusCode.UNAUTHORIZED, "Invalid token provided."));
}
if (scopes && !scopes.every((v) => payload.resource_access[payload.azp].roles.includes(v))) {
return reject(
new HttpError(HttpStatusCode.FORBIDDEN, "You are not allowed to perform this action."),
);
}
return resolve(payload);
});
}