|
| 1 | +import fp from "fastify-plugin"; |
| 2 | +import { FastifyPluginAsync, FastifyRequest } from "fastify"; |
| 3 | +import { UnauthorizedError } from "../../common/errors/index.js"; |
| 4 | +import { |
| 5 | + AuthorizationPoliciesRegistry, |
| 6 | + AvailableAuthorizationPolicies, |
| 7 | +} from "api/policies/definition.js"; |
| 8 | +import { evaluatePolicy } from "api/policies/evaluator.js"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Evaluates all policy restrictions for a request |
| 12 | + * @param {FastifyRequest} request - The Fastify request object |
| 13 | + * @returns {Promise<boolean>} - True if all policies pass, throws error otherwise |
| 14 | + */ |
| 15 | +export const evaluateAllRequestPolicies = async ( |
| 16 | + request: FastifyRequest, |
| 17 | +): Promise<boolean | string> => { |
| 18 | + if (!request.policyRestrictions) { |
| 19 | + return true; |
| 20 | + } |
| 21 | + |
| 22 | + for (const restriction of request.policyRestrictions) { |
| 23 | + if ( |
| 24 | + AuthorizationPoliciesRegistry[ |
| 25 | + restriction.name as keyof AvailableAuthorizationPolicies |
| 26 | + ] === undefined |
| 27 | + ) { |
| 28 | + request.log.warn(`Invalid policy name ${restriction.name}, skipping...`); |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + const policyFunction = |
| 33 | + AuthorizationPoliciesRegistry[ |
| 34 | + restriction.name as keyof AvailableAuthorizationPolicies |
| 35 | + ]; |
| 36 | + const policyResult = evaluatePolicy(request, { |
| 37 | + policy: policyFunction, |
| 38 | + params: restriction.params, |
| 39 | + }); |
| 40 | + |
| 41 | + request.log.info( |
| 42 | + `Policy ${restriction.name} evaluated to ${policyResult.allowed}.`, |
| 43 | + ); |
| 44 | + |
| 45 | + if (!policyResult.allowed) { |
| 46 | + return policyResult.message; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return true; |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Fastify plugin to evaluate authorization policies after the request body has been parsed |
| 55 | + */ |
| 56 | +const evaluatePoliciesPluginAsync: FastifyPluginAsync = async ( |
| 57 | + fastify, |
| 58 | + _options, |
| 59 | +) => { |
| 60 | + // Register a hook that runs after body parsing but before route handler |
| 61 | + fastify.addHook("preHandler", async (request: FastifyRequest, _reply) => { |
| 62 | + const result = await evaluateAllRequestPolicies(request); |
| 63 | + if (typeof result === "string") { |
| 64 | + throw new UnauthorizedError({ |
| 65 | + message: result, |
| 66 | + }); |
| 67 | + } |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +// Export the plugin as a properly wrapped fastify-plugin |
| 72 | +export default fp(evaluatePoliciesPluginAsync, { |
| 73 | + name: "evaluatePolicies", |
| 74 | +}); |
0 commit comments