mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(core): Set up OAuth2 cred test (#6960)
https://linear.app/n8n/issue/PAY-728
This commit is contained in:
parent
052dd7cc9d
commit
4fc69b776c
|
@ -31,6 +31,7 @@ import type {
|
|||
IHttpRequestHelper,
|
||||
INodeTypeData,
|
||||
INodeTypes,
|
||||
ICredentialTestFunctions,
|
||||
} from 'n8n-workflow';
|
||||
import {
|
||||
ICredentialsHelper,
|
||||
|
@ -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: '',
|
||||
|
@ -466,6 +470,14 @@ 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,
|
||||
): ICredentialTestFunction | ICredentialTestRequestData | undefined {
|
||||
|
@ -496,6 +508,26 @@ 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 &&
|
||||
this.credentialTypes.getParentTypes(name).includes('oAuth2Api')
|
||||
) {
|
||||
return async function oauth2CredTest(
|
||||
this: ICredentialTestFunctions,
|
||||
cred: ICredentialsDecrypted,
|
||||
): Promise<INodeCredentialTestResult> {
|
||||
return CredentialsHelper.hasAccessToken(cred)
|
||||
? {
|
||||
status: 'OK',
|
||||
message: OAUTH2_CREDENTIAL_TEST_SUCCEEDED,
|
||||
}
|
||||
: {
|
||||
status: 'Error',
|
||||
message: OAUTH2_CREDENTIAL_TEST_FAILED,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
if (name === credentialType && !!testedBy) {
|
||||
if (typeof testedBy === 'string') {
|
||||
if (node instanceof VersionedNodeType) {
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { isObjectLiteral } from '@/utils';
|
||||
import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
||||
import type { MigrationContext, IrreversibleMigration } from '@db/types';
|
||||
|
||||
|
@ -5,10 +6,6 @@ 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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue