Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Feat: Scoped JWT to organization, require organization on all requests by default on JWT requests

Daniel Hougaarddac5529b6c7a
diff --git a/backend/src/server/plugins/auth/verify-auth.ts b/backend/src/server/plugins/auth/verify-auth.ts
index db97008670..3b3a239f73 100644
--- a/backend/src/server/plugins/auth/verify-auth.ts
+++ b/backend/src/server/plugins/auth/verify-auth.ts
@@ -3,21 +3,25 @@ import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
import { UnauthorizedError } from "@app/lib/errors";
import { AuthMode } from "@app/services/auth/auth-type";
+interface TAuthOptions {
+ requireOrg: boolean;
+}
+
export const verifyAuth =
- <T extends FastifyRequest>(authStrats: AuthMode[], options: { requireOrg: boolean } = { requireOrg: true }) =>
+ <T extends FastifyRequest>(authStrategies: AuthMode[], options: TAuthOptions = { requireOrg: true }) =>
(req: T, _res: FastifyReply, done: HookHandlerDoneFunction) => {
- if (!Array.isArray(authStrats)) throw new Error("Auth strategy must be array");
+ if (!Array.isArray(authStrategies)) throw new Error("Auth strategy must be array");
if (!req.auth) throw new UnauthorizedError({ name: "Unauthorized access", message: "Token missing" });
- const isAccessAllowed = authStrats.some((strat) => strat === req.auth.authMode);
+ const isAccessAllowed = authStrategies.some((strategy) => strategy === req.auth.authMode);
if (!isAccessAllowed) {
throw new UnauthorizedError({ name: `${req.url} Unauthorized Access` });
}
// New optional option. There are some routes which do not require an organization ID to be present on the request.
- // En example of this is the /v1 auth routes.
- if (options.requireOrg === true && !req.permission.orgId) {
- throw new UnauthorizedError({ name: `${req.url} Unauthorized Access, no organization found` });
+ // An example of this is the /v1 auth routes.
+ if (req.auth.authMode === AuthMode.JWT && options.requireOrg === true && !req.permission.orgId) {
+ throw new UnauthorizedError({ name: `${req.url} Unauthorized Access, no organization found in request` });
}
done();