mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ credentials update
This commit is contained in:
parent
e6fef4b494
commit
2ca27006fd
|
@ -1,4 +1,6 @@
|
||||||
import {
|
import {
|
||||||
|
IAuthenticateHeaderAuth,
|
||||||
|
ICredentialTestRequest,
|
||||||
ICredentialType,
|
ICredentialType,
|
||||||
INodeProperties,
|
INodeProperties,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
@ -28,4 +30,18 @@ export class GithubApi implements ICredentialType {
|
||||||
default: '',
|
default: '',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
authenticate: IAuthenticateHeaderAuth = {
|
||||||
|
type: 'headerAuth',
|
||||||
|
properties: {
|
||||||
|
name: 'Authorization',
|
||||||
|
value: '=token {{$credentials?.accessToken}}',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
test: ICredentialTestRequest = {
|
||||||
|
request: {
|
||||||
|
baseURL: '={{$credentials?.server}}',
|
||||||
|
url: '/user',
|
||||||
|
method: 'GET',
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,23 +37,23 @@ export async function githubApiRequest(this: IHookFunctions | IExecuteFunctions,
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const authenticationMethod = this.getNodeParameter('authentication', 0, 'accessToken') as string;
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'accessToken') as string;
|
||||||
|
let credentialType = '';
|
||||||
|
|
||||||
if (authenticationMethod === 'accessToken') {
|
if (authenticationMethod === 'accessToken') {
|
||||||
const credentials = await this.getCredentials('githubApi');
|
const credentials = await this.getCredentials('githubApi');
|
||||||
|
credentialType = 'githubApi';
|
||||||
|
|
||||||
const baseUrl = credentials!.server || 'https://api.github.com';
|
const baseUrl = credentials!.server || 'https://api.github.com';
|
||||||
options.uri = `${baseUrl}${endpoint}`;
|
options.uri = `${baseUrl}${endpoint}`;
|
||||||
|
|
||||||
options.headers!.Authorization = `token ${credentials.accessToken}`;
|
|
||||||
return await this.helpers.request(options);
|
|
||||||
} else {
|
} else {
|
||||||
const credentials = await this.getCredentials('githubOAuth2Api');
|
const credentials = await this.getCredentials('githubOAuth2Api');
|
||||||
|
credentialType = 'githubOAuth2Api';
|
||||||
|
|
||||||
const baseUrl = credentials.server || 'https://api.github.com';
|
const baseUrl = credentials.server || 'https://api.github.com';
|
||||||
options.uri = `${baseUrl}${endpoint}`;
|
options.uri = `${baseUrl}${endpoint}`;
|
||||||
//@ts-ignore
|
|
||||||
return await this.helpers.requestOAuth2.call(this, 'githubOAuth2Api', options);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new NodeApiError(this.getNode(), error);
|
throw new NodeApiError(this.getNode(), error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
import {
|
|
||||||
OptionsWithUri,
|
|
||||||
} from 'request';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ICredentialsDecrypted,
|
|
||||||
ICredentialTestFunctions,
|
|
||||||
IDataObject,
|
IDataObject,
|
||||||
INodeCredentialTestResult,
|
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
|
@ -45,7 +38,6 @@ export class Github implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'githubApi',
|
name: 'githubApi',
|
||||||
required: true,
|
required: true,
|
||||||
testedBy: 'githubApiTest',
|
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
authentication: ['accessToken'],
|
authentication: ['accessToken'],
|
||||||
|
@ -1594,47 +1586,6 @@ export class Github implements INodeType {
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
methods = {
|
|
||||||
credentialTest: {
|
|
||||||
async githubApiTest(
|
|
||||||
this: ICredentialTestFunctions,
|
|
||||||
credential: ICredentialsDecrypted,
|
|
||||||
): Promise<INodeCredentialTestResult> {
|
|
||||||
const credentials = credential.data;
|
|
||||||
const baseUrl = (credentials!.server as string) || 'https://api.github.com';
|
|
||||||
|
|
||||||
const options: OptionsWithUri = {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
'User-Agent': 'n8n',
|
|
||||||
Authorization: `token ${credentials!.accessToken}`,
|
|
||||||
},
|
|
||||||
uri: baseUrl.endsWith('/') ? baseUrl + 'user' : baseUrl + '/user',
|
|
||||||
json: true,
|
|
||||||
timeout: 5000,
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
const response = await this.helpers.request(options);
|
|
||||||
if (!response.id) {
|
|
||||||
return {
|
|
||||||
status: 'Error',
|
|
||||||
message: `Token is not valid: ${response.error}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
status: 'Error',
|
|
||||||
message: `Settings are not valid: ${error}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
status: 'OK',
|
|
||||||
message: 'Authentication successful!',
|
|
||||||
};
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
Loading…
Reference in a new issue