Skip to content

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

4.2 KB
Raw Blame
import fp from "fastify-plugin";

import { logger } from "@app/lib/logger";
import { ActorType } from "@app/services/auth/auth-type";

// inject permission type needed based on auth extracted
export const injectPermission = fp(async (server) => {
  server.decorateRequest("permission");
  server.addHook("onRequest", async (req) => {
    if (!req.auth) return;

    if (req.auth.actor === ActorType.USER) {
      req.permission = {
        type: ActorType.USER,
        id: req.auth.userId,
        orgId: req.auth.orgId, // if the req.auth.authMode is AuthMode.API_KEY, the orgId will be "API_KEY"
        authMethod: req.auth.authMethod, // if the req.auth.authMode is AuthMode.API_KEY, the authMethod will be null
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForIdentity=${req.auth.userId}] [type=${ActorType.USER}]`
      );
    } else if (req.auth.actor === ActorType.IDENTITY) {
      req.permission = {
        type: ActorType.IDENTITY,
        id: req.auth.identityId,
        orgId: req.auth.orgId,
        authMethod: null,
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForIdentity=${req.auth.identityId}] [type=${ActorType.IDENTITY}]`
      );
    } else if (req.auth.actor === ActorType.SERVICE) {
      req.permission = {
        type: ActorType.SERVICE,
        id: req.auth.serviceTokenId,
        orgId: req.auth.orgId,
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId,
        authMethod: null
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForIdentity=${req.auth.serviceTokenId}] [type=${ActorType.SERVICE}]`
      );
    } else if (req.auth.actor === ActorType.SCIM_CLIENT) {
      req.permission = {
        type: ActorType.SCIM_CLIENT,
        id: req.auth.scimTokenId,
        orgId: req.auth.orgId,
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId,
        authMethod: null
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForIdentity=${req.auth.scimTokenId}] [type=${ActorType.SCIM_CLIENT}]`
      );
    } else if (req.auth.actor === ActorType.GATEWAY) {
      req.permission = {
        type: ActorType.GATEWAY,
        id: req.auth.gatewayId,
        orgId: req.auth.orgId,
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId,
        authMethod: null
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForGateway=${req.auth.gatewayId}] [type=${ActorType.GATEWAY}]`
      );
    } else if (req.auth.actor === ActorType.RELAY) {
      req.permission = {
        type: ActorType.RELAY,
        id: req.auth.relayId,
        orgId: req.auth.orgId,
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId,
        authMethod: null
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForRelay=${req.auth.relayId}] [type=${ActorType.RELAY}]`
      );
    } else if (req.auth.actor === ActorType.KMIP_SERVER) {
      req.permission = {
        type: ActorType.KMIP_SERVER,
        id: req.auth.kmipServerId,
        orgId: req.auth.orgId,
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId,
        authMethod: null
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForKmipServer=${req.auth.kmipServerId}] [type=${ActorType.KMIP_SERVER}]`
      );
    } else if (req.auth.actor === ActorType.AGENT_VAULT_PROXY) {
      // Without this the proxy routes' rate-limit keyGenerator falls back to source IP.
      req.permission = {
        type: ActorType.AGENT_VAULT_PROXY,
        id: req.auth.agentVaultProxyId,
        orgId: req.auth.orgId,
        rootOrgId: req.auth.rootOrgId,
        parentOrgId: req.auth.parentOrgId,
        authMethod: null
      };

      logger.info(
        `injectPermission: Injecting permissions for [permissionsForAgentVaultProxy=${req.auth.agentVaultProxyId}] [type=${ActorType.AGENT_VAULT_PROXY}]`
      );
    }
  });
});