Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
add audit logs into other identities
adilson64a849160df9
diff --git a/backend/src/server/routes/v1/identity-alicloud-auth-router.ts b/backend/src/server/routes/v1/identity-alicloud-auth-router.tsindex c1cb17ee21..a49d2e909a 100644--- a/backend/src/server/routes/v1/identity-alicloud-auth-router.ts+++ b/backend/src/server/routes/v1/identity-alicloud-auth-router.ts@@ -4,12 +4,13 @@ import { z } from "zod";import { IdentityAlicloudAuthsSchema, IdentityAuthMethod } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ALICLOUD_AUTH, ApiDocsTags } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { validateArns } from "@app/services/identity-alicloud-auth/identity-alicloud-auth-validators";import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";@@ -79,43 +80,62 @@ export const registerIdentityAliCloudAuthRouter = async (server: FastifyZodProvi}},handler: async (req) => {- const { identityAliCloudAuth, accessToken, identityAccessToken, identity } =- await server.services.identityAliCloudAuth.login(req.body);+ try {+ const { identityAliCloudAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityAliCloudAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_ALICLOUD_AUTH,- metadata: {- identityId: identityAliCloudAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityAliCloudAuthId: identityAliCloudAuth.id+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_ALICLOUD_AUTH,+ metadata: {+ identityId: identityAliCloudAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityAliCloudAuthId: identityAliCloudAuth.id+ }}- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityAliCloudAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityAliCloudAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.ALICLOUD_AUTH- }- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityAliCloudAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityAliCloudAuth.accessTokenTTL,- accessTokenMaxTTL: identityAliCloudAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityAliCloudAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityAliCloudAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.ALICLOUD_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityAliCloudAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityAliCloudAuth.accessTokenTTL,+ accessTokenMaxTTL: identityAliCloudAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_ALICLOUD_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-aws-iam-auth-router.ts b/backend/src/server/routes/v1/identity-aws-iam-auth-router.tsindex 63e89dff0f..2d1b3ddf77 100644--- a/backend/src/server/routes/v1/identity-aws-iam-auth-router.ts+++ b/backend/src/server/routes/v1/identity-aws-iam-auth-router.ts@@ -3,12 +3,13 @@ import { z } from "zod";import { IdentityAuthMethod, IdentityAwsAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, AWS_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import {validateAccountIds,@@ -46,43 +47,62 @@ export const registerIdentityAwsAuthRouter = async (server: FastifyZodProvider)}},handler: async (req) => {- const { identityAwsAuth, accessToken, identityAccessToken, identity } =- await server.services.identityAwsAuth.login(req.body);+ try {+ const { identityAwsAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityAwsAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_AWS_AUTH,- metadata: {- identityId: identityAwsAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityAwsAuthId: identityAwsAuth.id- }- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityAwsAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityAwsAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.AWS_AUTH+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_AWS_AUTH,+ metadata: {+ identityId: identityAwsAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityAwsAuthId: identityAwsAuth.id+ }}- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityAwsAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityAwsAuth.accessTokenTTL,- accessTokenMaxTTL: identityAwsAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityAwsAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityAwsAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.AWS_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityAwsAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityAwsAuth.accessTokenTTL,+ accessTokenMaxTTL: identityAwsAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_AWS_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-azure-auth-router.ts b/backend/src/server/routes/v1/identity-azure-auth-router.tsindex 544bac737b..c882cd4866 100644--- a/backend/src/server/routes/v1/identity-azure-auth-router.ts+++ b/backend/src/server/routes/v1/identity-azure-auth-router.ts@@ -3,12 +3,13 @@ import { z } from "zod";import { IdentityAuthMethod, IdentityAzureAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, AZURE_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { validateAzureAuthField } from "@app/services/identity-azure-auth/identity-azure-auth-validators";import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";@@ -41,43 +42,62 @@ export const registerIdentityAzureAuthRouter = async (server: FastifyZodProvider}},handler: async (req) => {- const { identityAzureAuth, accessToken, identityAccessToken, identity } =- await server.services.identityAzureAuth.login(req.body);+ try {+ const { identityAzureAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityAzureAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_AZURE_AUTH,- metadata: {- identityId: identityAzureAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityAzureAuthId: identityAzureAuth.id- }- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityAzureAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityAzureAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.AZURE_AUTH+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_AZURE_AUTH,+ metadata: {+ identityId: identityAzureAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityAzureAuthId: identityAzureAuth.id+ }}- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityAzureAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityAzureAuth.accessTokenTTL,- accessTokenMaxTTL: identityAzureAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityAzureAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityAzureAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.AZURE_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityAzureAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityAzureAuth.accessTokenTTL,+ accessTokenMaxTTL: identityAzureAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_AZURE_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-gcp-auth-router.ts b/backend/src/server/routes/v1/identity-gcp-auth-router.tsindex 218b7e8cad..78bf46d4ab 100644--- a/backend/src/server/routes/v1/identity-gcp-auth-router.ts+++ b/backend/src/server/routes/v1/identity-gcp-auth-router.ts@@ -3,12 +3,13 @@ import { z } from "zod";import { IdentityAuthMethod, IdentityGcpAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, GCP_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { validateGcpAuthField } from "@app/services/identity-gcp-auth/identity-gcp-auth-validators";import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";@@ -41,43 +42,62 @@ export const registerIdentityGcpAuthRouter = async (server: FastifyZodProvider)}},handler: async (req) => {- const { identityGcpAuth, accessToken, identityAccessToken, identity } =- await server.services.identityGcpAuth.login(req.body);+ try {+ const { identityGcpAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityGcpAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_GCP_AUTH,- metadata: {- identityId: identityGcpAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityGcpAuthId: identityGcpAuth.id- }- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityGcpAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityGcpAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.GCP_AUTH+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_GCP_AUTH,+ metadata: {+ identityId: identityGcpAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityGcpAuthId: identityGcpAuth.id+ }}- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityGcpAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityGcpAuth.accessTokenTTL,- accessTokenMaxTTL: identityGcpAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityGcpAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityGcpAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.GCP_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityGcpAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityGcpAuth.accessTokenTTL,+ accessTokenMaxTTL: identityGcpAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_GCP_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-jwt-auth-router.ts b/backend/src/server/routes/v1/identity-jwt-auth-router.tsindex 24eb61e605..bbb0024ce2 100644--- a/backend/src/server/routes/v1/identity-jwt-auth-router.ts+++ b/backend/src/server/routes/v1/identity-jwt-auth-router.ts@@ -3,12 +3,13 @@ import { z } from "zod";import { IdentityAuthMethod, IdentityJwtAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, JWT_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { JwtConfigurationType } from "@app/services/identity-jwt-auth/identity-jwt-auth-types";import {@@ -117,43 +118,62 @@ export const registerIdentityJwtAuthRouter = async (server: FastifyZodProvider)}},handler: async (req) => {- const { identityJwtAuth, accessToken, identityAccessToken, identity } =- await server.services.identityJwtAuth.login(req.body);+ try {+ const { identityJwtAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityJwtAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_JWT_AUTH,- metadata: {- identityId: identityJwtAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityJwtAuthId: identityJwtAuth.id+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_JWT_AUTH,+ metadata: {+ identityId: identityJwtAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityJwtAuthId: identityJwtAuth.id+ }}- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityJwtAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityJwtAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.JWT_AUTH- }- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityJwtAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityJwtAuth.accessTokenTTL,- accessTokenMaxTTL: identityJwtAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityJwtAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityJwtAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.JWT_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityJwtAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityJwtAuth.accessTokenTTL,+ accessTokenMaxTTL: identityJwtAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_JWT_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts b/backend/src/server/routes/v1/identity-kubernetes-auth-router.tsindex 105f1f28dd..58f08717a1 100644--- a/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts+++ b/backend/src/server/routes/v1/identity-kubernetes-auth-router.ts@@ -3,13 +3,14 @@ import { z } from "zod";import { IdentityAuthMethod, IdentityKubernetesAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, KUBERNETES_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { IdentityKubernetesAuthTokenReviewMode } from "@app/services/identity-kubernetes-auth/identity-kubernetes-auth-types";import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";@@ -62,43 +63,62 @@ export const registerIdentityKubernetesRouter = async (server: FastifyZodProvide}},handler: async (req) => {- const { identityKubernetesAuth, accessToken, identityAccessToken, identity } =- await server.services.identityKubernetesAuth.login(req.body);+ try {+ const { identityKubernetesAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityKubernetesAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_KUBERNETES_AUTH,- metadata: {- identityId: identityKubernetesAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityKubernetesAuthId: identityKubernetesAuth.id- }- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityKubernetesAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityKubernetesAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.KUBERNETES_AUTH+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_KUBERNETES_AUTH,+ metadata: {+ identityId: identityKubernetesAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityKubernetesAuthId: identityKubernetesAuth.id+ }}- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityKubernetesAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityKubernetesAuth.accessTokenTTL,- accessTokenMaxTTL: identityKubernetesAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityKubernetesAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityKubernetesAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.KUBERNETES_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityKubernetesAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityKubernetesAuth.accessTokenTTL,+ accessTokenMaxTTL: identityKubernetesAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_KUBERNETES_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-ldap-auth-router.ts b/backend/src/server/routes/v1/identity-ldap-auth-router.tsindex 2eb5d6f24f..0f981a9fd6 100644--- a/backend/src/server/routes/v1/identity-ldap-auth-router.ts+++ b/backend/src/server/routes/v1/identity-ldap-auth-router.ts@@ -25,7 +25,7 @@ import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { AllowedFieldsSchema } from "@app/services/identity-ldap-auth/identity-ldap-auth-types";import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";@@ -169,63 +169,82 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)}},handler: async (req, res) => {- const { identityId, username } = req.body;-- // Load LDAP config and attach to request for passport strategy- const { ldapConfig } = await server.services.identityLdapAuth.getLdapConfig(identityId);- req.ldapConfig = {- ...ldapConfig,- isActive: true,- groupSearchBase: "",- uniqueUserAttribute: "",- groupSearchFilter: ""- };-- // Authenticate with passport, wrapped in lockout protection- const { identityId: authIdentityId, user } = await server.services.identityLdapAuth.withLdapLockout(- { identityId, username },- () => authenticateWithPassport(req, res)- );-- const { accessToken, identityLdapAuth, identity } = await server.services.identityLdapAuth.login({- identityId: authIdentityId,- organizationSlug: req.body.organizationSlug- });-- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_LDAP_AUTH,- metadata: {- identityId: authIdentityId,- ldapEmail: user.mail,- ldapUsername: user.uid- }- }- });+ try {+ const { identityId, username } = req.body;++ // Load LDAP config and attach to request for passport strategy+ const { ldapConfig } = await server.services.identityLdapAuth.getLdapConfig(identityId);+ req.ldapConfig = {+ ...ldapConfig,+ isActive: true,+ groupSearchBase: "",+ uniqueUserAttribute: "",+ groupSearchFilter: ""+ };++ // Authenticate with passport, wrapped in lockout protection+ const { identityId: authIdentityId, user } = await server.services.identityLdapAuth.withLdapLockout(+ { identityId, username },+ () => authenticateWithPassport(req, res)+ );++ const { accessToken, identityLdapAuth, identity } = await server.services.identityLdapAuth.login({+ identityId: authIdentityId,+ organizationSlug: req.body.organizationSlug+ });- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${authIdentityId}`,- organizationId: identity.orgId,- properties: {- identityId: authIdentityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.LDAP_AUTH+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_LDAP_AUTH,+ metadata: {+ identityId: authIdentityId,+ ldapEmail: user.mail,+ ldapUsername: user.uid+ }}- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${authIdentityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityLdapAuth.accessTokenTTL,- accessTokenMaxTTL: identityLdapAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${authIdentityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: authIdentityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.LDAP_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${authIdentityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityLdapAuth.accessTokenTTL,+ accessTokenMaxTTL: identityLdapAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_LDAP_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-oci-auth-router.ts b/backend/src/server/routes/v1/identity-oci-auth-router.tsindex 76f3369fb2..10e978561e 100644--- a/backend/src/server/routes/v1/identity-oci-auth-router.ts+++ b/backend/src/server/routes/v1/identity-oci-auth-router.ts@@ -3,12 +3,13 @@ import { z } from "zod";import { IdentityAuthMethod, IdentityOciAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, OCI_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { validateTenancy, validateUsernames } from "@app/services/identity-oci-auth/identity-oci-auth-validators";import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";@@ -58,43 +59,62 @@ export const registerIdentityOciAuthRouter = async (server: FastifyZodProvider)}},handler: async (req) => {- const { identityOciAuth, accessToken, identityAccessToken, identity } =- await server.services.identityOciAuth.login(req.body);+ try {+ const { identityOciAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityOciAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_OCI_AUTH,- metadata: {- identityId: identityOciAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityOciAuthId: identityOciAuth.id- }- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityOciAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityOciAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.OCI_AUTH+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_OCI_AUTH,+ metadata: {+ identityId: identityOciAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityOciAuthId: identityOciAuth.id+ }}- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityOciAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityOciAuth.accessTokenTTL,- accessTokenMaxTTL: identityOciAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityOciAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityOciAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.OCI_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityOciAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityOciAuth.accessTokenTTL,+ accessTokenMaxTTL: identityOciAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_OCI_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-oidc-auth-router.ts b/backend/src/server/routes/v1/identity-oidc-auth-router.tsindex 17bd20bb56..f93cc0d143 100644--- a/backend/src/server/routes/v1/identity-oidc-auth-router.ts+++ b/backend/src/server/routes/v1/identity-oidc-auth-router.ts@@ -3,12 +3,13 @@ import { z } from "zod";import { IdentityAuthMethod, IdentityOidcAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, OIDC_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import {validateOidcAuthAudiencesField,@@ -65,47 +66,66 @@ export const registerIdentityOidcAuthRouter = async (server: FastifyZodProvider)}},handler: async (req) => {- const { identityOidcAuth, accessToken, identityAccessToken, identity, oidcTokenData } =- await server.services.identityOidcAuth.login(req.body);+ try {+ const { identityOidcAuth, accessToken, identityAccessToken, identity, oidcTokenData } =+ await server.services.identityOidcAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_OIDC_AUTH,- metadata: {- identityId: identityOidcAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityOidcAuthId: identityOidcAuth.id,- oidcClaimsReceived:- Buffer.from(JSON.stringify(oidcTokenData), "utf8").byteLength < MAX_OIDC_CLAIM_SIZE- ? oidcTokenData- : { payload: "Error: Payload exceeds 32KB, provided oidc claim not recorded in audit log." }+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_OIDC_AUTH,+ metadata: {+ identityId: identityOidcAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityOidcAuthId: identityOidcAuth.id,+ oidcClaimsReceived:+ Buffer.from(JSON.stringify(oidcTokenData), "utf8").byteLength < MAX_OIDC_CLAIM_SIZE+ ? oidcTokenData+ : { payload: "Error: Payload exceeds 32KB, provided oidc claim not recorded in audit log." }+ }}- }- });-- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityOidcAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityOidcAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.OIDC_AUTH- }- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityOidcAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityOidcAuth.accessTokenTTL,- accessTokenMaxTTL: identityOidcAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityOidcAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityOidcAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.OIDC_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityOidcAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityOidcAuth.accessTokenTTL,+ accessTokenMaxTTL: identityOidcAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_OIDC_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-spiffe-auth-router.ts b/backend/src/server/routes/v1/identity-spiffe-auth-router.tsindex f275197bd9..1ee87de00e 100644--- a/backend/src/server/routes/v1/identity-spiffe-auth-router.ts+++ b/backend/src/server/routes/v1/identity-spiffe-auth-router.ts@@ -3,10 +3,11 @@ import { z } from "zod";import { IdentitySpiffeAuthsSchema } from "@app/db/schemas";import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, SPIFFE_AUTH } from "@app/lib/api-docs";+import { UnauthorizedError } from "@app/lib/errors";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { SpiffeTrustBundleProfile } from "@app/services/identity-spiffe-auth/identity-spiffe-auth-types";import {@@ -129,27 +130,46 @@ export const registerIdentitySpiffeAuthRouter = async (server: FastifyZodProvide}},handler: async (req) => {- const { identitySpiffeAuth, accessToken, identityAccessToken, identity } =- await server.services.identitySpiffeAuth.login(req.body);+ try {+ const { identitySpiffeAuth, accessToken, identityAccessToken, identity } =+ await server.services.identitySpiffeAuth.login(req.body);- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_SPIFFE_AUTH,- metadata: {- identityId: identitySpiffeAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identitySpiffeAuthId: identitySpiffeAuth.id+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_SPIFFE_AUTH,+ metadata: {+ identityId: identitySpiffeAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identitySpiffeAuthId: identitySpiffeAuth.id+ }}+ });+ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identitySpiffeAuth.accessTokenTTL,+ accessTokenMaxTTL: identitySpiffeAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_SPIFFE_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });}- });- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identitySpiffeAuth.accessTokenTTL,- accessTokenMaxTTL: identitySpiffeAuth.accessTokenMaxTTL- };+ throw error;+ }}});diff --git a/backend/src/server/routes/v1/identity-tls-cert-auth-router.ts b/backend/src/server/routes/v1/identity-tls-cert-auth-router.tsindex 0a261189a4..a73709d92d 100644--- a/backend/src/server/routes/v1/identity-tls-cert-auth-router.ts+++ b/backend/src/server/routes/v1/identity-tls-cert-auth-router.ts@@ -5,13 +5,13 @@ import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { ApiDocsTags, TLS_CERT_AUTH } from "@app/lib/api-docs";import { getConfig } from "@app/lib/config/env";import { crypto } from "@app/lib/crypto/cryptography";-import { BadRequestError } from "@app/lib/errors";+import { BadRequestError, UnauthorizedError } from "@app/lib/errors";import { logger } from "@app/lib/logger";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";import { slugSchema } from "@app/server/lib/schemas";import { getTelemetryDistinctId } from "@app/server/lib/telemetry";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";-import { AuthMode } from "@app/services/auth/auth-type";+import { ActorType, AuthMode } from "@app/services/auth/auth-type";import { TIdentityTrustedIp } from "@app/services/identity/identity-types";import { isSuperAdmin } from "@app/services/super-admin/super-admin-fns";import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";@@ -64,52 +64,71 @@ export const registerIdentityTlsCertAuthRouter = async (server: FastifyZodProvid}},handler: async (req) => {- const appCfg = getConfig();- const clientCertificate = req.headers[appCfg.IDENTITY_TLS_CERT_AUTH_CLIENT_CERTIFICATE_HEADER_KEY];- if (!clientCertificate) {- throw new BadRequestError({ message: "Missing TLS certificate in header" });- }-- const { identityTlsCertAuth, accessToken, identityAccessToken, identity } =- await server.services.identityTlsCertAuth.login({- ...req.body,- clientCertificate: clientCertificate as string- });-- await server.services.auditLog.createAuditLog({- ...req.auditLogInfo,- orgId: identity.orgId,- event: {- type: EventType.LOGIN_IDENTITY_TLS_CERT_AUTH,- metadata: {- identityId: identityTlsCertAuth.identityId,- identityAccessTokenId: identityAccessToken.id,- identityTlsCertAuthId: identityTlsCertAuth.id- }+ try {+ const appCfg = getConfig();+ const clientCertificate = req.headers[appCfg.IDENTITY_TLS_CERT_AUTH_CLIENT_CERTIFICATE_HEADER_KEY];+ if (!clientCertificate) {+ throw new BadRequestError({ message: "Missing TLS certificate in header" });}- });- void server.services.telemetry- .sendPostHogEvents({- event: PostHogEventTypes.MachineIdentityLogin,- distinctId: `identity-${identityTlsCertAuth.identityId}`,- organizationId: identity.orgId,- properties: {- identityId: identityTlsCertAuth.identityId,- orgId: identity.orgId,- authMethod: IdentityAuthMethod.TLS_CERT_AUTH+ const { identityTlsCertAuth, accessToken, identityAccessToken, identity } =+ await server.services.identityTlsCertAuth.login({+ ...req.body,+ clientCertificate: clientCertificate as string+ });++ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ orgId: identity.orgId,+ event: {+ type: EventType.LOGIN_IDENTITY_TLS_CERT_AUTH,+ metadata: {+ identityId: identityTlsCertAuth.identityId,+ identityAccessTokenId: identityAccessToken.id,+ identityTlsCertAuthId: identityTlsCertAuth.id+ }}- })- .catch((error) => {- logger.error(error, `Failed to send telemetry event [identityId=${identityTlsCertAuth.identityId}]`);});- return {- accessToken,- tokenType: "Bearer" as const,- expiresIn: identityTlsCertAuth.accessTokenTTL,- accessTokenMaxTTL: identityTlsCertAuth.accessTokenMaxTTL- };+ void server.services.telemetry+ .sendPostHogEvents({+ event: PostHogEventTypes.MachineIdentityLogin,+ distinctId: `identity-${identityTlsCertAuth.identityId}`,+ organizationId: identity.orgId,+ properties: {+ identityId: identityTlsCertAuth.identityId,+ orgId: identity.orgId,+ authMethod: IdentityAuthMethod.TLS_CERT_AUTH+ }+ })+ .catch((error) => {+ logger.error(error, `Failed to send telemetry event [identityId=${identityTlsCertAuth.identityId}]`);+ });++ return {+ accessToken,+ tokenType: "Bearer" as const,+ expiresIn: identityTlsCertAuth.accessTokenTTL,+ accessTokenMaxTTL: identityTlsCertAuth.accessTokenMaxTTL+ };+ } catch (error) {+ if (error instanceof UnauthorizedError && error.detail?.orgId && error.detail?.identityId) {+ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ actor: { type: ActorType.UNKNOWN_USER, metadata: {} },+ orgId: error.detail.orgId as string,+ event: {+ type: EventType.LOGIN_IDENTITY_TLS_CERT_AUTH_FAILED,+ metadata: {+ identityId: error.detail.identityId as string,+ reason: error.detail.reason as string,+ message: error.message+ }+ }+ });+ }+ throw error;+ }}});diff --git a/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.ts b/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.tsindex 0c06ed8ab8..3b46faf9fd 100644--- a/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.ts+++ b/backend/src/services/identity-alicloud-auth/identity-alicloud-auth-service.ts@@ -77,7 +77,10 @@ export const identityAliCloudAuthServiceFactory = ({}const identity = await identityDAL.findById(identityAliCloudAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -103,7 +106,8 @@ export const identityAliCloudAuthServiceFactory = ({if (!isAccountAllowed)throw new UnauthorizedError({- message: "Access denied: Alibaba Cloud account ARN not allowed."+ message: "Access denied: Alibaba Cloud account ARN not allowed.",+ detail: { reason: "arn_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -123,7 +127,8 @@ export const identityAliCloudAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-service.tsindex 9d748dd5c2..02c572f61d 100644--- a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts@@ -115,7 +115,10 @@ export const identityAwsAuthServiceFactory = ({}const identity = await identityDAL.findById(identityAwsAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -159,7 +162,8 @@ export const identityAwsAuthServiceFactory = ({if (!isAccountAllowed)throw new UnauthorizedError({- message: "Access denied: AWS account ID not allowed."+ message: "Access denied: AWS account ID not allowed.",+ detail: { reason: "account_id_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -185,7 +189,8 @@ export const identityAwsAuthServiceFactory = ({);throw new UnauthorizedError({- message: `Access denied: AWS principal ARN not allowed. [principal-arn=${formattedArn}]`+ message: `Access denied: AWS principal ARN not allowed. [principal-arn=${formattedArn}]`,+ detail: { reason: "principal_arn_not_allowed", identityId: identity.id, orgId: identity.orgId }});}}@@ -206,7 +211,8 @@ export const identityAwsAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-azure-auth/identity-azure-auth-service.ts b/backend/src/services/identity-azure-auth/identity-azure-auth-service.tsindex a2296806be..11885c53a8 100644--- a/backend/src/services/identity-azure-auth/identity-azure-auth-service.ts+++ b/backend/src/services/identity-azure-auth/identity-azure-auth-service.ts@@ -71,7 +71,10 @@ export const identityAzureAuthServiceFactory = ({}const identity = await identityDAL.findById(identityAzureAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -87,7 +90,10 @@ export const identityAzureAuthServiceFactory = ({});if (azureIdentity.tid !== identityAzureAuth.tenantId)- throw new UnauthorizedError({ message: "Tenant ID mismatch" });+ throw new UnauthorizedError({+ message: "Tenant ID mismatch",+ detail: { reason: "tenant_id_mismatch", identityId: identity.id, orgId: identity.orgId }+ });if (identityAzureAuth.allowedServicePrincipalIds) {// validate if the service principal id is in the list of allowed service principal ids@@ -98,7 +104,10 @@ export const identityAzureAuthServiceFactory = ({.some((servicePrincipalId) => servicePrincipalId === azureIdentity.oid);if (!isServicePrincipalAllowed) {- throw new UnauthorizedError({ message: `Service principal '${azureIdentity.oid}' not allowed` });+ throw new UnauthorizedError({+ message: `Service principal '${azureIdentity.oid}' not allowed`,+ detail: { reason: "service_principal_not_allowed", identityId: identity.id, orgId: identity.orgId }+ });}}@@ -118,7 +127,8 @@ export const identityAzureAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.tsindex 9e9613dd71..55b5a319d2 100644--- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts@@ -69,7 +69,10 @@ export const identityGcpAuthServiceFactory = ({}const identity = await identityDAL.findById(identityGcpAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -109,7 +112,8 @@ export const identityGcpAuthServiceFactory = ({if (!isServiceAccountAllowed)throw new UnauthorizedError({- message: "Access denied: GCP service account not allowed."+ message: "Access denied: GCP service account not allowed.",+ detail: { reason: "service_account_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -127,7 +131,8 @@ export const identityGcpAuthServiceFactory = ({if (!isProjectAllowed)throw new UnauthorizedError({- message: "Access denied: GCP project not allowed."+ message: "Access denied: GCP project not allowed.",+ detail: { reason: "project_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -139,7 +144,8 @@ export const identityGcpAuthServiceFactory = ({if (!isZoneAllowed)throw new UnauthorizedError({- message: "Access denied: GCP zone not allowed."+ message: "Access denied: GCP zone not allowed.",+ detail: { reason: "zone_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -159,7 +165,8 @@ export const identityGcpAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.ts b/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.tsindex 9477d08868..87babe43f2 100644--- a/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.ts+++ b/backend/src/services/identity-jwt-auth/identity-jwt-auth-service.ts@@ -83,7 +83,10 @@ export const identityJwtAuthServiceFactory = ({}const identity = await identityDAL.findById(identityJwtAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -100,7 +103,8 @@ export const identityJwtAuthServiceFactory = ({const decodedToken = crypto.jwt().decode(jwtValue, { complete: true });if (!decodedToken) {throw new UnauthorizedError({- message: "Invalid JWT"+ message: "Invalid JWT",+ detail: { reason: "invalid_jwt", identityId: identity.id, orgId: identity.orgId }});}@@ -132,7 +136,8 @@ export const identityJwtAuthServiceFactory = ({} catch (error) {if (error instanceof jwt.JsonWebTokenError) {throw new UnauthorizedError({- message: `Access denied: ${error.message}`+ message: `Access denied: ${error.message}`,+ detail: { reason: "jwt_verification_failed", identityId: identity.id, orgId: identity.orgId }});}@@ -158,7 +163,8 @@ export const identityJwtAuthServiceFactory = ({if (!isMatchAnyKey) {throw new UnauthorizedError({- message: `Access denied: JWT verification failed with all keys. Errors - ${errors.join("; ")}`+ message: `Access denied: JWT verification failed with all keys. Errors - ${errors.join("; ")}`,+ detail: { reason: "jwt_verification_failed", identityId: identity.id, orgId: identity.orgId }});}}@@ -174,7 +180,8 @@ export const identityJwtAuthServiceFactory = ({if (identityJwtAuth.boundSubject) {if (!tokenData.sub) {throw new UnauthorizedError({- message: "Access denied: token has no subject field"+ message: "Access denied: token has no subject field",+ detail: { reason: "missing_subject", identityId: identity.id, orgId: identity.orgId }});}@@ -188,7 +195,8 @@ export const identityJwtAuthServiceFactory = ({if (identityJwtAuth.boundAudiences) {if (!tokenData.aud) {throw new UnauthorizedError({- message: "Access denied: token has no audience field"+ message: "Access denied: token has no audience field",+ detail: { reason: "missing_audience", identityId: identity.id, orgId: identity.orgId }});}@@ -198,7 +206,8 @@ export const identityJwtAuthServiceFactory = ({.some((policyValue) => doesFieldValueMatchJwtPolicy(tokenData.aud, policyValue))) {throw new UnauthorizedError({- message: "Access denied: token audience not allowed"+ message: "Access denied: token audience not allowed",+ detail: { reason: "audience_not_allowed", identityId: identity.id, orgId: identity.orgId }});}}@@ -210,14 +219,16 @@ export const identityJwtAuthServiceFactory = ({if (!value) {throw new UnauthorizedError({- message: `Access denied: token has no ${claimKey} field`+ message: `Access denied: token has no ${claimKey} field`,+ detail: { reason: "missing_claim", identityId: identity.id, orgId: identity.orgId }});}// handle both single and multi-valued claimsif (!claimValue.split(", ").some((claimEntry) => doesFieldValueMatchJwtPolicy(value, claimEntry))) {throw new UnauthorizedError({- message: `Access denied: claim mismatch for field ${claimKey}`+ message: `Access denied: claim mismatch for field ${claimKey}`,+ detail: { reason: "claim_mismatch", identityId: identity.id, orgId: identity.orgId }});}});@@ -239,7 +250,8 @@ export const identityJwtAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.tsindex 00b603152d..64ee967b7d 100644--- a/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts+++ b/backend/src/services/identity-kubernetes-auth/identity-kubernetes-auth-service.ts@@ -258,7 +258,10 @@ export const identityKubernetesAuthServiceFactory = ({}const identity = await identityDAL.findById(identityKubernetesAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -485,13 +488,18 @@ export const identityKubernetesAuthServiceFactory = ({}if ("error" in data.status)- throw new UnauthorizedError({ message: data.status.error, name: "KubernetesTokenReviewError" });+ throw new UnauthorizedError({+ message: data.status.error,+ name: "KubernetesTokenReviewError",+ detail: { reason: "token_review_error", identityId: identity.id, orgId: identity.orgId }+ });// check the response to determine if the token is validif (!(data.status && data.status.authenticated))throw new UnauthorizedError({message: "Kubernetes token not authenticated",- name: "KubernetesTokenReviewError"+ name: "KubernetesTokenReviewError",+ detail: { reason: "token_not_authenticated", identityId: identity.id, orgId: identity.orgId }});const { namespace: targetNamespace, name: targetName } = extractK8sUsername(data.status.user.username);@@ -506,7 +514,8 @@ export const identityKubernetesAuthServiceFactory = ({if (!isNamespaceAllowed)throw new UnauthorizedError({- message: "Access denied: K8s namespace not allowed."+ message: "Access denied: K8s namespace not allowed.",+ detail: { reason: "namespace_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -520,7 +529,8 @@ export const identityKubernetesAuthServiceFactory = ({if (!isNameAllowed)throw new UnauthorizedError({- message: "Access denied: K8s name not allowed."+ message: "Access denied: K8s name not allowed.",+ detail: { reason: "name_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -532,7 +542,8 @@ export const identityKubernetesAuthServiceFactory = ({if (!isAudienceAllowed)throw new UnauthorizedError({- message: "Access denied: K8s audience not allowed."+ message: "Access denied: K8s audience not allowed.",+ detail: { reason: "audience_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -552,7 +563,8 @@ export const identityKubernetesAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.tsindex fb0c1a6151..9541da846b 100644--- a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts+++ b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts@@ -164,7 +164,10 @@ export const identityLdapAuthServiceFactory = ({}const identity = await identityDAL.findById(identityLdapAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -195,7 +198,8 @@ export const identityLdapAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}@@ -848,6 +852,9 @@ export const identityLdapAuthServiceFactory = ({): Promise<T> => {const usernameSlug = slugify(username.trim().toLowerCase());+ const identity = await identityDAL.findById(identityId);+ const orgId = identity?.orgId ?? null;+const LOCKOUT_KEY = `lockout:identity:${identityId}:${IdentityAuthMethod.LDAP_AUTH}:${usernameSlug}`;const lockoutRaw = await keyStore.getItem(LOCKOUT_KEY);@@ -859,7 +866,8 @@ export const identityLdapAuthServiceFactory = ({if (lockout && lockout?.lockedOut) {throw new UnauthorizedError({- message: "This identity auth method is temporarily locked, please try again later"+ message: "This identity auth method is temporarily locked, please try again later",+ detail: { reason: "temporarily_locked", identityId, orgId }});}@@ -877,7 +885,10 @@ export const identityLdapAuthServiceFactory = ({if ((error as any).status === 401 || error instanceof UnauthorizedError) {const identityLdapAuth = await identityLdapAuthDAL.findOne({ identityId });if (!identityLdapAuth) {- throw new UnauthorizedError({ message: "Invalid credentials" });+ throw new UnauthorizedError({+ message: "Invalid credentials",+ detail: { reason: "ldap_auth_not_found", identityId, orgId }+ });}if (identityLdapAuth.lockoutEnabled) {@@ -902,7 +913,8 @@ export const identityLdapAuthServiceFactory = ({if (lockout.lockedOut) {throw new UnauthorizedError({- message: "This identity auth method is temporarily locked, please try again later"+ message: "This identity auth method is temporarily locked, please try again later",+ detail: { reason: "temporarily_locked", identityId, orgId }});}@@ -931,7 +943,10 @@ export const identityLdapAuthServiceFactory = ({}}- throw new UnauthorizedError({ message: "Invalid credentials" });+ throw new UnauthorizedError({+ message: "Invalid credentials",+ detail: { reason: "invalid_credentials", identityId, orgId }+ });}throw error;}diff --git a/backend/src/services/identity-oci-auth/identity-oci-auth-service.ts b/backend/src/services/identity-oci-auth/identity-oci-auth-service.tsindex 304e4f66d3..c57df66c8e 100644--- a/backend/src/services/identity-oci-auth/identity-oci-auth-service.ts+++ b/backend/src/services/identity-oci-auth/identity-oci-auth-service.ts@@ -73,7 +73,10 @@ export const identityOciAuthServiceFactory = ({}const identity = await identityDAL.findById(identityOciAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -100,7 +103,8 @@ export const identityOciAuthServiceFactory = ({if (data.compartmentId !== identityOciAuth.tenancyOcid) {throw new UnauthorizedError({- message: "Access denied: OCI account isn't part of tenancy."+ message: "Access denied: OCI account isn't part of tenancy.",+ detail: { reason: "tenancy_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -109,7 +113,8 @@ export const identityOciAuthServiceFactory = ({if (!isAccountAllowed)throw new UnauthorizedError({- message: "Access denied: OCI account username not allowed."+ message: "Access denied: OCI account username not allowed.",+ detail: { reason: "username_not_allowed", identityId: identity.id, orgId: identity.orgId }});}@@ -129,7 +134,8 @@ export const identityOciAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.ts b/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.tsindex d4f01c9b21..74546be219 100644--- a/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.ts+++ b/backend/src/services/identity-oidc-auth/identity-oidc-auth-service.ts@@ -84,7 +84,10 @@ export const identityOidcAuthServiceFactory = ({}const identity = await identityDAL.findById(identityOidcAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -116,21 +119,24 @@ export const identityOidcAuthServiceFactory = ({discoveryDoc = response.data;} catch (error) {throw new UnauthorizedError({- message: `Access denied: Failed to fetch OIDC discovery document from ${identityOidcAuth.oidcDiscoveryUrl}. ${error instanceof Error ? error.message : String(error)}`+ message: `Access denied: Failed to fetch OIDC discovery document from ${identityOidcAuth.oidcDiscoveryUrl}. ${error instanceof Error ? error.message : String(error)}`,+ detail: { reason: "discovery_document_fetch_failed", identityId: identity.id, orgId: identity.orgId }});}const jwksUri = discoveryDoc.jwks_uri;if (!jwksUri) {throw new UnauthorizedError({- message: `Access denied: OIDC discovery document does not contain a jwks_uri. The identity provider may be misconfigured.`+ message: `Access denied: OIDC discovery document does not contain a jwks_uri. The identity provider may be misconfigured.`,+ detail: { reason: "missing_jwks_uri", identityId: identity.id, orgId: identity.orgId }});}const decodedToken = crypto.jwt().decode(oidcJwt, { complete: true });if (!decodedToken) {throw new UnauthorizedError({- message: "Invalid JWT"+ message: "Invalid JWT",+ detail: { reason: "invalid_jwt", identityId: identity.id, orgId: identity.orgId }});}@@ -151,11 +157,13 @@ export const identityOidcAuthServiceFactory = ({} catch (error) {if (error instanceof Error && error.name === "SigningKeyNotFoundError") {throw new UnauthorizedError({- message: `Access denied: Unable to verify JWT signature. The signing key '${kid}' was not found in the OIDC provider's JWKS endpoint. This may indicate an invalid token or misconfigured OIDC provider.`+ message: `Access denied: Unable to verify JWT signature. The signing key '${kid}' was not found in the OIDC provider's JWKS endpoint. This may indicate an invalid token or misconfigured OIDC provider.`,+ detail: { reason: "signing_key_not_found", identityId: identity.id, orgId: identity.orgId }});}throw new UnauthorizedError({- message: `Access denied: Failed to retrieve signing key from OIDC provider: ${error instanceof Error ? error.message : String(error)}`+ message: `Access denied: Failed to retrieve signing key from OIDC provider: ${error instanceof Error ? error.message : String(error)}`,+ detail: { reason: "signing_key_retrieval_failed", identityId: identity.id, orgId: identity.orgId }});}@@ -166,7 +174,8 @@ export const identityOidcAuthServiceFactory = ({} catch (error) {if (error instanceof jwt.JsonWebTokenError) {throw new UnauthorizedError({- message: `Access denied: ${error.message}`+ message: `Access denied: ${error.message}`,+ detail: { reason: "jwt_verification_failed", identityId: identity.id, orgId: identity.orgId }});}throw error;@@ -182,13 +191,15 @@ export const identityOidcAuthServiceFactory = ({allSigningKeys = await client.getSigningKeys();} catch (error) {throw new UnauthorizedError({- message: `Access denied: Failed to retrieve signing keys from OIDC provider: ${error instanceof Error ? error.message : String(error)}`+ message: `Access denied: Failed to retrieve signing keys from OIDC provider: ${error instanceof Error ? error.message : String(error)}`,+ detail: { reason: "signing_keys_retrieval_failed", identityId: identity.id, orgId: identity.orgId }});}if (!allSigningKeys || allSigningKeys.length === 0) {throw new UnauthorizedError({- message: "Access denied: No signing keys available from OIDC provider's JWKS endpoint."+ message: "Access denied: No signing keys available from OIDC provider's JWKS endpoint.",+ detail: { reason: "no_signing_keys", identityId: identity.id, orgId: identity.orgId }});}@@ -196,7 +207,8 @@ export const identityOidcAuthServiceFactory = ({const MAX_KEYS_TO_TRY = 10;if (allSigningKeys.length > MAX_KEYS_TO_TRY) {throw new UnauthorizedError({- message: `Access denied: OIDC provider has ${allSigningKeys.length} signing keys. Tokens must include 'kid' header when provider has more than ${MAX_KEYS_TO_TRY} keys.`+ message: `Access denied: OIDC provider has ${allSigningKeys.length} signing keys. Tokens must include 'kid' header when provider has more than ${MAX_KEYS_TO_TRY} keys.`,+ detail: { reason: "too_many_signing_keys", identityId: identity.id, orgId: identity.orgId }});}@@ -223,7 +235,8 @@ export const identityOidcAuthServiceFactory = ({if (!verified) {throw new UnauthorizedError({- message: `Access denied: Unable to verify JWT signature with any available signing key. ${lastError ? lastError.message : "Invalid token"}`+ message: `Access denied: Unable to verify JWT signature with any available signing key. ${lastError ? lastError.message : "Invalid token"}`,+ detail: { reason: "jwt_verification_failed", identityId: identity.id, orgId: identity.orgId }});}}@@ -231,7 +244,8 @@ export const identityOidcAuthServiceFactory = ({// Ensure tokenData was successfully assignedif (!tokenData) {throw new UnauthorizedError({- message: "Access denied: Failed to verify JWT token"+ message: "Access denied: Failed to verify JWT token",+ detail: { reason: "jwt_verification_failed", identityId: identity.id, orgId: identity.orgId }});}@@ -252,7 +266,8 @@ export const identityOidcAuthServiceFactory = ({.some((policyValue) => doesAudValueMatchOidcPolicy(verifiedTokenData.aud, policyValue))) {throw new UnauthorizedError({- message: "Access denied: OIDC audience not allowed."+ message: "Access denied: OIDC audience not allowed.",+ detail: { reason: "audience_not_allowed", identityId: identity.id, orgId: identity.orgId }});}}@@ -264,14 +279,16 @@ export const identityOidcAuthServiceFactory = ({if (!value) {throw new UnauthorizedError({- message: `Access denied: token has no ${claimKey} field`+ message: `Access denied: token has no ${claimKey} field`,+ detail: { reason: "missing_claim", identityId: identity.id, orgId: identity.orgId }});}// handle both single and multi-valued claimsif (!claimValue.split(", ").some((claimEntry) => doesFieldValueMatchOidcPolicy(value, claimEntry))) {throw new UnauthorizedError({- message: "Access denied: OIDC claim not allowed."+ message: "Access denied: OIDC claim not allowed.",+ detail: { reason: "claim_not_allowed", identityId: identity.id, orgId: identity.orgId }});}});@@ -284,7 +301,8 @@ export const identityOidcAuthServiceFactory = ({const value = getValueByDot(verifiedTokenData, claimKey);if (!value) {throw new UnauthorizedError({- message: `Access denied: token has no ${claimKey} field`+ message: `Access denied: token has no ${claimKey} field`,+ detail: { reason: "missing_metadata_claim", identityId: identity.id, orgId: identity.orgId }});}filteredClaims[permissionKey] = value.toString();@@ -307,7 +325,8 @@ export const identityOidcAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.ts b/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.tsindex 372f07e22f..324f0c2574 100644--- a/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.ts+++ b/backend/src/services/identity-spiffe-auth/identity-spiffe-auth-service.ts@@ -291,7 +291,10 @@ export const identitySpiffeAuthServiceFactory = ({}const identity = await identityDAL.findById(identitySpiffeAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -317,14 +320,35 @@ export const identitySpiffeAuthServiceFactory = ({orgId: identity.orgId,forceRefresh: true}));- tokenData = await verifyJwtSvid(jwtValue, jwksJson, allowedAudiences);+ try {+ tokenData = await verifyJwtSvid(jwtValue, jwksJson, allowedAudiences);+ } catch (retryError) {+ if (retryError instanceof UnauthorizedError) {+ retryError.detail = {+ reason: "jwt_svid_verification_failed",+ identityId: identity.id,+ orgId: identity.orgId+ };+ }+ throw retryError;+ }} else {+ if (verifyError instanceof UnauthorizedError) {+ verifyError.detail = {+ reason: "jwt_svid_verification_failed",+ identityId: identity.id,+ orgId: identity.orgId+ };+ }throw verifyError;}}if (!validateSpiffeClaims(tokenData, identitySpiffeAuth)) {- throw new UnauthorizedError({ message: "Access denied" });+ throw new UnauthorizedError({+ message: "Access denied",+ detail: { reason: "spiffe_claims_invalid", identityId: identity.id, orgId: identity.orgId }+ });}// Sub-org resolution@@ -343,7 +367,8 @@ export const identitySpiffeAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.tsindex 751a82e2f2..64c168eebd 100644--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts@@ -82,7 +82,10 @@ export const identityTlsCertAuthServiceFactory = ({}const identity = await identityDAL.findById(identityTlsCertAuth.identityId);- if (!identity) throw new UnauthorizedError({ message: "Identity not found" });+ if (!identity)+ throw new UnauthorizedError({+ message: "Identity not found"+ });const org = await orgDAL.findById(identity.orgId);const isSubOrgIdentity = Boolean(org.rootOrgId);@@ -111,18 +114,21 @@ export const identityTlsCertAuthServiceFactory = ({const isValidCertificate = clientCertificateX509.verify(caCertificateX509.publicKey);if (!isValidCertificate)throw new UnauthorizedError({- message: "Access denied: Certificate not issued by the provided CA."+ message: "Access denied: Certificate not issued by the provided CA.",+ detail: { reason: "ca_verification_failed", identityId: identity.id, orgId: identity.orgId }});if (new Date(clientCertificateX509.validTo) < new Date()) {throw new UnauthorizedError({- message: "Access denied: Certificate has expired."+ message: "Access denied: Certificate has expired.",+ detail: { reason: "certificate_expired", identityId: identity.id, orgId: identity.orgId }});}if (new Date(clientCertificateX509.validFrom) > new Date()) {throw new UnauthorizedError({- message: "Access denied: Certificate not yet valid."+ message: "Access denied: Certificate not yet valid.",+ detail: { reason: "certificate_not_yet_valid", identityId: identity.id, orgId: identity.orgId }});}@@ -131,7 +137,8 @@ export const identityTlsCertAuthServiceFactory = ({const isValidCommonName = identityTlsCertAuth.allowedCommonNames.split(",").includes(subjectDetails.CN);if (!isValidCommonName) {throw new UnauthorizedError({- message: "Access denied: TLS Certificate Auth common name not allowed."+ message: "Access denied: TLS Certificate Auth common name not allowed.",+ detail: { reason: "common_name_not_allowed", identityId: identity.id, orgId: identity.orgId }});}}@@ -152,7 +159,8 @@ export const identityTlsCertAuthServiceFactory = ({if (!subOrgMembership) {throw new UnauthorizedError({- message: `Identity not authorized to access sub organization ${organizationSlug}`+ message: `Identity not authorized to access sub organization ${organizationSlug}`,+ detail: { reason: "sub_org_unauthorized", identityId: identity.id, orgId: identity.orgId }});}