feat: primary forwarding mode completed

This commit is contained in:
=
2025-09-04 22:29:16 +05:30
parent c116eb9ed2
commit 035156bcc3
8 changed files with 77 additions and 5 deletions
@@ -107,11 +107,16 @@ export const extractAuth = async (req: FastifyRequest, jwtSecret: string) => {
};
// ! Important: You can only 100% count on the `req.permission.orgId` field being present when the auth method is Identity Access Token (Machine Identity).
export const injectIdentity = fp(async (server: FastifyZodProvider) => {
export const injectIdentity = fp(async (server: FastifyZodProvider, opt: { isPrimaryForwardingMode?: boolean }) => {
server.decorateRequest("auth", null);
server.decorateRequest("isPrimaryForwardingMode", Boolean(opt.isPrimaryForwardingMode));
server.addHook("onRequest", async (req) => {
const appCfg = getConfig();
if (opt.isPrimaryForwardingMode && req.method !== "GET") {
return;
}
if (req.url.includes(".well-known/est") || req.url.includes("/api/v3/auth/")) {
return;
}
@@ -10,6 +10,10 @@ interface TAuthOptions {
export const verifyAuth =
<T extends FastifyRequest>(authStrategies: AuthMode[], options: TAuthOptions = { requireOrg: true }) =>
(req: T, _res: FastifyReply, done: HookHandlerDoneFunction) => {
if (req.isPrimaryForwardingMode && req.method !== "GET") {
return done();
}
if (!Array.isArray(authStrategies)) throw new Error("Auth strategy must be array");
if (!req.auth) throw new UnauthorizedError({ message: "Token missing" });
@@ -0,0 +1,14 @@
import replyFrom from "@fastify/reply-from";
import fp from "fastify-plugin";
export const primaryForwardingMode = fp(async (server, opt: { primaryUrl: string }) => {
await server.register(replyFrom, {
base: opt.primaryUrl
});
server.addHook("preValidation", async (request, reply) => {
if (request.url.startsWith("/api") && ["POST", "PUT", "DELETE", "PATCH"].includes(request.method)) {
return reply.from(request.url);
}
});
});