n8n
Version:
n8n Workflow Automation Tool
250 lines (249 loc) • 12.5 kB
TypeScript
import type { CreateCredentialDto } from '@n8n/api-types';
import { Logger } from '@n8n/backend-common';
import { CredentialsEntity, DbLockService, SharedCredentials, CredentialsRepository, InstanceCredentialAssignmentRepository, ProjectRepository, SharedCredentialsRepository, UserRepository } from '@n8n/db';
import type { SlimProject, User, ICredentialsDb, OperationContext } from '@n8n/db';
import { type Scope } from '@n8n/permissions';
import { type EntityManager, type FindOptionsRelations } from '@n8n/typeorm';
import { ErrorReporter } from 'n8n-core';
import type { ICredentialDataDecryptedObject, ICredentialsDecrypted, INodeProperties } from 'n8n-workflow';
import { CredentialTypes } from '../credential-types';
import { CredentialsHelper } from '../credentials-helper';
import { ExternalHooks } from '../external-hooks';
import { ExternalSecretsConfig } from '../modules/external-secrets.ee/external-secrets.config';
import { SecretsProviderAccessCheckService } from '../modules/external-secrets.ee/secret-provider-access-check.service.ee';
import type { CredentialRequest, ListQuery } from '../requests';
import { CredentialsTester } from '../services/credentials-tester.service';
import { OwnershipService } from '../services/ownership.service';
import { ProjectService } from '../services/project.service.ee';
import { RoleService } from '../services/role.service';
import { CredentialConnectionStatusProxy } from './credential-connection-status-proxy';
import { CredentialDependencyService } from './credential-dependency.service';
import { CredentialsFinderService } from './credentials-finder.service';
import { InstanceCredentialUseRegistry } from './instance-credential-use.registry';
export type CredentialsGetSharedOptions = {
allowGlobalScope: true;
globalScope: Scope;
} | {
allowGlobalScope: false;
};
type PrepareUpdateDataOptions = {
clearOauthTokenData?: boolean;
operationContext?: OperationContext;
};
type UpdateOptions = {
deleteUserEntries?: boolean;
instanceCredential?: Pick<CredentialsEntity, 'id' | 'type'>;
};
type InstanceCredentialWriteOptions = {
skipExternalHooks?: boolean;
encryptedData?: ICredentialsDb;
};
type WorkflowCredentialResult = {
id: string;
name: string;
type: string;
createdAt: string;
updatedAt: string;
scopes: Scope[];
isManaged: boolean;
isGlobal: boolean;
isResolvable: boolean;
homeProject: SlimProject | null;
sharedWithProjects: SlimProject[];
currentUserHasAccess: boolean;
connectedByMe?: boolean;
};
export declare class CredentialsService {
private readonly credentialsRepository;
private readonly credentialDependencyService;
private readonly sharedCredentialsRepository;
private readonly ownershipService;
private readonly logger;
private readonly errorReporter;
private readonly credentialsTester;
private readonly externalHooks;
private readonly credentialTypes;
private readonly projectRepository;
private readonly projectService;
private readonly roleService;
private readonly userRepository;
private readonly credentialsFinderService;
private readonly credentialsHelper;
private readonly externalSecretsConfig;
private readonly externalSecretsProviderAccessCheckService;
private readonly connectionStatusProxy;
private readonly instanceCredentialAssignmentRepository;
private readonly instanceCredentialUseRegistry;
private readonly dbLockService;
constructor(credentialsRepository: CredentialsRepository, credentialDependencyService: CredentialDependencyService, sharedCredentialsRepository: SharedCredentialsRepository, ownershipService: OwnershipService, logger: Logger, errorReporter: ErrorReporter, credentialsTester: CredentialsTester, externalHooks: ExternalHooks, credentialTypes: CredentialTypes, projectRepository: ProjectRepository, projectService: ProjectService, roleService: RoleService, userRepository: UserRepository, credentialsFinderService: CredentialsFinderService, credentialsHelper: CredentialsHelper, externalSecretsConfig: ExternalSecretsConfig, externalSecretsProviderAccessCheckService: SecretsProviderAccessCheckService, connectionStatusProxy: CredentialConnectionStatusProxy, instanceCredentialAssignmentRepository: InstanceCredentialAssignmentRepository, instanceCredentialUseRegistry: InstanceCredentialUseRegistry, dbLockService: DbLockService);
countConnectedUsers(credentialId: string): Promise<number>;
populateConnectedByMe<T extends {
id: string;
isResolvable?: boolean;
}>(credentials: T[], user: User): Promise<void>;
private addGlobalCredentials;
private extractTypeFilter;
getMany(user: User, options: {
listQueryOptions?: ListQuery.Options;
includeScopes?: boolean;
includeData: true;
onlySharedWithMe?: boolean;
includeGlobal?: boolean;
filters?: {
externalSecretsStore?: string;
};
}): Promise<Array<ICredentialsDecrypted<ICredentialDataDecryptedObject>>>;
getMany(user: User, options?: {
listQueryOptions?: ListQuery.Options;
includeScopes?: boolean;
includeData?: boolean;
onlySharedWithMe?: boolean;
includeGlobal?: boolean;
filters?: {
externalSecretsStore?: string;
};
}): Promise<CredentialsEntity[]>;
private getManyForAdminUser;
private getManyForMemberUser;
private applyPersonalProjectFilter;
private enrichCredentials;
private populateSharedRelations;
private addScopesToCredentials;
private addDecryptedDataToCredentials;
getCredentialsAUserCanUseInAWorkflow(user: User, options: {
workflowId: string;
} | {
projectId: string;
}): Promise<WorkflowCredentialResult[]>;
findAllGlobalCredentialIds(includeData?: boolean): Promise<CredentialsEntity[]>;
findAllCredentialIdsForWorkflow(workflowId: string): Promise<CredentialsEntity[]>;
findAllCredentialIdsForProject(projectId: string): Promise<CredentialsEntity[]>;
getSharing(user: User, credentialId: string, globalScopes: Scope[], relations?: FindOptionsRelations<SharedCredentials>): Promise<SharedCredentials | null>;
prepareUpdateData(user: User, data: CredentialRequest.CredentialProperties, existingCredential: CredentialsEntity, options?: PrepareUpdateDataOptions): Promise<CredentialsEntity>;
createEncryptedData(credential: {
id: string | null;
name: string;
type: string;
data: ICredentialDataDecryptedObject;
}): Promise<ICredentialsDb>;
clearOauthTokenData(credential: CredentialsEntity): Promise<void>;
decrypt(credential: CredentialsEntity, includeRawData?: boolean): Promise<ICredentialDataDecryptedObject>;
update(credentialId: string, newCredentialData: ICredentialsDb, decryptedCredentialData?: ICredentialDataDecryptedObject, options?: UpdateOptions): Promise<CredentialsEntity | null>;
runInstanceCredentialHooks(event: 'create' | 'update', credential: {
id: string | null;
name: string;
type: string;
data: ICredentialDataDecryptedObject;
}): Promise<ICredentialsDb>;
updateInstanceCredential(user: User, credentialId: string, data: CredentialRequest.CredentialProperties, ctx: OperationContext, options?: InstanceCredentialWriteOptions): Promise<CredentialsEntity>;
private resolveOwningProjectIdForNewCredential;
save(credential: CredentialsEntity, encryptedData: ICredentialsDb, user: User, projectId: string, decryptedCredentialData?: ICredentialDataDecryptedObject): Promise<CredentialsEntity>;
delete(user: User, credentialId: string, options?: {
includeInstanceCredentials?: boolean;
}): Promise<void>;
test(userId: User['id'], credentials: ICredentialsDecrypted): Promise<import("n8n-workflow").INodeCredentialTestResult>;
testById(userId: User['id'], credentialId: string): Promise<import("n8n-workflow").INodeCredentialTestResult>;
testWithCredentials(user: User, credentials: ICredentialsDecrypted): Promise<import("n8n-workflow").INodeCredentialTestResult>;
redact(data: ICredentialDataDecryptedObject, credential: CredentialsEntity): ICredentialDataDecryptedObject;
private redactValues;
private redactCollectionOption;
private redactJsonLeaves;
private mergeRedactedJsonLeaves;
private unredactRestoreValues;
getCredentialTypeProperties(credentialType: string): INodeProperties[];
getChangedSharedFields(credential: CredentialsEntity, newData: ICredentialDataDecryptedObject): Promise<string[]>;
unredact(redactedData: ICredentialDataDecryptedObject, savedData: ICredentialDataDecryptedObject, props?: INodeProperties[]): ICredentialDataDecryptedObject;
getOne(user: User, credentialId: string, includeDecryptedData: boolean, options?: {
includeInstanceCredentials?: boolean;
}): Promise<{
id: string;
generateId(): void;
createdAt: Date;
updatedAt: Date;
setUpdateDate(): void;
name: string;
type: string;
shared: SharedCredentials[];
isManaged: boolean;
isGlobal: boolean;
isResolvable: boolean;
resolvableAllowFallback: boolean;
resolverId: string | null;
usageScope: import("@n8n/db").CredentialUsageScope;
connectedByMe?: boolean;
connectedUserCount?: number;
data: ICredentialDataDecryptedObject;
} | {
id: string;
generateId(): void;
createdAt: Date;
updatedAt: Date;
setUpdateDate(): void;
name: string;
type: string;
shared: SharedCredentials[];
isManaged: boolean;
isGlobal: boolean;
isResolvable: boolean;
resolvableAllowFallback: boolean;
resolverId: string | null;
usageScope: import("@n8n/db").CredentialUsageScope;
connectedByMe?: boolean;
connectedUserCount?: number;
}>;
getCredentialScopes(user: User, credentialId: string): Promise<Scope[]>;
transferAll(fromProjectId: string, toProjectId: string, trx?: EntityManager): Promise<void>;
replaceCredentialContentsForSharee(user: User, credential: CredentialsEntity, decryptedData: ICredentialDataDecryptedObject, mergedCredentials: ICredentialsDecrypted): Promise<void>;
createUnmanagedCredential(dto: CreateCredentialDto, user: User): Promise<{
id: string;
generateId(): void;
createdAt: Date;
updatedAt: Date;
setUpdateDate(): void;
name: string;
data: string;
type: string;
isManaged: boolean;
isGlobal: boolean;
isResolvable: boolean;
resolvableAllowFallback: boolean;
resolverId: string | null;
usageScope: import("@n8n/db").CredentialUsageScope;
scopes: Scope[];
}>;
createInstanceCredential(dto: CreateCredentialDto, user: User, ctx: OperationContext, options?: InstanceCredentialWriteOptions): Promise<CredentialsEntity>;
createStubCredential(opts: {
name: string;
type: string;
projectId: string;
}, user: User): Promise<CredentialsEntity>;
checkCredentialData(type: string, data: ICredentialDataDecryptedObject, user: User, projectId: string): Promise<void>;
private validateCredentialData;
isOAuthCredentialType(type: string): boolean;
private validateOAuthCredentialUrls;
createManagedCredential(dto: CreateCredentialDto, user: User): Promise<{
id: string;
generateId(): void;
createdAt: Date;
updatedAt: Date;
setUpdateDate(): void;
name: string;
data: string;
type: string;
isManaged: boolean;
isGlobal: boolean;
isResolvable: boolean;
resolvableAllowFallback: boolean;
resolverId: string | null;
usageScope: import("@n8n/db").CredentialUsageScope;
scopes: Scope[];
}>;
ensureCanManageEndUserCredential(user: User, projectId?: string): Promise<void>;
private createCredential;
private persistInstanceCredential;
private getValidatedInstanceCredentialHookData;
validateInstanceCredentialData(data: ICredentialDataDecryptedObject): void;
validateInstanceCredentialUpdate(credential: Pick<CredentialsEntity, 'id' | 'type'>, data: ICredentialDataDecryptedObject, credentialUseIds?: string[], ctx?: OperationContext): Promise<void>;
private prepareCredentialsForTest;
}
export {};