Gitlab OAuth2 support

This commit is contained in:
Rupenieks 2020-06-16 15:50:17 +02:00
parent 346ae8efc9
commit 39f5cf92d8
5 changed files with 176 additions and 16 deletions

View file

@ -0,0 +1,53 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class GitlabOAuth2Api implements ICredentialType {
name = 'gitlabOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'Gitlab OAuth2 API';
properties = [
{
displayName: 'Gitlab Server',
name: 'server',
type: 'string' as NodePropertyTypes,
default: 'https://gitlab.com'
},
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://gitlab.com/oauth/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://gitlab.com/oauth/token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'string' as NodePropertyTypes,
default: 'api',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden' as NodePropertyTypes,
default: 'body',
},
];
}

View file

@ -6,6 +6,7 @@ import {
import {
IDataObject,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
/**
* Make an API request to Gitlab
@ -17,24 +18,43 @@ import {
* @returns {Promise<any>}
*/
export async function gitlabApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('gitlabApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const options = {
const options : OptionsWithUri = {
method,
headers: {
'Private-Token': `${credentials.accessToken}`,
},
headers: {},
body,
qs: query,
uri: `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`,
uri: '',
json: true
};
if (query === undefined) {
delete options.qs;
}
const authenticationMethod = this.getNodeParameter('authentication', 0);
try {
return await this.helpers.request(options);
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('gitlabApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.headers!['Private-Token'] = `${credentials.accessToken}`;
options.uri = `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`;
return await this.helpers.request(options);
} else {
const credentials = this.getCredentials('gitlabOAuth2Api');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.uri = `${(credentials.server as string).replace(/\/$/, '')}/api/v4${endpoint}`;
return await this.helpers.requestOAuth2!.call(this, 'gitlabOAuth2Api', options);
}
} catch (error) {
if (error.statusCode === 401) {
// Return a clear error

View file

@ -33,9 +33,44 @@ export class Gitlab implements INodeType {
{
name: 'gitlabApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'gitlabOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
description: 'The resource to operate on.',
},
{
displayName: 'Resource',
name: 'resource',
@ -793,10 +828,26 @@ export class Gitlab implements INodeType {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const credentials = this.getCredentials('gitlabApi');
let credentials;
if (credentials === undefined) {
throw new Error('No credentials got returned!');
const authenticationMethod = this.getNodeParameter('authentication', 0);
try {
if (authenticationMethod === 'accessToken') {
credentials = this.getCredentials('gitlabApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
} else {
credentials = this.getCredentials('gitlabOAuth2Api');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
}
} catch (error) {
throw new Error(error);
}
// Operations which overwrite the returned data

View file

@ -34,7 +34,25 @@ export class GitlabTrigger implements INodeType {
{
name: 'gitlabApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'gitlabOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
{
@ -45,6 +63,23 @@ export class GitlabTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Access Token',
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'accessToken',
description: 'The resource to operate on.',
},
{
displayName: 'Repository Owner',
name: 'owner',

View file

@ -56,6 +56,7 @@
"dist/credentials/GithubApi.credentials.js",
"dist/credentials/GithubOAuth2Api.credentials.js",
"dist/credentials/GitlabApi.credentials.js",
"dist/credentials/GitlabOAuth2Api.credentials.js",
"dist/credentials/GoogleApi.credentials.js",
"dist/credentials/GoogleCalendarOAuth2Api.credentials.js",
"dist/credentials/GoogleDriveOAuth2Api.credentials.js",