Add isScim to user created event

This commit is contained in:
adrinr 2023-03-24 16:46:03 +00:00
parent 31eaa36883
commit 6df08799bb
8 changed files with 24 additions and 6 deletions

View File

@ -19,7 +19,12 @@ export function doInIdentityContext(identity: IdentityContext, task: any) {
}
// used in server/worker
export function doInUserContext(user: User, ctx: Ctx, task: any) {
export function doInUserContext(
user: User,
ctx: Ctx,
task: any,
isScim: boolean
) {
const userContext: UserContext = {
...user,
_id: user._id as string,
@ -29,6 +34,7 @@ export function doInUserContext(user: User, ctx: Ctx, task: any) {
// filled in by koa-useragent package
userAgent: ctx.userAgent._agent.source,
},
isScimCall: isScim,
}
return doInIdentityContext(userContext, task)
}

View File

@ -15,13 +15,14 @@ import {
UserUpdatedEvent,
UserOnboardingEvent,
} from "@budibase/types"
import { context } from "../.."
async function created(user: User, timestamp?: number) {
const properties: UserCreatedEvent = {
userId: user._id as string,
audited: {
email: user.email,
scim: !!user.scimInfo?.isSync,
viaScim: !!(context.getIdentity() as any)?.isScimCall,
},
}
await publishEvent(Event.USER_CREATED, properties, timestamp)
@ -31,7 +32,7 @@ async function updated(user: User) {
const properties: UserUpdatedEvent = {
userId: user._id as string,
audited: {
email: user.email,
email: user.email
},
}
await publishEvent(Event.USER_UPDATED, properties)

View File

@ -4,6 +4,7 @@ import {
clearCookie,
openJwt,
isValidInternalAPIKey,
isScimEndpoint,
} from "../utils"
import { getUser } from "../cache/user"
import { getSession, updateSessionTTL } from "../security/sessions"
@ -105,6 +106,8 @@ export default function (
apiKey = ctx.request.headers[Header.AUTHORIZATION].split(" ")[1]
}
const isScimCall = isScimEndpoint(ctx)
const tenantId = ctx.request.headers[Header.TENANT_ID]
let authenticated = false,
user = null,
@ -168,7 +171,7 @@ export default function (
finalise(ctx, { authenticated, user, internal, version, publicEndpoint })
if (user && user.email) {
return identity.doInUserContext(user, ctx, next)
return identity.doInUserContext(user, ctx, next, isScimCall)
} else {
return next()
}

View File

@ -0,0 +1,6 @@
import { Ctx } from "@budibase/types"
const SCIM_ENDPOINTS = new RegExp(["scim/"].join("|"))
export function isScimEndpoint(ctx: Ctx): boolean {
return SCIM_ENDPOINTS.test(ctx.request.url)
}

View File

@ -1,2 +1,3 @@
export * from "./hashing"
export * from "./utils"
export * from "./endpointUtils"

View File

@ -17,6 +17,7 @@ export interface UserContext extends BaseContext, User {
tenantId: string
account?: Account
hostInfo: HostInfo
isScimCall?: boolean
}
export type IdentityContext = BaseContext | AccountUserContext | UserContext

View File

@ -198,7 +198,7 @@ export enum Event {
// a user facing event or not.
export const AuditedEventFriendlyName: Record<Event, string | undefined> = {
// USER
[Event.USER_CREATED]: `User "{{ email }}" created{{#if scim}} via SCIM{{/if}}`,
[Event.USER_CREATED]: `User "{{ email }}" created{{#if viaScim}} via SCIM{{/if}}`,
[Event.USER_UPDATED]: `User "{{ email }}" updated`,
[Event.USER_DELETED]: `User "{{ email }}" deleted`,
[Event.USER_PERMISSION_ADMIN_ASSIGNED]: `User "{{ email }}" admin role assigned`,

View File

@ -4,7 +4,7 @@ export interface UserCreatedEvent extends BaseEvent {
userId: string
audited: {
email: string
scim: boolean
viaScim: boolean
}
}