Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Set up OAuth2 cred test #6960

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
IHttpRequestHelper,
INodeTypeData,
INodeTypes,
ICredentialTestFunctions,
} from 'n8n-workflow';
import {
ICredentialsHelper,
Expand All @@ -53,6 +54,9 @@ import { CredentialsOverwrites } from '@/CredentialsOverwrites';
import { whereClause } from './UserManagement/UserManagementHelper';
import { RESPONSE_ERROR_MESSAGES } from './constants';
import { Container } from 'typedi';
import { isObjectLiteral } from './utils';

const { OAUTH2_CREDENTIAL_TEST_SUCCEEDED, OAUTH2_CREDENTIAL_TEST_FAILED } = RESPONSE_ERROR_MESSAGES;

const mockNode = {
name: '',
Expand Down Expand Up @@ -466,8 +470,17 @@ export class CredentialsHelper extends ICredentialsHelper {
await Db.collections.Credentials.update(findQuery, newCredentialsData);
}

private static hasAccessToken(credentialsDecrypted: ICredentialsDecrypted) {
const oauthTokenData = credentialsDecrypted?.data?.oauthTokenData;

if (!isObjectLiteral(oauthTokenData)) return false;

return 'access_token' in oauthTokenData;
}

private getCredentialTestFunction(
credentialType: string,
credentialsDecrypted: ICredentialsDecrypted,
): ICredentialTestFunction | ICredentialTestRequestData | undefined {
// Check if test is defined on credentials
const type = this.credentialTypes.getByName(credentialType);
Expand Down Expand Up @@ -496,6 +509,25 @@ export class CredentialsHelper extends ICredentialsHelper {
for (const nodeType of allNodeTypes) {
// Check each of teh credentials
for (const { name, testedBy } of nodeType.description.credentials ?? []) {
if (name === credentialType && name.endsWith('OAuth2Api')) {
ivov marked this conversation as resolved.
Show resolved Hide resolved
const hasAccessToken = CredentialsHelper.hasAccessToken(credentialsDecrypted);

return async function oauth2CredTest(
this: ICredentialTestFunctions,
__: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
return hasAccessToken
? {
status: 'OK',
message: OAUTH2_CREDENTIAL_TEST_SUCCEEDED,
}
: {
status: 'Error',
message: OAUTH2_CREDENTIAL_TEST_FAILED,
};
};
}
ivov marked this conversation as resolved.
Show resolved Hide resolved

if (name === credentialType && !!testedBy) {
if (typeof testedBy === 'string') {
if (node instanceof VersionedNodeType) {
Expand Down Expand Up @@ -532,7 +564,10 @@ export class CredentialsHelper extends ICredentialsHelper {
credentialType: string,
credentialsDecrypted: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentialTestFunction = this.getCredentialTestFunction(credentialType);
const credentialTestFunction = this.getCredentialTestFunction(
credentialType,
credentialsDecrypted,
ivov marked this conversation as resolved.
Show resolved Hide resolved
);
if (credentialTestFunction === undefined) {
return {
status: 'Error',
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const RESPONSE_ERROR_MESSAGES = {
PACKAGE_LOADING_FAILED: 'The specified package could not be loaded',
DISK_IS_FULL: 'There appears to be insufficient disk space',
USERS_QUOTA_REACHED: 'Maximum number of users reached',
OAUTH2_CREDENTIAL_TEST_SUCCEEDED: 'Connection Successful!',
OAUTH2_CREDENTIAL_TEST_FAILED: 'This OAuth2 credential was not connected to an account.',
};

export const AUTH_COOKIE_NAME = 'n8n-auth';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { isObjectLiteral } from '@/utils';
import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
import type { MigrationContext, IrreversibleMigration } from '@db/types';

type OldPinnedData = { [nodeName: string]: IDataObject[] };
type NewPinnedData = { [nodeName: string]: INodeExecutionData[] };
type Workflow = { id: number; pinData: string | OldPinnedData };

function isObjectLiteral(item: unknown): item is { [key: string]: string } {
return typeof item === 'object' && item !== null && !Array.isArray(item);
}

function isJsonKeyObject(item: unknown): item is {
json: unknown;
[keys: string]: unknown;
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ export function isStringArray(value: unknown): value is string[] {
}

export const isIntegerString = (value: string) => /^\d+$/.test(value);

export function isObjectLiteral(item: unknown): item is { [key: string]: string } {
return typeof item === 'object' && item !== null && !Array.isArray(item);
}