mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-13 05:47:31 -08:00
🔀 Merge branch 'RicardoE105-feature/github-oauth' into oauth-support
This commit is contained in:
commit
6e6ece1bcb
|
@ -979,7 +979,7 @@ class App {
|
||||||
// Save the credentials in DB
|
// Save the credentials in DB
|
||||||
await Db.collections.Credentials!.update(state.cid, newCredentialsData);
|
await Db.collections.Credentials!.update(state.cid, newCredentialsData);
|
||||||
|
|
||||||
res.sendFile(pathResolve('templates/oauth-callback.html'));
|
res.sendFile(pathResolve(__dirname, '../../templates/oauth-callback.html'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -34,11 +34,11 @@ import CredentialsInput from '@/components/CredentialsInput.vue';
|
||||||
import {
|
import {
|
||||||
ICredentialsCreatedEvent,
|
ICredentialsCreatedEvent,
|
||||||
ICredentialsDecryptedResponse,
|
ICredentialsDecryptedResponse,
|
||||||
INodeProperties,
|
|
||||||
} from '@/Interface';
|
} from '@/Interface';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ICredentialType,
|
ICredentialType,
|
||||||
|
INodeProperties,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import mixins from 'vue-typed-mixins';
|
import mixins from 'vue-typed-mixins';
|
||||||
|
@ -172,6 +172,42 @@ export default mixins(
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
mergeCredentialProperties (mainProperties: INodeProperties[], addProperties: INodeProperties[]): void {
|
||||||
|
let existingIndex: number;
|
||||||
|
for (const property of addProperties) {
|
||||||
|
existingIndex = mainProperties.findIndex(element => element.name === property.name);
|
||||||
|
|
||||||
|
if (existingIndex === -1) {
|
||||||
|
// Property does not exist yet, so add
|
||||||
|
mainProperties.push(property);
|
||||||
|
} else {
|
||||||
|
// Property exists already, so overwrite
|
||||||
|
mainProperties[existingIndex] = property;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getCredentialProperties (name: string): INodeProperties[] {
|
||||||
|
const credentialsData = this.$store.getters.credentialType(name);
|
||||||
|
|
||||||
|
if (credentialsData === null) {
|
||||||
|
throw new Error(`Could not find credentials of type: ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (credentialsData.extends === undefined) {
|
||||||
|
return credentialsData.properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
const combineProperties = [] as INodeProperties[];
|
||||||
|
for (const credentialsTypeName of credentialsData.extends) {
|
||||||
|
const mergeCredentialProperties = this.getCredentialProperties(credentialsTypeName);
|
||||||
|
this.mergeCredentialProperties(combineProperties, mergeCredentialProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The properties defined on the parent credentials take presidence
|
||||||
|
this.mergeCredentialProperties(combineProperties, credentialsData.properties);
|
||||||
|
|
||||||
|
return combineProperties;
|
||||||
|
},
|
||||||
getCredentialTypeData (name: string): ICredentialType | null {
|
getCredentialTypeData (name: string): ICredentialType | null {
|
||||||
let credentialData = this.$store.getters.credentialType(name);
|
let credentialData = this.$store.getters.credentialType(name);
|
||||||
|
|
||||||
|
@ -179,25 +215,14 @@ export default mixins(
|
||||||
return credentialData;
|
return credentialData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: The credential-extend-resolve-logic is currently not needed in the backend
|
||||||
|
// as the whole credential data gets saved with the defaults. That logic should,
|
||||||
|
// however, probably also get improved in the future.
|
||||||
|
|
||||||
// Credentials extends another one. So get the properties of the one it
|
// Credentials extends another one. So get the properties of the one it
|
||||||
// extends and add them.
|
// extends and add them.
|
||||||
credentialData = JSON.parse(JSON.stringify(credentialData));
|
credentialData = JSON.parse(JSON.stringify(credentialData));
|
||||||
let existingIndex: number;
|
credentialData.properties = this.getCredentialProperties(credentialData.name);
|
||||||
for (const credentialTypeName of credentialData.extends) {
|
|
||||||
const data = this.$store.getters.credentialType(credentialTypeName);
|
|
||||||
|
|
||||||
for (const property of data.properties) {
|
|
||||||
existingIndex = credentialData.properties.findIndex(element => element.name === property.name);
|
|
||||||
|
|
||||||
if (existingIndex === -1) {
|
|
||||||
// Property does not exist yet, so add
|
|
||||||
credentialData.properties.push(property);
|
|
||||||
} else {
|
|
||||||
// Property exists already, so overwrite
|
|
||||||
credentialData.properties[existingIndex] = property;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return credentialData;
|
return credentialData;
|
||||||
},
|
},
|
||||||
|
|
|
@ -41,18 +41,21 @@
|
||||||
<font-awesome-icon icon="question-circle" />
|
<font-awesome-icon icon="question-circle" />
|
||||||
</el-tooltip>
|
</el-tooltip>
|
||||||
</div>
|
</div>
|
||||||
<el-row v-for="parameter in credentialProperties" :key="parameter.name" class="parameter-wrapper">
|
<div v-for="parameter in credentialProperties" :key="parameter.name">
|
||||||
<el-col :span="6" class="parameter-name">
|
<el-row v-if="displayNodeParameter(parameter)" class="parameter-wrapper">
|
||||||
{{parameter.displayName}}:
|
<el-col :span="6" class="parameter-name">
|
||||||
<el-tooltip placement="top" class="parameter-info" v-if="parameter.description" effect="light">
|
{{parameter.displayName}}:
|
||||||
<div slot="content" v-html="parameter.description"></div>
|
<el-tooltip placement="top" class="parameter-info" v-if="parameter.description" effect="light">
|
||||||
<font-awesome-icon icon="question-circle"/>
|
<div slot="content" v-html="parameter.description"></div>
|
||||||
</el-tooltip>
|
<font-awesome-icon icon="question-circle"/>
|
||||||
</el-col>
|
</el-tooltip>
|
||||||
<el-col :span="18">
|
</el-col>
|
||||||
<parameter-input :parameter="parameter" :value="propertyValue[parameter.name]" :path="parameter.name" :isCredential="true" @valueChanged="valueChanged" />
|
<el-col :span="18">
|
||||||
</el-col>
|
<parameter-input :parameter="parameter" :value="propertyValue[parameter.name]" :path="parameter.name" :isCredential="true" @valueChanged="valueChanged" />
|
||||||
</el-row>
|
</el-col>
|
||||||
|
</el-row>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<el-row class="nodes-access-wrapper">
|
<el-row class="nodes-access-wrapper">
|
||||||
<el-col :span="6" class="headline">
|
<el-col :span="6" class="headline">
|
||||||
|
@ -239,6 +242,13 @@ export default mixins(
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
displayNodeParameter (parameter: INodeProperties): boolean {
|
||||||
|
if (parameter.type === 'hidden') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
async oAuth2CredentialAuthorize () {
|
async oAuth2CredentialAuthorize () {
|
||||||
let url;
|
let url;
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,26 @@ export class GithubOAuth2Api implements ICredentialType {
|
||||||
];
|
];
|
||||||
displayName = 'Github OAuth2 API';
|
displayName = 'Github OAuth2 API';
|
||||||
properties = [
|
properties = [
|
||||||
|
{
|
||||||
|
displayName: 'Authorization URL',
|
||||||
|
name: 'authUrl',
|
||||||
|
type: 'hidden' as NodePropertyTypes,
|
||||||
|
default: 'https://github.com/login/oauth/authorize',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Access Token URL',
|
||||||
|
name: 'accessTokenUrl',
|
||||||
|
type: 'hidden' as NodePropertyTypes,
|
||||||
|
default: 'https://github.com/login/oauth/access_token',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
displayName: 'Scope',
|
||||||
|
name: 'scope',
|
||||||
|
type: 'hidden' as NodePropertyTypes,
|
||||||
|
default: 'repo,admin:repo_hook,admin:org,admin:org_hook,gist,notifications,user,write:packages,read:packages,delete:packages,worfklow',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Auth URI Query Parameters',
|
displayName: 'Auth URI Query Parameters',
|
||||||
name: 'authQueryParameters',
|
name: 'authQueryParameters',
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { OptionsWithUri } from 'request';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
IHookFunctions,
|
IHookFunctions,
|
||||||
|
@ -17,16 +19,12 @@ import {
|
||||||
* @returns {Promise<any>}
|
* @returns {Promise<any>}
|
||||||
*/
|
*/
|
||||||
export async function githubApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
|
export async function githubApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
|
||||||
const credentials = this.getCredentials('githubApi');
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'accessToken') as string;
|
||||||
if (credentials === undefined) {
|
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = {
|
const options: OptionsWithUri = {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `token ${credentials.accessToken}`,
|
'User-Agent': 'n8n',
|
||||||
'User-Agent': credentials.user,
|
|
||||||
},
|
},
|
||||||
body,
|
body,
|
||||||
qs: query,
|
qs: query,
|
||||||
|
@ -35,7 +33,16 @@ export async function githubApiRequest(this: IHookFunctions | IExecuteFunctions,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.helpers.request(options);
|
if (authenticationMethod === 'accessToken') {
|
||||||
|
const credentials = this.getCredentials('githubApi');
|
||||||
|
if (credentials === undefined) {
|
||||||
|
throw new Error('No credentials got returned!');
|
||||||
|
}
|
||||||
|
options.headers!.Authorization = `token ${credentials.accessToken}`;
|
||||||
|
return await this.helpers.request(options);
|
||||||
|
} else {
|
||||||
|
return await this.helpers.requestOAuth.call(this, 'githubOAuth2Api', options);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.statusCode === 401) {
|
if (error.statusCode === 401) {
|
||||||
// Return a clear error
|
// Return a clear error
|
||||||
|
|
|
@ -33,9 +33,44 @@ export class Github implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'githubApi',
|
name: 'githubApi',
|
||||||
required: true,
|
required: true,
|
||||||
}
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'accessToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'githubOAuth2Api',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'oauth2',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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',
|
displayName: 'Resource',
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
|
@ -1088,12 +1123,6 @@ export class Github implements INodeType {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
const credentials = this.getCredentials('githubApi');
|
|
||||||
|
|
||||||
if (credentials === undefined) {
|
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Operations which overwrite the returned data
|
// Operations which overwrite the returned data
|
||||||
const overwriteDataOperations = [
|
const overwriteDataOperations = [
|
||||||
'file:create',
|
'file:create',
|
||||||
|
|
Loading…
Reference in a new issue