import { Plugin } from "@webiny/plugins/types"; import { Context } from "@webiny/handler/types"; import { Authentication, Identity } from "@webiny/api-authentication/types"; import { Topic } from "@webiny/pubsub/types"; import { GetTenant } from "./createSecurity"; import { ProjectPackageFeatures } from "@webiny/wcp/types"; import { TenancyContext } from "@webiny/api-tenancy/types"; export type { Jwk, Jwt } from "./utils/verifyJwtUsingJwk"; export type SecurityIdentity = Identity; export type SecurityAuthenticationPlugin = Plugin & { type: "security-authentication"; authenticate(context: Context): Promise | Promise; }; export interface SecurityAuthorizationPlugin extends Plugin { type: "security-authorization"; getPermissions(context: SecurityContext): Promise; } export type GetPermissions = (name: string) => Promise; export interface Authorizer { (): Promise; } export interface SecurityConfig { advancedAccessControlLayer?: ProjectPackageFeatures["advancedAccessControlLayer"]; getTenant: GetTenant; storageOperations: SecurityStorageOperations; groupsProvider?: () => Promise; teamsProvider?: () => Promise; } export interface ErrorEvent extends InstallEvent { error: Error; } export interface InstallEvent { tenant: string; } export interface LoginEvent { identity: TIdentity; } export interface IdentityEvent { identity: TIdentity; } export interface GetGroupWhere { id?: string; slug?: string; tenant?: string; } export interface GetTeamWhere { id?: string; slug?: string; tenant?: string; } export type AuthenticationToken = string; export interface Security extends Authentication { /** * @deprecated */ onBeforeInstall: Topic; onSystemBeforeInstall: Topic; onInstall: Topic; /** * @deprecated */ onAfterInstall: Topic; onSystemAfterInstall: Topic; onCleanup: Topic; onBeforeLogin: Topic>; onLogin: Topic>; onAfterLogin: Topic>; onIdentity: Topic>; /** * Returns the token which was used to authenticate (if authentication was successful). */ getToken(): AuthenticationToken | undefined; config: SecurityConfig; getStorageOperations(): SecurityStorageOperations; isAuthorizationEnabled(): boolean; withoutAuthorization(cb: () => Promise): Promise; withIdentity(identity: Identity | undefined, cb: () => Promise): Promise; addAuthorizer(authorizer: Authorizer): void; getAuthorizers(): Authorizer[]; getPermission(permission: string): Promise; getPermissions(permission: string): Promise; listPermissions(): Promise; hasFullAccess(): Promise; getApiKey(id: string): Promise; getApiKeyByToken(token: string): Promise; listApiKeys(): Promise; createApiKey(data: ApiKeyInput): Promise; updateApiKey(id: string, data: ApiKeyInput): Promise; deleteApiKey(id: string): Promise; onApiKeyBeforeCreate: Topic<{ apiKey: ApiKey; }>; onApiKeyAfterCreate: Topic<{ apiKey: ApiKey; }>; onApiKeyBeforeUpdate: Topic<{ original: ApiKey; apiKey: ApiKey; }>; onApiKeyAfterUpdate: Topic<{ original: ApiKey; apiKey: ApiKey; }>; onApiKeyBeforeDelete: Topic<{ apiKey: ApiKey; }>; onApiKeyAfterDelete: Topic<{ apiKey: ApiKey; }>; getGroup(params: GetGroupParams): Promise; listGroups(params?: ListGroupsParams): Promise; createGroup(input: GroupInput): Promise; updateGroup(id: string, input: Partial): Promise; deleteGroup(id: string): Promise; onGroupBeforeCreate: Topic<{ group: Group; }>; onGroupAfterCreate: Topic<{ group: Group; }>; onGroupBeforeUpdate: Topic<{ original: Group; group: Group; }>; onGroupAfterUpdate: Topic<{ original: Group; group: Group; }>; onGroupBeforeDelete: Topic<{ group: Group; }>; onGroupAfterDelete: Topic<{ group: Group; }>; getTeam(params: GetTeamParams): Promise; listTeams(params?: ListTeamsParams): Promise; createTeam(input: TeamInput): Promise; updateTeam(id: string, input: Partial): Promise; deleteTeam(id: string): Promise; onTeamBeforeCreate: Topic<{ team: Team; }>; onTeamAfterCreate: Topic<{ team: Team; }>; onTeamBeforeUpdate: Topic<{ original: Team; team: Team; }>; onTeamAfterUpdate: Topic<{ original: Team; team: Team; }>; onTeamBeforeDelete: Topic<{ team: Team; }>; onTeamAfterDelete: Topic<{ team: Team; }>; createTenantLinks(params: CreateTenantLinkParams[]): Promise; updateTenantLinks(params: UpdateTenantLinkParams[]): Promise; deleteTenantLinks(params: DeleteTenantLinkParams[]): Promise; listTenantLinksByType(params: ListTenantLinksByTypeParams): Promise; listTenantLinksByTenant(params: ListTenantLinksParams): Promise; listTenantLinksByIdentity(params: ListTenantLinksByIdentityParams): Promise; getTenantLinkByIdentity(params: GetTenantLinkByIdentityParams): Promise; getVersion(): Promise; setVersion(version: string): Promise; install(this: Security): Promise; } export interface SecurityStorageOperations { getGroup(params: StorageOperationsGetGroupParams): Promise; listGroups(params: StorageOperationsListGroupsParams): Promise; createGroup(params: StorageOperationsCreateGroupParams): Promise; updateGroup(params: StorageOperationsUpdateGroupParams): Promise; deleteGroup(params: StorageOperationsDeleteGroupParams): Promise; getTeam(params: StorageOperationsGetTeamParams): Promise; listTeams(params: StorageOperationsListTeamsParams): Promise; createTeam(params: StorageOperationsCreateTeamParams): Promise; updateTeam(params: StorageOperationsUpdateTeamParams): Promise; deleteTeam(params: StorageOperationsDeleteTeamParams): Promise; getSystemData(params: StorageOperationsGetSystemParams): Promise; createSystemData(params: StorageOperationsCreateSystemParams): Promise; updateSystemData(params: StorageOperationsUpdateSystemParams): Promise; createTenantLinks(params: StorageOperationsCreateTenantLinkParams[]): Promise; updateTenantLinks(params: StorageOperationsUpdateTenantLinkParams[]): Promise; deleteTenantLinks(params: StorageOperationsDeleteTenantLinkParams[]): Promise; listTenantLinksByType(params: ListTenantLinksByTypeParams): Promise; listTenantLinksByTenant(params: StorageOperationsListTenantLinksParams): Promise; listTenantLinksByIdentity(params: StorageOperationsListTenantLinksByIdentityParams): Promise; getTenantLinkByIdentity(params: StorageOperationsGetTenantLinkByIdentityParams): Promise; getApiKey(params: StorageOperationsGetApiKeyParams): Promise; getApiKeyByToken(params: StorageOperationsGetApiKeyByTokenParams): Promise; listApiKeys(params: StorageOperationsListApiKeysParams): Promise; createApiKey(params: StorageOperationsCreateApiKeyParams): Promise; updateApiKey(params: StorageOperationsUpdateApiKeyParams): Promise; deleteApiKey(params: StorageOperationsDeleteApiKeyParams): Promise; } export type SecurityPermission> = T & { name: string; }; export interface SecurityContext extends TenancyContext { security: Security; } export interface FullAccessPermission { name: "*"; } export interface CreatedBy { id: string; displayName: string | null; type: string; } export interface Group { tenant: string | null; createdOn: string | null; createdBy: CreatedBy | null; id: string; name: string; slug: string; description: string; system: boolean; permissions: SecurityPermission[]; webinyVersion: string | null; plugin?: boolean; } export type SecurityRole = Group; export type SecurityTeam = Team; export type GroupInput = Pick & { system?: boolean; }; export interface GetGroupParams { where: GetGroupWhere; } export interface ListGroupsParams { where?: { id_in?: string[]; slug_in?: string[]; }; sort?: string[]; } export interface GroupsCreateParams { group: Group; } export interface CreateGroupParams { group: Group; } export interface UpdateGroupParams { original: Group; group: Group; } export interface DeleteGroupParams { group: Group; } export interface Team { tenant: string | null; createdOn: string | null; createdBy: CreatedBy | null; id: string; name: string; slug: string; description: string; system: boolean; groups: string[]; webinyVersion: string | null; plugin?: boolean; } export type TeamInput = Pick & { system?: boolean; }; export interface GetTeamParams { where: GetTeamWhere; } export interface ListTeamsParams { where?: { id_in?: string[]; slug_in?: string[]; }; sort?: string[]; } export interface TeamsCreateParams { team: Team; } export interface CreateTeamParams { team: Team; } export interface UpdateTeamParams { original: Team; team: Team; } export interface DeleteTeamParams { team: Team; } export interface System { tenant: string; version: string; installedOn: string; } export interface GetSystemParams { tenant: string; } export interface CreateSystemParams { system: System; } export interface UpdateSystemParams { original: System; system: System; } export interface CreateTenantLinkParams> { identity: string; tenant: string; type: string; data?: TData; } export interface UpdateTenantLinkParams> { identity: string; tenant: string; type: string; data?: TData; } export interface DeleteTenantLinkParams { identity: string; tenant: string; } export interface ListTenantLinksByTypeParams { tenant: string; type: string; } export interface ListTenantLinksByIdentityParams { identity: string; } export interface ListTenantLinksParams { tenant: string; } export interface GetTenantLinkByIdentityParams { identity: string; tenant: string; } export interface TenantLink { createdOn: string; identity: string; tenant: string; type: string; data?: TData; webinyVersion: string; } export interface PermissionsTenantLinkGroup { id: string; permissions: SecurityPermission[]; } export interface PermissionsTenantLinkTeam { id: string; groups: Array<{ id: string; permissions: SecurityPermission[]; }>; } export type PermissionsTenantLink = TenantLink<{ groups: PermissionsTenantLinkGroup[]; teams: PermissionsTenantLinkTeam[]; }>; export interface ApiKey { id: string; tenant: string; name: string; description: string; token: string; permissions: SecurityPermission[]; createdBy: CreatedBy; createdOn: string; webinyVersion?: string; } export interface ApiKeyInput { name: string; description: string; permissions: SecurityPermission[]; } export interface ApiKeyPermission extends SecurityPermission { name: "security.apiKey"; } export interface GetApiKeyParams { tenant: string; id: string; } export interface ListApiKeysParams { sort?: string[]; } export interface GetApiKeyByTokenParams { tenant: string; token: string; } export interface CreateApiKeyParams { apiKey: ApiKey; } export interface UpdateApiKeyParams { original: ApiKey; apiKey: ApiKey; } export interface DeleteApiKeyParams { apiKey: ApiKey; } export interface StorageOperationsListApiKeysParams extends ListApiKeysParams { where: { tenant: string; }; } export interface StorageOperationsGetGroupParams extends GetGroupParams { where: GetGroupParams["where"] & { tenant: string; }; } export interface StorageOperationsListGroupsParams extends ListGroupsParams { where: ListGroupsParams["where"] & { tenant: string; }; } export type StorageOperationsCreateGroupParams = CreateGroupParams; export type StorageOperationsUpdateGroupParams = UpdateGroupParams; export type StorageOperationsDeleteGroupParams = DeleteGroupParams; export interface StorageOperationsGetTeamParams extends GetTeamParams { where: GetTeamParams["where"] & { tenant: string; }; } export interface StorageOperationsListTeamsParams extends ListTeamsParams { where: ListTeamsParams["where"] & { tenant: string; }; } export type StorageOperationsCreateTeamParams = CreateTeamParams; export type StorageOperationsUpdateTeamParams = UpdateTeamParams; export type StorageOperationsDeleteTeamParams = DeleteTeamParams; export type StorageOperationsGetSystemParams = GetSystemParams; export type StorageOperationsCreateSystemParams = CreateSystemParams; export type StorageOperationsUpdateSystemParams = UpdateSystemParams; export interface StorageOperationsCreateTenantLinkParams extends CreateTenantLinkParams { createdOn: string; webinyVersion: string; } export type StorageOperationsUpdateTenantLinkParams = UpdateTenantLinkParams; export type StorageOperationsDeleteTenantLinkParams = DeleteTenantLinkParams; export type StorageOperationsListTenantLinksParams = ListTenantLinksParams; export type StorageOperationsListTenantLinksByIdentityParams = ListTenantLinksByIdentityParams; export type StorageOperationsGetTenantLinkByIdentityParams = GetTenantLinkByIdentityParams; export type StorageOperationsGetApiKeyParams = GetApiKeyParams; export type StorageOperationsGetApiKeyByTokenParams = GetApiKeyByTokenParams; export type StorageOperationsCreateApiKeyParams = CreateApiKeyParams; export type StorageOperationsUpdateApiKeyParams = UpdateApiKeyParams; export type StorageOperationsDeleteApiKeyParams = DeleteApiKeyParams;