AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
feat(audit-logs): record every sign-in with the method used (#17061)
lina-alomari5f91e7d7ec96
diff --git a/docs/install/reference/breaking-changes-unreleased.mdx b/docs/install/reference/breaking-changes-unreleased.mdxindex b0026d203e5..029594e3366 100644--- a/docs/install/reference/breaking-changes-unreleased.mdx+++ b/docs/install/reference/breaking-changes-unreleased.mdx@@ -8,6 +8,15 @@ Entries here move to [Breaking Changes](/install/reference/breaking-changes) aut## What has changed?+#### Audit logs: SAML and embedded sign-ins no longer send `user.signed.up` on every sign-in++`user.signed.up` used to fire on every SAML and embedded (JWT) sign-in, even for existing members. It now fires once, when the member is created. Later SAML sign-ins, and sign-ins with email or Google, send the new `user.signed.in` event, which carries the sign-in method. Later embedded sign-ins send no event.++#### What you need to do++- If an Event Streaming destination used `user.signed.up` to track SAML logins, subscribe it to `user.signed.in` as well.+- Embedded logins of existing members are no longer streamed. Track them in your own app, which issues the JWT.+#### RabbitMQ 0.2.0: the Send Message actions are no longer AI agent tools, and their output changes**Send Message to Queue** and **Send Message to Exchange** (shown before as `sendMessageToQueue` and `sendMessageToExchange`) are now shown only in the flow builder. AI agents and MCP clients get **Publish to Queue** and **Publish to Exchange** instead.diff --git a/packages/core/shared/package.json b/packages/core/shared/package.jsonindex f981ca55b1d..44c2967ebad 100644--- a/packages/core/shared/package.json+++ b/packages/core/shared/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/shared",- "version": "0.213.0",+ "version": "0.214.0","type": "commonjs","sideEffects": false,"main": "./dist/src/index.js",diff --git a/packages/core/shared/src/lib/ee/audit-events/index.ts b/packages/core/shared/src/lib/ee/audit-events/index.tsindex b1799ea0050..2557c23ab40 100644--- a/packages/core/shared/src/lib/ee/audit-events/index.ts+++ b/packages/core/shared/src/lib/ee/audit-events/index.ts@@ -2,6 +2,7 @@ import { AgentRunSource, Flow, FlowOperationRequest, FlowOperationType, FlowVersimport { BaseModelSchema, DateOrString, isNil, Nullable, OptionalArrayFromQuery, ProjectRole } from '@activepieces/core-utils'import { z } from 'zod'import * as zMini from 'zod/mini'+import { UserIdentityProvider } from '../../core/authentication/user-identity'import { UserWithMetaInformation } from '../../core/user/user'import { SigningKey } from '../signing-key'export const ListAuditEventsRequest = z.object({@@ -477,7 +478,6 @@ const AuthenticationEventData = z.object({export const AuthenticationEvent = z.object({...BaseAuditEventProps,action: z.union([- z.literal(ApplicationEventName.USER_SIGNED_IN),z.literal(ApplicationEventName.USER_PASSWORD_RESET),z.literal(ApplicationEventName.USER_EMAIL_VERIFIED),]),@@ -489,7 +489,9 @@ export type AuthenticationEvent = z.infer<typeof AuthenticationEvent>export const UserSignedInEvent = z.object({...BaseAuditEventProps,action: z.literal(ApplicationEventName.USER_SIGNED_IN),- data: AuthenticationEventData,+ data: AuthenticationEventData.extend({+ method: z.enum(UserIdentityProvider).optional(),+ }),})export type UserSignedInEvent = z.infer<typeof UserSignedInEvent>@@ -515,7 +517,8 @@ export const SignUpEvent = z.object({z.literal('credentials'),z.literal('sso'),z.literal('managed'),- ]),+ ]).optional(),+ method: z.enum(UserIdentityProvider).optional(),user: UserMeta.optional(),}),})@@ -697,6 +700,7 @@ export const ApplicationEvent = z.union([FlowDeactivatedEvent,FlowRunEvent,AuthenticationEvent,+ UserSignedInEvent,FolderEvent,SignUpEvent,SigningKeyEvent,@@ -779,13 +783,13 @@ export function summarizeApplicationEvent(event: ApplicationEvent) {case ApplicationEventName.VARIABLE_VALUE_REVEALED:return `Variable ${event.data.variable.name} value was revealed`case ApplicationEventName.USER_SIGNED_IN:- return `User ${event.userEmail} signed in`+ return `User ${event.userEmail} signed in${describeMethod(event.data.method)}`case ApplicationEventName.USER_PASSWORD_RESET:return `User ${event.userEmail} reset password`case ApplicationEventName.USER_EMAIL_VERIFIED:return `User ${event.userEmail} verified email`case ApplicationEventName.USER_SIGNED_UP:- return `User ${event.userEmail} signed up using email from ${event.data.source}`+ return summarizeSignUp(event)case ApplicationEventName.SIGNING_KEY_CREATED:return `${event.data.signingKey.displayName} is created`case ApplicationEventName.AUDIT_LOG_RETENTION_UPDATED: {@@ -955,6 +959,27 @@ function describeAuditLogRetentionPeriod(days: number): string {return days === 1 ? '1 day' : `${days} days`}+function summarizeSignUp(event: SignUpEvent): string {+ if (!isNil(event.data.method)) {+ return `User ${event.userEmail} signed up${describeMethod(event.data.method)}`+ }+ if (!isNil(event.data.source)) {+ return `User ${event.userEmail} signed up using email from ${event.data.source}`+ }+ return `User ${event.userEmail} signed up`+}++function describeMethod(method: UserIdentityProvider | undefined): string {+ return isNil(method) ? '' : ` with ${METHOD_PHRASE[method]}`+}++const METHOD_PHRASE: Record<UserIdentityProvider, string> = {+ [UserIdentityProvider.EMAIL]: 'email',+ [UserIdentityProvider.GOOGLE]: 'Google',+ [UserIdentityProvider.SAML]: 'SAML',+ [UserIdentityProvider.JWT]: 'JWT',+}+export const AUDIT_LOG_RETENTION_MIN_DAYS = 30export const AUDIT_LOG_RETENTION_MAX_DAYS = 3650export const AUDIT_LOG_RETENTION_BACKLOG_GRACE_DAYS = 2diff --git a/packages/core/shared/src/lib/ee/audit-events/mock-event-builder.ts b/packages/core/shared/src/lib/ee/audit-events/mock-event-builder.tsindex 8f34c875147..3a1e4f26736 100644--- a/packages/core/shared/src/lib/ee/audit-events/mock-event-builder.ts+++ b/packages/core/shared/src/lib/ee/audit-events/mock-event-builder.ts@@ -1,5 +1,6 @@import { AgentRunSource, FlowOperationType, FlowStatus } from '@activepieces/core-execution'import { apId, PlatformId, ProjectId } from '@activepieces/core-utils'+import { UserIdentityProvider } from '../../core/authentication/user-identity'import {AgentActionExecutedEvent,AgentActionKind,@@ -28,6 +29,7 @@ import {ProjectRoleEvent,SigningKeyEvent,SignUpEvent,+ UserSignedInEvent,VariableEvent,} from './index'@@ -263,7 +265,14 @@ export const buildMockEvent = ({ event, platformId, projectId }: BuildMockEventP}return mock}- case ApplicationEventName.USER_SIGNED_IN:+ case ApplicationEventName.USER_SIGNED_IN: {+ const mock: UserSignedInEvent = {+ ...baseEnvelope,+ action: ApplicationEventName.USER_SIGNED_IN,+ data: { user, method: UserIdentityProvider.EMAIL },+ }+ return mock+ }case ApplicationEventName.USER_PASSWORD_RESET:case ApplicationEventName.USER_EMAIL_VERIFIED: {const mock: AuthenticationEvent = {@@ -277,7 +286,7 @@ export const buildMockEvent = ({ event, platformId, projectId }: BuildMockEventPconst mock: SignUpEvent = {...baseEnvelope,action: ApplicationEventName.USER_SIGNED_UP,- data: { source: 'credentials', user },+ data: { source: 'credentials', method: UserIdentityProvider.EMAIL, user },}return mock}diff --git a/packages/core/shared/test/ee/sign-in-summary.test.ts b/packages/core/shared/test/ee/sign-in-summary.test.tsnew file mode 100644index 00000000000..65721807b54--- /dev/null+++ b/packages/core/shared/test/ee/sign-in-summary.test.ts@@ -0,0 +1,32 @@+import { describe, expect, it } from 'vitest'+import { ApplicationEventName, buildMockEvent, SignUpEvent, summarizeApplicationEvent, UserIdentityProvider, UserSignedInEvent } from '../../src/index'++function signedInEvent(data: UserSignedInEvent['data']): UserSignedInEvent {+ const mock = UserSignedInEvent.parse(buildMockEvent({ event: ApplicationEventName.USER_SIGNED_IN, platformId: 'platform-id' }))+ return { ...mock, userEmail: '[email protected]', data }+}++function signedUpEvent(data: SignUpEvent['data']): SignUpEvent {+ const mock = SignUpEvent.parse(buildMockEvent({ event: ApplicationEventName.USER_SIGNED_UP, platformId: 'platform-id' }))+ return { ...mock, userEmail: '[email protected]', data }+}++describe('summarizes sign-in and sign-up rows with their method', () => {+ it.each([+ { data: { method: UserIdentityProvider.EMAIL }, expected: 'User [email protected] signed in with email' },+ { data: { method: UserIdentityProvider.GOOGLE }, expected: 'User [email protected] signed in with Google' },+ { data: { method: UserIdentityProvider.SAML }, expected: 'User [email protected] signed in with SAML' },+ { data: {}, expected: 'User [email protected] signed in' },+ ])('describes a sign-in with $data as "$expected"', ({ data, expected }) => {+ expect(summarizeApplicationEvent(signedInEvent(data))).toBe(expected)+ })++ it.each([+ { data: { method: UserIdentityProvider.GOOGLE, source: 'sso' as const }, expected: 'User [email protected] signed up with Google' },+ { data: { method: UserIdentityProvider.JWT, source: 'managed' as const }, expected: 'User [email protected] signed up with JWT' },+ { data: { source: 'sso' as const }, expected: 'User [email protected] signed up using email from sso' },+ { data: {}, expected: 'User [email protected] signed up' },+ ])('describes a sign-up with $data as "$expected"', ({ data, expected }) => {+ expect(summarizeApplicationEvent(signedUpEvent(data))).toBe(expected)+ })+})diff --git a/packages/server/api/src/app/authentication/authentication-side-effects.ts b/packages/server/api/src/app/authentication/authentication-side-effects.tsnew file mode 100644index 00000000000..b1f495ebbd5--- /dev/null+++ b/packages/server/api/src/app/authentication/authentication-side-effects.ts@@ -0,0 +1,34 @@+import { isNil } from '@activepieces/core-utils'+import { ApplicationEventName, AuthenticationResponse, UserIdentityProvider } from '@activepieces/shared'+import { FastifyBaseLogger } from 'fastify'+import { applicationEvents } from '../helper/application-events'++export const authenticationSideEffects = (log: FastifyBaseLogger) => ({+ onSignIn({ response, signedUp, method, ip }: OnSignInParams): void {+ if (isNil(response.platformId)) {+ return+ }+ applicationEvents(log).sendUserEvent({+ platformId: response.platformId,+ userId: response.id,+ projectId: response.projectId ?? undefined,+ ip,+ }, signedUp+ ? { action: ApplicationEventName.USER_SIGNED_UP, data: { method, source: LEGACY_SIGN_UP_SOURCE[method] } }+ : { action: ApplicationEventName.USER_SIGNED_IN, data: { method } })+ },+})++const LEGACY_SIGN_UP_SOURCE: Record<UserIdentityProvider, 'credentials' | 'sso' | 'managed'> = {+ [UserIdentityProvider.EMAIL]: 'credentials',+ [UserIdentityProvider.GOOGLE]: 'sso',+ [UserIdentityProvider.SAML]: 'sso',+ [UserIdentityProvider.JWT]: 'managed',+}++type OnSignInParams = {+ response: AuthenticationResponse+ signedUp: boolean+ method: UserIdentityProvider+ ip: string+}diff --git a/packages/server/api/src/app/authentication/authentication.controller.ts b/packages/server/api/src/app/authentication/authentication.controller.tsindex 72f3d54c81b..cf97aa94fa1 100644--- a/packages/server/api/src/app/authentication/authentication.controller.ts+++ b/packages/server/api/src/app/authentication/authentication.controller.ts@@ -1,10 +1,9 @@import { isNil } from '@activepieces/core-utils'-import { ApplicationEventName, attributionUtils, CompleteSignUpRequest, PrincipalType, SignInRequest, SignUpMethod, SignUpRequest, SwitchPlatformRequest, TelemetryEventName, UserIdentityProvider } from '@activepieces/shared'+import { attributionUtils, CompleteSignUpRequest, PrincipalType, SignInRequest, SignUpMethod, SignUpRequest, SwitchPlatformRequest, TelemetryEventName, UserIdentityProvider } from '@activepieces/shared'import { FastifyRequest } from 'fastify'import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'import { securityAccess } from '../core/security/authorization/fastify-security'import { authnRateLimit } from '../core/security/rate-limit'-import { applicationEvents } from '../helper/application-events'import { networkUtils } from '../helper/network-utils'import { rejectedPromiseHandler } from '../helper/promise-handler'import { system } from '../helper/system/system'@@ -12,6 +11,7 @@ import { AppSystemProp } from '../helper/system/system-props'import { telemetry } from '../helper/telemetry.utils'import { platformUtils } from '../platform/platform.utils'import { userService } from '../user/user-service'+import { authenticationSideEffects } from './authentication-side-effects'import { authenticationService } from './authentication.service'import { turnstile } from './lib/turnstile'import { passwordlessAuthService } from './passwordless-auth.service'@@ -34,18 +34,13 @@ export const authenticationController: FastifyPluginAsyncZod = async (platformId: platformId ?? null,})+ authenticationSideEffects(request.log).onSignIn({+ response,+ signedUp,+ method: UserIdentityProvider.EMAIL,+ ip: networkUtils.clientIp(request),+ })if (signedUp && !isNil(response.platformId)) {- applicationEvents(request.log).sendUserEvent({- platformId: response.platformId,- userId: response.id,- projectId: response.projectId ?? undefined,- ip: networkUtils.extractClientRealIp(request, system.get(AppSystemProp.CLIENT_REAL_IP_HEADER)),- }, {- action: ApplicationEventName.USER_SIGNED_UP,- data: {- source: 'credentials',- },- })rejectedPromiseHandler(telemetry(request.log).identifySignUp({userId: response.id,platformId: response.platformId,@@ -75,16 +70,13 @@ export const authenticationController: FastifyPluginAsyncZod = async (}), request.log)}+ authenticationSideEffects(request.log).onSignIn({+ response,+ signedUp,+ method: UserIdentityProvider.EMAIL,+ ip: networkUtils.clientIp(request),+ })if (!isNil(response.platformId)) {- applicationEvents(request.log).sendUserEvent({- platformId: response.platformId,- userId: response.id,- projectId: response.projectId ?? undefined,- ip: networkUtils.extractClientRealIp(request, system.get(AppSystemProp.CLIENT_REAL_IP_HEADER)),- }, {- action: ApplicationEventName.USER_SIGNED_IN,- data: {},- })rejectedPromiseHandler(telemetry(request.log).trackUser({userId: response.id,platformId: response.platformId,@@ -107,16 +99,13 @@ export const authenticationController: FastifyPluginAsyncZod = async (fullName: request.body.fullName,})+ authenticationSideEffects(request.log).onSignIn({+ response,+ signedUp,+ method: provider,+ ip: networkUtils.clientIp(request),+ })if (signedUp && !isNil(response.platformId)) {- applicationEvents(request.log).sendUserEvent({- platformId: response.platformId,- userId: response.id,- projectId: response.projectId ?? undefined,- ip: networkUtils.extractClientRealIp(request, system.get(AppSystemProp.CLIENT_REAL_IP_HEADER)),- }, {- action: ApplicationEventName.USER_SIGNED_UP,- data: {},- })rejectedPromiseHandler(telemetry(request.log).identifySignUp({userId: response.id,platformId: response.platformId,diff --git a/packages/server/api/src/app/authentication/passwordless-auth.controller.ts b/packages/server/api/src/app/authentication/passwordless-auth.controller.tsindex 26e1f0f981b..e8d0c497840 100644--- a/packages/server/api/src/app/authentication/passwordless-auth.controller.ts+++ b/packages/server/api/src/app/authentication/passwordless-auth.controller.ts@@ -1,14 +1,14 @@import { isNil } from '@activepieces/core-utils'-import { ApplicationEventName, RequestEmailCodeRequest, SignUpMethod, TelemetryEventName, VerifyEmailCodeRequest } from '@activepieces/shared'+import { RequestEmailCodeRequest, SignUpMethod, TelemetryEventName, UserIdentityProvider, VerifyEmailCodeRequest } from '@activepieces/shared'import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'import { StatusCodes } from 'http-status-codes'import { securityAccess } from '../core/security/authorization/fastify-security'import { authnRateLimit, emailCodeRateLimit } from '../core/security/rate-limit'-import { applicationEvents } from '../helper/application-events'import { networkUtils } from '../helper/network-utils'import { rejectedPromiseHandler } from '../helper/promise-handler'import { telemetry } from '../helper/telemetry.utils'import { platformUtils } from '../platform/platform.utils'+import { authenticationSideEffects } from './authentication-side-effects'import { passwordlessAuthService } from './passwordless-auth.service'export const passwordlessAuthController: FastifyPluginAsyncZod = async (@@ -42,16 +42,13 @@ export const passwordlessAuthController: FastifyPluginAsyncZod = async (}), request.log)}+ authenticationSideEffects(request.log).onSignIn({+ response,+ signedUp,+ method: UserIdentityProvider.EMAIL,+ ip: networkUtils.clientIp(request),+ })if (!isNil(response.platformId)) {- applicationEvents(request.log).sendUserEvent({- platformId: response.platformId,- userId: response.id,- projectId: response.projectId ?? undefined,- ip: networkUtils.clientIp(request),- }, {- action: ApplicationEventName.USER_SIGNED_IN,- data: {},- })rejectedPromiseHandler(telemetry(request.log).trackUser({userId: response.id,platformId: response.platformId,diff --git a/packages/server/api/src/app/ee/authentication/federated-authn/federated-authn-module.ts b/packages/server/api/src/app/ee/authentication/federated-authn/federated-authn-module.tsindex 85670ea5c61..d643d6ad63c 100644--- a/packages/server/api/src/app/ee/authentication/federated-authn/federated-authn-module.ts+++ b/packages/server/api/src/app/ee/authentication/federated-authn/federated-authn-module.ts@@ -1,13 +1,11 @@import { isNil } from '@activepieces/core-utils'-import { ApplicationEventName, ClaimTokenRequest, SignUpMethod, TelemetryEventName, ThirdPartyAuthnProviderEnum } from '@activepieces/shared'+import { ClaimTokenRequest, SignUpMethod, TelemetryEventName, ThirdPartyAuthnProviderEnum, UserIdentityProvider } from '@activepieces/shared'import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'import { z } from 'zod'+import { authenticationSideEffects } from '../../../authentication/authentication-side-effects'import { securityAccess } from '../../../core/security/authorization/fastify-security'-import { applicationEvents } from '../../../helper/application-events'import { networkUtils } from '../../../helper/network-utils'import { rejectedPromiseHandler } from '../../../helper/promise-handler'-import { system } from '../../../helper/system/system'-import { AppSystemProp } from '../../../helper/system/system-props'import { telemetry } from '../../../helper/telemetry.utils'import { platformUtils } from '../../../platform/platform.utils'import { federatedAuthnService } from './federated-authn-service'@@ -32,18 +30,13 @@ const federatedAuthnController: FastifyPluginAsyncZod = async (app) => {platformId: platformId ?? undefined,code: req.body.code,})+ authenticationSideEffects(req.log).onSignIn({+ response,+ signedUp,+ method: UserIdentityProvider.GOOGLE,+ ip: networkUtils.clientIp(req),+ })if (signedUp && !isNil(response.platformId)) {- applicationEvents(req.log).sendUserEvent({- platformId: response.platformId,- userId: response.id,- projectId: response.projectId ?? undefined,- ip: networkUtils.extractClientRealIp(req, system.get(AppSystemProp.CLIENT_REAL_IP_HEADER)),- }, {- action: ApplicationEventName.USER_SIGNED_UP,- data: {- source: 'sso',- },- })rejectedPromiseHandler(telemetry(req.log).identifySignUp({userId: response.id,platformId: response.platformId,diff --git a/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-controller.ts b/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-controller.tsindex d2729dc1648..bcaa5613a37 100644--- a/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-controller.ts+++ b/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-controller.ts@@ -1,13 +1,11 @@import { assertNotNullOrUndefined } from '@activepieces/core-utils'-import { ApplicationEventName, PrincipalType, TelemetryEventName } from '@activepieces/shared'+import { PrincipalType, TelemetryEventName, UserIdentityProvider } from '@activepieces/shared'import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'import { z } from 'zod'+import { authenticationSideEffects } from '../../../authentication/authentication-side-effects'import { securityAccess } from '../../../core/security/authorization/fastify-security'-import { applicationEvents } from '../../../helper/application-events'import { networkUtils } from '../../../helper/network-utils'import { rejectedPromiseHandler } from '../../../helper/promise-handler'-import { system } from '../../../helper/system/system'-import { AppSystemProp } from '../../../helper/system/system-props'import { telemetry } from '../../../helper/telemetry.utils'import { platformUtils } from '../../../platform/platform.utils'import { platformMustHaveFeatureEnabled } from '../ee-authorization'@@ -28,22 +26,17 @@ export const authnSsoSamlController: FastifyPluginAsyncZod = async (app) => {?? await platformUtils.getPlatformIdForRequest(req)assertNotNullOrUndefined(platformId, 'Platform Id should not be null')const { saml } = await authnSsoSamlService(req.log).getSamlConfigOrThrow(platformId)- const response = await authnSsoSamlService(req.log).acs(platformId, saml, {+ const { response, signedUp } = await authnSsoSamlService(req.log).acs(platformId, saml, {body: req.body,query: req.query,})const url = new URL('/authenticate', networkUtils.getRequestBaseUrl(req))url.searchParams.append('response', JSON.stringify(response))- applicationEvents(req.log).sendUserEvent({- platformId,- userId: response.id,- projectId: response.projectId ?? undefined,- ip: networkUtils.extractClientRealIp(req, system.get(AppSystemProp.CLIENT_REAL_IP_HEADER)),- }, {- action: ApplicationEventName.USER_SIGNED_UP,- data: {- source: 'sso',- },+ authenticationSideEffects(req.log).onSignIn({+ response,+ signedUp,+ method: UserIdentityProvider.SAML,+ ip: networkUtils.clientIp(req),})rejectedPromiseHandler(telemetry(req.log).trackUser({userId: response.id,diff --git a/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-service.ts b/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-service.tsindex dd7ba10b7db..f794f36512c 100644--- a/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-service.ts+++ b/packages/server/api/src/app/ee/authentication/saml-authn/authn-sso-saml-service.ts@@ -1,8 +1,9 @@import { resolveTxt } from 'dns/promises'import { ActivepiecesError, apId, assertNotNullOrUndefined, ErrorCode, isNil, PlatformId, spreadIfDefined, tryCatch, UserId } from '@activepieces/core-utils'-import { ApEdition, AuthenticationResponse, SAMLAuthnProviderConfig, SsoDomainVerification, SsoDomainVerificationRecordType, SsoDomainVerificationStatus, UserIdentityProvider } from '@activepieces/shared'+import { ApEdition, SAMLAuthnProviderConfig, SsoDomainVerification, SsoDomainVerificationRecordType, SsoDomainVerificationStatus, UserIdentityProvider } from '@activepieces/shared'import { FastifyBaseLogger } from 'fastify'import { z } from 'zod'+import { AuthenticationResult } from '../../../authentication/authentication-utils'import { authenticationService } from '../../../authentication/authentication.service'import { domainHelper } from '../../../helper/domain-helper'import { system } from '../../../helper/system/system'@@ -42,11 +43,11 @@ export const authnSsoSamlService = (log: FastifyBaseLogger) => {redirectUrl,}},- async acs(platformId: string, samlProvider: SAMLAuthnProviderConfig, idpLoginResponse: IdpLoginResponse): Promise<AuthenticationResponse> {+ async acs(platformId: string, samlProvider: SAMLAuthnProviderConfig, idpLoginResponse: IdpLoginResponse): Promise<AuthenticationResult> {const acsUrl = await this.getAcsUrl(platformId)const client = await createSamlClient({ platformId, samlProvider, acsUrl })const attributes = await client.parseAndValidateLoginResponse(idpLoginResponse)- const { response } = await authenticationService(log).federatedAuthn({+ return authenticationService(log).federatedAuthn({email: attributes.email,firstName: attributes.firstName,lastName: attributes.lastName,@@ -55,7 +56,6 @@ export const authnSsoSamlService = (log: FastifyBaseLogger) => {provider: UserIdentityProvider.SAML,predefinedPlatformId: platformId,})- return response},async discoverByDomain(domain: string): Promise<DiscoverResult> {const platformId = await this.findPlatformIdByDomain({ domain })diff --git a/packages/server/api/src/app/ee/managed-authn/managed-authn-controller.ts b/packages/server/api/src/app/ee/managed-authn/managed-authn-controller.tsindex f30ec97bffb..f055d12eabf 100644--- a/packages/server/api/src/app/ee/managed-authn/managed-authn-controller.ts+++ b/packages/server/api/src/app/ee/managed-authn/managed-authn-controller.ts@@ -1,11 +1,9 @@import { assertNotNullOrUndefined } from '@activepieces/core-utils'-import { ApplicationEventName, AuthenticationResponse, ManagedAuthnRequestBody } from '@activepieces/shared'+import { AuthenticationResponse, ManagedAuthnRequestBody, UserIdentityProvider } from '@activepieces/shared'import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'+import { authenticationSideEffects } from '../../authentication/authentication-side-effects'import { securityAccess } from '../../core/security/authorization/fastify-security'-import { applicationEvents } from '../../helper/application-events'import { networkUtils } from '../../helper/network-utils'-import { system } from '../../helper/system/system'-import { AppSystemProp } from '../../helper/system/system-props'import { managedAuthnService } from './managed-authn-service'export const managedAuthnController: FastifyPluginAsyncZod = async (@@ -17,21 +15,18 @@ export const managedAuthnController: FastifyPluginAsyncZod = async (async (req): Promise<AuthenticationResponse> => {const { externalAccessToken } = req.body- const response = await managedAuthnService(req.log).externalToken({+ const { response, signedUp } = await managedAuthnService(req.log).externalToken({externalAccessToken,})assertNotNullOrUndefined(response.platformId, 'Platform ID is required')- applicationEvents(req.log).sendUserEvent({- platformId: response.platformId,- userId: response.id,- projectId: response.projectId ?? undefined,- ip: networkUtils.extractClientRealIp(req, system.get(AppSystemProp.CLIENT_REAL_IP_HEADER)),- }, {- action: ApplicationEventName.USER_SIGNED_UP,- data: {- source: 'managed',- },- })+ if (signedUp) {+ authenticationSideEffects(req.log).onSignIn({+ response,+ signedUp,+ method: UserIdentityProvider.JWT,+ ip: networkUtils.clientIp(req),+ })+ }return response},)diff --git a/packages/server/api/src/app/ee/managed-authn/managed-authn-service.ts b/packages/server/api/src/app/ee/managed-authn/managed-authn-service.tsindex b668a8d14d3..d74fc9364e3 100644--- a/packages/server/api/src/app/ee/managed-authn/managed-authn-service.ts+++ b/packages/server/api/src/app/ee/managed-authn/managed-authn-service.ts@@ -1,8 +1,9 @@import { createHash } from 'crypto'import { isNil } from '@activepieces/core-utils'import { cryptoUtils } from '@activepieces/server-utils'-import { AuthenticationResponse, PiecesFilterType, PlatformRole, PrincipalType, Project, ProjectType, User, UserIdentity, UserIdentityProvider } from '@activepieces/shared'+import { PiecesFilterType, PlatformRole, PrincipalType, Project, ProjectType, User, UserIdentity, UserIdentityProvider } from '@activepieces/shared'import { FastifyBaseLogger } from 'fastify'+import { AuthenticationResult } from '../../authentication/authentication-utils'import { accessTokenManager } from '../../authentication/lib/access-token-manager'import { userIdentityService } from '../../authentication/user-identity/user-identity-service'import { platformService } from '../../platform/platform.service'@@ -16,7 +17,7 @@ import { externalTokenExtractor } from './lib/external-token-extractor'export const managedAuthnService = (log: FastifyBaseLogger) => ({async externalToken({externalAccessToken,- }: AuthenticateParams): Promise<AuthenticationResponse> {+ }: AuthenticateParams): Promise<AuthenticationResult> {const externalPrincipal = await externalTokenExtractor(log).extract(externalAccessToken,)@@ -52,7 +53,7 @@ export const managedAuthnService = (log: FastifyBaseLogger) => ({log,})- const user = await getOrCreateUser(externalPrincipal, log)+ const { user, created } = await getOrCreateUser(externalPrincipal, log)await projectMemberService(log).upsert({projectId: project.id,@@ -73,19 +74,22 @@ export const managedAuthnService = (log: FastifyBaseLogger) => ({tokenVersion: identity.tokenVersion,}, 7 * 24 * 60 * 60)return {- id: user.id,- platformRole: user.platformRole,- status: user.status,- externalId: user.externalId,- platformId: user.platformId,- firstName: identity.firstName,- lastName: identity.lastName,- email: identity.email,- trackEvents: identity.trackEvents,- newsLetter: identity.newsLetter,- verified: identity.verified,- token,- projectId: project.id,+ response: {+ id: user.id,+ platformRole: user.platformRole,+ status: user.status,+ externalId: user.externalId,+ platformId: user.platformId,+ firstName: identity.firstName,+ lastName: identity.lastName,+ email: identity.email,+ trackEvents: identity.trackEvents,+ newsLetter: identity.newsLetter,+ verified: identity.verified,+ token,+ projectId: project.id,+ },+ signedUp: created,}},})@@ -125,14 +129,14 @@ const applyProjectPieceAccess = async ({ platformId, projectId, pieceSetKey, pieconst getOrCreateUser = async (params: GetOrCreateUserParams,log: FastifyBaseLogger,-): Promise<User> => {+): Promise<GetOrCreateUserResult> => {const existingUser = await userService(log).getByPlatformAndExternalId({platformId: params.platformId,externalId: params.externalUserId,})if (!isNil(existingUser)) {- return existingUser+ return { user: existingUser, created: false }}const identity = await getOrCreateUserIdentity(params, log)const user = await userService(log).create({@@ -141,7 +145,7 @@ const getOrCreateUser = async (identityId: identity.id,platformRole: PlatformRole.MEMBER,})- return user+ return { user, created: true }}const getOrCreateUserIdentity = async (@@ -204,6 +208,11 @@ type AuthenticateParams = {externalAccessToken: string}+type GetOrCreateUserResult = {+ user: User+ created: boolean+}+type GetOrCreateUserParams = {platformId: stringexternalUserId: stringdiff --git a/packages/server/api/test/integration/cloud/audit-event/sign-in-audit-events.test.ts b/packages/server/api/test/integration/cloud/audit-event/sign-in-audit-events.test.tsnew file mode 100644index 00000000000..9d4db328d0f--- /dev/null+++ b/packages/server/api/test/integration/cloud/audit-event/sign-in-audit-events.test.ts@@ -0,0 +1,220 @@+import { ApplicationEvent, ApplicationEventName, OtpType, PlatformRole, UserIdentityProvider, UserStatus } from '@activepieces/shared'+import { faker } from '@faker-js/faker'+import { FastifyInstance } from 'fastify'+import { StatusCodes } from 'http-status-codes'+import { turnstile } from '../../../../src/app/authentication/lib/turnstile'+import { userIdentityService } from '../../../../src/app/authentication/user-identity/user-identity-service'+import { databaseConnection } from '../../../../src/app/database/database-connection'+import { distributedStore } from '../../../../src/app/database/redis-connections'+import { auditLogRepo } from '../../../../src/app/ee/audit-logs/audit-event-service'+import { generateMockExternalToken } from '../../../helpers/auth'+import { db } from '../../../helpers/db'+import { createMockProject, createMockSigningKey, mockAndSaveBasicSetup, mockBasicUser } from '../../../helpers/mocks'+import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'++process.env.AP_TURNSTILE_SITE_KEY = 'test-site-key'+process.env.AP_TURNSTILE_SECRET_KEY = 'test-secret-key'++const googleAccount = vi.hoisted(() => ({+ email: '',+ firstName: 'Sam',+ lastName: 'Lee',+}))++vi.mock('../../../../src/app/ee/authentication/federated-authn/google-authn-provider', () => ({+ googleAuthnProvider: () => ({+ getLoginUrl: () => Promise.resolve('https://accounts.google.com/o/oauth2/v2/auth'),+ authenticate: () => Promise.resolve(googleAccount),+ }),+}))++let app: FastifyInstance | null = null+let callers = 0++beforeAll(async () => {+ vi.spyOn(turnstile, 'assertSolved').mockResolvedValue(undefined)+ app = await setupTestEnvironment()+ process.env.AP_GOOGLE_CLIENT_ID = 'test-google-client-id'+ process.env.AP_GOOGLE_CLIENT_SECRET = 'test-google-client-secret'+})++afterAll(async () => {+ delete process.env.AP_GOOGLE_CLIENT_ID+ delete process.env.AP_GOOGLE_CLIENT_SECRET+ await teardownTestEnvironment()+})++describe('Sign-in audit events', () => {+ it('records a password sign-in with its method', async () => {+ const { platformId, email, password } = await createMember()++ const response = await app?.inject({ method: 'POST', url: '/api/v1/authentication/sign-in', body: { email, password } })++ expect(response?.statusCode).toBe(StatusCodes.OK)+ expect(await settledEvents({ platformId, expectedCount: 1 })).toEqual([signInEvent({ email, method: UserIdentityProvider.EMAIL })])+ })++ it('records an emailed-code sign-in with its method', async () => {+ const { platformId, email } = await createMember()++ const response = await signInWithCode(email)++ expect(response?.statusCode).toBe(StatusCodes.OK)+ expect(await settledEvents({ platformId, expectedCount: 1 })).toEqual([signInEvent({ email, method: UserIdentityProvider.EMAIL })])+ })++ it('records a returning Google sign-in', async () => {+ const { platformId, email } = await createMember()+ googleAccount.email = email++ const response = await claimWithGoogle()++ expect(response?.statusCode).toBe(StatusCodes.OK)+ expect(await settledEvents({ platformId, expectedCount: 1 })).toEqual([signInEvent({ email, method: UserIdentityProvider.GOOGLE })])+ })++ describe('records a first sign-in as signed up only, with the method', () => {+ it('for Google', async () => {+ googleAccount.email = faker.internet.email().toLowerCase()++ const response = await claimWithGoogle()++ expect(response?.statusCode).toBe(StatusCodes.OK)+ const events = await settledEvents({ platformId: response?.json().platformId, expectedCount: 1 })+ expect(events).toEqual([signUpEvent({ email: googleAccount.email, method: UserIdentityProvider.GOOGLE, source: 'sso' })])+ })++ it('for an emailed code from someone whose name we already have', async () => {+ const email = faker.internet.email().toLowerCase()+ await createIdentity({ email, password: 'password' })++ const response = await signInWithCode(email)++ expect(response?.statusCode).toBe(StatusCodes.OK)+ const events = await settledEvents({ platformId: response?.json().platformId, expectedCount: 1 })+ expect(events).toEqual([signUpEvent({ email, method: UserIdentityProvider.EMAIL, source: 'credentials' })])+ })++ it('for the first password sign-in after verifying the email', async () => {+ const email = faker.internet.email().toLowerCase()+ await createIdentity({ email, password: 'password' })++ const response = await app?.inject({ method: 'POST', url: '/api/v1/authentication/sign-in', body: { email, password: 'password' } })++ expect(response?.statusCode).toBe(StatusCodes.OK)+ const events = await settledEvents({ platformId: response?.json().platformId, expectedCount: 1 })+ expect(events).toEqual([signUpEvent({ email, method: UserIdentityProvider.EMAIL, source: 'credentials' })])+ })+ })++ it('records the method when sign-up finishes at the name step', async () => {+ const email = faker.internet.email().toLowerCase()+ const onboarding = await signInWithCode(email)+ expect(onboarding?.json().platformId).toBeNull()++ const response = await app?.inject({+ method: 'POST',+ url: '/api/v1/authentication/complete-sign-up',+ headers: { authorization: `Bearer ${onboarding?.json().token}` },+ body: { fullName: 'Dana Levi' },+ })++ expect(response?.statusCode).toBe(StatusCodes.OK)+ const events = await settledEvents({ platformId: response?.json().platformId, expectedCount: 1 })+ expect(events).toEqual([signUpEvent({ email, method: UserIdentityProvider.EMAIL, source: 'credentials' })])+ })++ it('records an embed sign-up only when the member is created', async () => {+ const { mockPlatform } = await mockAndSaveBasicSetup()+ const signingKey = createMockSigningKey({ platformId: mockPlatform.id })+ await db.save('signing_key', signingKey)+ const { mockExternalToken } = generateMockExternalToken({ platformId: mockPlatform.id, signingKeyId: signingKey.id })+ const exchange = async () => app?.inject({+ method: 'POST',+ url: '/api/v1/managed-authn/external-token',+ body: { externalAccessToken: mockExternalToken },+ })++ const first = await exchange()+ const second = await exchange()++ expect(first?.statusCode).toBe(StatusCodes.OK)+ expect(second?.statusCode).toBe(StatusCodes.OK)+ const events = await settledEvents({ platformId: mockPlatform.id, expectedCount: 1 })+ expect(events).toEqual([expect.objectContaining({+ action: ApplicationEventName.USER_SIGNED_UP,+ userId: first?.json().id,+ data: expect.objectContaining({ method: UserIdentityProvider.JWT, source: 'managed' }),+ })])+ })+})++async function createMember(): Promise<{ platformId: string, email: string, password: string }> {+ const email = faker.internet.email().toLowerCase()+ const password = 'password'+ const { mockPlatform } = await mockAndSaveBasicSetup({ platform: { emailAuthEnabled: true, enforceAllowedAuthDomains: false } })+ const { mockUser } = await mockBasicUser({+ user: { status: UserStatus.ACTIVE, platformId: mockPlatform.id, platformRole: PlatformRole.MEMBER },+ userIdentity: { email, password, verified: true, firstName: 'Priya', lastName: 'Shah' },+ })+ await db.save('user', mockUser)+ await db.save('project', createMockProject({ ownerId: mockUser.id, platformId: mockPlatform.id }))+ return { platformId: mockPlatform.id, email, password }+}++async function createIdentity({ email, password }: { email: string, password: string }): Promise<void> {+ await userIdentityService(app!.log).create({+ email,+ password,+ firstName: 'Omar',+ lastName: 'Haddad',+ trackEvents: false,+ newsLetter: false,+ provider: UserIdentityProvider.EMAIL,+ verified: true,+ })+}++async function signInWithCode(email: string) {+ callers += 1+ await app?.inject({+ method: 'POST',+ url: '/api/v1/authentication/otp/request',+ headers: { 'x-real-ip': `10.1.${Math.floor(callers / 256)}.${callers % 256}` },+ body: { email },+ })+ const identity = await databaseConnection().getRepository('user_identity').findOneByOrFail({ email })+ const code = await distributedStore.get<string>(`otp-pending-code:${identity.id}:${OtpType.EMAIL_LOGIN}`)+ return app?.inject({ method: 'POST', url: '/api/v1/authentication/otp/verify', body: { email, code } })+}++async function claimWithGoogle() {+ return app?.inject({+ method: 'POST',+ url: '/api/v1/authn/federated/claim',+ body: { providerName: 'google', code: 'authorization-code' },+ })+}++async function settledEvents({ platformId, expectedCount }: { platformId: string, expectedCount: number }): Promise<ApplicationEvent[]> {+ const find = async (): Promise<ApplicationEvent[]> => auditLogRepo().find({ where: { platformId }, order: { created: 'ASC' } })+ await vi.waitUntil(async () => (await find()).length >= expectedCount, { timeout: 5000, interval: 50 })+ await new Promise((resolve) => setTimeout(resolve, 300))+ return find()+}++function signInEvent({ email, method }: { email: string, method: UserIdentityProvider }): unknown {+ return expect.objectContaining({+ action: ApplicationEventName.USER_SIGNED_IN,+ userEmail: email,+ data: expect.objectContaining({ method }),+ })+}++function signUpEvent({ email, method, source }: { email: string, method: UserIdentityProvider, source: string }): unknown {+ return expect.objectContaining({+ action: ApplicationEventName.USER_SIGNED_UP,+ userEmail: email,+ data: expect.objectContaining({ method, source }),+ })+}diff --git a/packages/server/api/test/integration/ee/saml-acs/saml-acs.test.ts b/packages/server/api/test/integration/ee/saml-acs/saml-acs.test.tsindex 9f3b6a8df69..4d3fd7250ff 100644--- a/packages/server/api/test/integration/ee/saml-acs/saml-acs.test.ts+++ b/packages/server/api/test/integration/ee/saml-acs/saml-acs.test.ts@@ -1,7 +1,8 @@-import { UserIdentityProvider } from '@activepieces/shared'+import { ApplicationEvent, ApplicationEventName, UserIdentityProvider } from '@activepieces/shared'import { FastifyInstance } from 'fastify'import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'import { databaseConnection } from '../../../../src/app/database/database-connection'+import { auditLogRepo } from '../../../../src/app/ee/audit-logs/audit-event-service'import { system } from '../../../../src/app/helper/system/system'import { AppSystemProp } from '../../../../src/app/helper/system/system-props'import { createMockPlatform, createMockPlatformPlan, createMockUser, createMockUserIdentity } from '../../../helpers/mocks'@@ -116,4 +117,25 @@ describe('SAML ACS redirect', () => {expect(location.origin).toBe('https://sso.customer.example.com')expect(location.pathname).toBe('/authenticate')})++ it('records a returning SAML sign-in as signed in, not signed up', async () => {+ const before = await settledEvents({ atLeast: 0 })++ await acs({ host: 'apps.customer.example.com' })+ const after = await settledEvents({ atLeast: before.length + 1 })+ const added = after.filter((event) => !before.some((earlier) => earlier.id === event.id))++ expect(added).toEqual([expect.objectContaining({+ action: ApplicationEventName.USER_SIGNED_IN,+ userEmail: SSO_EMAIL,+ data: expect.objectContaining({ method: UserIdentityProvider.SAML }),+ })])+ })})++async function settledEvents({ atLeast }: { atLeast: number }): Promise<ApplicationEvent[]> {+ const find = async (): Promise<ApplicationEvent[]> => auditLogRepo().find({ where: { platformId }, order: { created: 'ASC' } })+ await vi.waitUntil(async () => (await find()).length >= atLeast, { timeout: 5000, interval: 50 })+ await new Promise((resolve) => setTimeout(resolve, 300))+ return find()+}diff --git a/packages/web/public/locales/en/translation.json b/packages/web/public/locales/en/translation.jsonindex 58c842d1145..7684f2226e5 100644--- a/packages/web/public/locales/en/translation.json+++ b/packages/web/public/locales/en/translation.json@@ -3702,5 +3702,12 @@"1 min ago": "1 min ago","{count} mins ago": "{count} mins ago","1 hour ago": "1 hour ago",- "{count} hours ago": "{count} hours ago"+ "{count} hours ago": "{count} hours ago",+ "Sign-in method": "Sign-in method",+ "Sign-up method": "Sign-up method",+ "Not recorded": "Not recorded",+ "Google": "Google",+ "JWT": "JWT",+ "SSO": "SSO",+ "Managed": "Managed"}diff --git a/packages/web/src/app/routes/platform/security/audit-logs/index.tsx b/packages/web/src/app/routes/platform/security/audit-logs/index.tsxindex 94c2b1e10d2..b10d638951b 100644--- a/packages/web/src/app/routes/platform/security/audit-logs/index.tsx+++ b/packages/web/src/app/routes/platform/security/audit-logs/index.tsx@@ -5,7 +5,9 @@ import {AgentRunSource,ApplicationEvent,ApplicationEventName,+ SignUpEvent,summarizeApplicationEvent,+ UserIdentityProvider,} from '@activepieces/shared';import { t } from 'i18next';import {@@ -321,10 +323,14 @@ export default function AuditLogsPage() {</p><div className="grid grid-cols-[150px_1fr] gap-y-3 text-sm">{extractEventDetails(selectedEvent).map(- ({ label, value }) => (+ ({ label, value, muted }) => (<Fragment key={label}><span className="text-gray-11">{label}</span>- <span className="font-medium">{value}</span>+ <span+ className={muted ? 'text-gray-11' : 'font-medium'}+ >+ {value}+ </span></Fragment>),)}@@ -610,15 +616,18 @@ function extractEventDetails(event: ApplicationEvent): EventDetailRow[] {case ApplicationEventName.FOLDER_DELETED:return [{ label: t('Folder'), value: event.data.folder.displayName }];case ApplicationEventName.USER_SIGNED_IN:+ return [+ methodRow({ label: t('Sign-in method'), method: event.data.method }),+ ];case ApplicationEventName.USER_PASSWORD_RESET:case ApplicationEventName.USER_EMAIL_VERIFIED:return [];case ApplicationEventName.USER_SIGNED_UP:return [- {- label: t('Source'),- value: formatUtils.convertEnumToHumanReadable(event.data.source),- },+ methodRow({ label: t('Sign-up method'), method: event.data.method }),+ isNil(event.data.source)+ ? notRecordedRow(t('Source'))+ : { label: t('Source'), value: SOURCE_LABEL[event.data.source]() },];case ApplicationEventName.SIGNING_KEY_CREATED:return [@@ -739,9 +748,26 @@ function extractEventDetails(event: ApplicationEvent): EventDetailRow[] {}}+function methodRow({+ label,+ method,+}: {+ label: string;+ method: UserIdentityProvider | undefined;+}): EventDetailRow {+ return isNil(method)+ ? notRecordedRow(label)+ : { label, value: METHOD_LABEL[method]() };+}++function notRecordedRow(label: string): EventDetailRow {+ return { label, value: t('Not recorded'), muted: true };+}+type EventDetailRow = {label: string;value: string;+ muted?: boolean;};const OUTCOME_LABEL: Record<AgentActionOutcome, () => string> = {@@ -755,3 +781,19 @@ const RAN_FROM_LABEL: Record<AgentRunSource, () => string> = {[AgentRunSource.CHAT]: () => t('Chat'),[AgentRunSource.AGENT_BUILDER]: () => t('The agent builder'),};++const METHOD_LABEL: Record<UserIdentityProvider, () => string> = {+ [UserIdentityProvider.EMAIL]: () => t('Email'),+ [UserIdentityProvider.GOOGLE]: () => t('Google'),+ [UserIdentityProvider.SAML]: () => t('SAML'),+ [UserIdentityProvider.JWT]: () => t('JWT'),+};++const SOURCE_LABEL: Record<+ NonNullable<SignUpEvent['data']['source']>,+ () => string+> = {+ credentials: () => t('Credentials'),+ sso: () => t('SSO'),+ managed: () => t('Managed'),+};