Some small changes to basic OAuth support

This commit is contained in:
Jan Oberhauser 2020-01-07 18:29:11 -06:00
parent 0c5972bb98
commit 740cb8a6fc
10 changed files with 224 additions and 186 deletions

View file

@ -2,6 +2,7 @@ import * as express from 'express';
import { import {
dirname as pathDirname, dirname as pathDirname,
join as pathJoin, join as pathJoin,
resolve as pathResolve,
} from 'path'; } from 'path';
import { import {
getConnectionManager, getConnectionManager,
@ -850,7 +851,7 @@ class App {
// ---------------------------------------- // ----------------------------------------
// Returns all the credential types which are defined in the loaded n8n-modules // Authorize OAuth Data
this.app.get('/rest/oauth2-credential/auth', ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<string> => { this.app.get('/rest/oauth2-credential/auth', ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<string> => {
if (req.query.id === undefined) { if (req.query.id === undefined) {
throw new Error('Required credential id is missing!'); throw new Error('Required credential id is missing!');
@ -877,13 +878,13 @@ class App {
throw new Error('Unable to read OAuth credentials'); throw new Error('Unable to read OAuth credentials');
} }
let token = new csrf(); const token = new csrf();
// Generate a CSRF prevention token and send it as a OAuth2 state stringma/ERR // Generate a CSRF prevention token and send it as a OAuth2 state stringma/ERR
oauthCredentials.csrfSecret = token.secretSync(); oauthCredentials.csrfSecret = token.secretSync();
const state = { const state = {
'token': token.create(oauthCredentials.csrfSecret), token: token.create(oauthCredentials.csrfSecret),
'cid': req.query.id cid: req.query.id
} };
const stateEncodedStr = Buffer.from(JSON.stringify(state)).toString('base64') as string; const stateEncodedStr = Buffer.from(JSON.stringify(state)).toString('base64') as string;
const oAuthObj = new clientOAuth2({ const oAuthObj = new clientOAuth2({
@ -891,9 +892,9 @@ class App {
clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string, clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string,
accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string, accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string,
authorizationUri: _.get(oauthCredentials, 'authUrl', '') as string, authorizationUri: _.get(oauthCredentials, 'authUrl', '') as string,
redirectUri: _.get(oauthCredentials, 'callbackUrl', WebhookHelpers.getWebhookBaseUrl()) as string, redirectUri: `${WebhookHelpers.getWebhookBaseUrl()}rest/oauth2-credential/callback`,
scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ','), scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ','),
state: stateEncodedStr state: stateEncodedStr,
}); });
credentials.setData(oauthCredentials, encryptionKey); credentials.setData(oauthCredentials, encryptionKey);
@ -913,42 +914,46 @@ class App {
// ---------------------------------------- // ----------------------------------------
// Verify and store app code. Generate access tokens and store for respective credential. // Verify and store app code. Generate access tokens and store for respective credential.
this.app.get('/rest/oauth2-credential/callback', ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<string> => { this.app.get('/rest/oauth2-credential/callback', async (req: express.Request, res: express.Response) => {
const {code, state: stateEncoded} = req.query; const {code, state: stateEncoded} = req.query;
if (code === undefined || stateEncoded === undefined) { if (code === undefined || stateEncoded === undefined) {
throw new Error('Insufficient parameters for OAuth2 callback') throw new Error('Insufficient parameters for OAuth2 callback');
} }
let state; let state;
try { try {
state = JSON.parse(Buffer.from(stateEncoded, 'base64').toString()); state = JSON.parse(Buffer.from(stateEncoded, 'base64').toString());
} catch (error) { } catch (error) {
throw new Error('Invalid state format returned'); const errorResponse = new ResponseHelper.ResponseError('Invalid state format returned', undefined, 503);
return ResponseHelper.sendErrorResponse(res, errorResponse);
} }
const result = await Db.collections.Credentials!.findOne(state.cid); const result = await Db.collections.Credentials!.findOne(state.cid);
if (result === undefined) { if (result === undefined) {
res.status(404).send('The credential is not known.'); const errorResponse = new ResponseHelper.ResponseError('The credential is not known.', undefined, 404);
return ''; return ResponseHelper.sendErrorResponse(res, errorResponse);
} }
let encryptionKey = undefined; let encryptionKey = undefined;
encryptionKey = await UserSettings.getEncryptionKey(); encryptionKey = await UserSettings.getEncryptionKey();
if (encryptionKey === undefined) { if (encryptionKey === undefined) {
throw new Error('No encryption key got found to decrypt the credentials!'); const errorResponse = new ResponseHelper.ResponseError('No encryption key got found to decrypt the credentials!', undefined, 503);
return ResponseHelper.sendErrorResponse(res, errorResponse);
} }
const credentials = new Credentials(result.name, result.type, result.nodesAccess, result.data); const credentials = new Credentials(result.name, result.type, result.nodesAccess, result.data);
(result as ICredentialsDecryptedDb).data = credentials.getData(encryptionKey!); (result as ICredentialsDecryptedDb).data = credentials.getData(encryptionKey!);
const oauthCredentials = (result as ICredentialsDecryptedDb).data; const oauthCredentials = (result as ICredentialsDecryptedDb).data;
if (oauthCredentials === undefined) { if (oauthCredentials === undefined) {
throw new Error('Unable to read OAuth credentials'); const errorResponse = new ResponseHelper.ResponseError('Unable to read OAuth credentials!', undefined, 503);
return ResponseHelper.sendErrorResponse(res, errorResponse);
} }
let token = new csrf(); const token = new csrf();
if (oauthCredentials.csrfSecret === undefined || !token.verify(oauthCredentials.csrfSecret as string, state.token)) { if (oauthCredentials.csrfSecret === undefined || !token.verify(oauthCredentials.csrfSecret as string, state.token)) {
res.status(404).send('The OAuth2 callback state is invalid.'); const errorResponse = new ResponseHelper.ResponseError('The OAuth2 callback state is invalid!', undefined, 404);
return ''; return ResponseHelper.sendErrorResponse(res, errorResponse);
} }
const oAuthObj = new clientOAuth2({ const oAuthObj = new clientOAuth2({
@ -956,13 +961,15 @@ class App {
clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string, clientSecret: _.get(oauthCredentials, 'clientSecret', '') as string,
accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string, accessTokenUri: _.get(oauthCredentials, 'accessTokenUrl', '') as string,
authorizationUri: _.get(oauthCredentials, 'authUrl', '') as string, authorizationUri: _.get(oauthCredentials, 'authUrl', '') as string,
redirectUri: _.get(oauthCredentials, 'callbackUrl', WebhookHelpers.getWebhookBaseUrl()) as string, redirectUri: `${WebhookHelpers.getWebhookBaseUrl()}rest/oauth2-credential/callback`,
scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ',') scopes: _.split(_.get(oauthCredentials, 'scope', 'openid,') as string, ',')
}); });
const oauthToken = await oAuthObj.code.getToken(req.originalUrl); const oauthToken = await oAuthObj.code.getToken(req.originalUrl);
if (oauthToken === undefined) { if (oauthToken === undefined) {
throw new Error('Unable to get access tokens'); const errorResponse = new ResponseHelper.ResponseError('Unable to get access tokens!', undefined, 404);
return ResponseHelper.sendErrorResponse(res, errorResponse);
} }
oauthCredentials.oauthTokenData = JSON.stringify(oauthToken.data); oauthCredentials.oauthTokenData = JSON.stringify(oauthToken.data);
@ -974,8 +981,9 @@ 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);
return 'Success!'; res.sendFile(pathResolve('templates/oauth-callback.html'));
})); });
// ---------------------------------------- // ----------------------------------------
// Executions // Executions

View file

@ -0,0 +1,9 @@
<html>
<script>
(function messageParent() {
window.opener.postMessage('success', '*');
}());
</script>
Got connected. The window can be closed now.
</html>

View file

@ -145,8 +145,8 @@ export interface IRestApi {
deleteExecutions(sendData: IExecutionDeleteFilter): Promise<void>; deleteExecutions(sendData: IExecutionDeleteFilter): Promise<void>;
retryExecution(id: string, loadWorkflow?: boolean): Promise<boolean>; retryExecution(id: string, loadWorkflow?: boolean): Promise<boolean>;
getTimezones(): Promise<IDataObject>; getTimezones(): Promise<IDataObject>;
OAuth2CredentialAuthorize(sendData: ICredentialsResponse): Promise<string>; oAuth2CredentialAuthorize(sendData: ICredentialsResponse): Promise<string>;
OAuth2Callback(code: string, state: string): Promise<string>; oAuth2Callback(code: string, state: string): Promise<string>;
} }
export interface IBinaryDisplayData { export interface IBinaryDisplayData {

View file

@ -13,6 +13,26 @@
</el-col> </el-col>
</el-row> </el-row>
<el-row v-if="isOAuthType" class="oauth-information">
<el-col :span="6" class="headline">
OAuth
</el-col>
<el-col :span="18">
<span v-if="isOAuthConnected === true">
<el-button title="Reconnect OAuth Credentials" @click.stop="oAuth2CredentialAuthorize()" circle>
<font-awesome-icon icon="redo" />
</el-button>
Is connected
</span>
<span v-else>
<el-button title="Connect OAuth Credentials" @click.stop="oAuth2CredentialAuthorize()" circle>
<font-awesome-icon icon="sign-in-alt" />
</el-button>
Is NOT connected
</span>
</el-col>
</el-row>
<br /> <br />
<div class="headline"> <div class="headline">
Credential Data: Credential Data:
@ -152,6 +172,16 @@ export default mixins(
}; };
}); });
}, },
isOAuthType (): boolean {
return this.credentialData && this.credentialData.type === 'oAuth2Api';
},
isOAuthConnected (): boolean {
if (this.isOAuthType === false) {
return false;
}
return !!this.credentialData.data.oauthTokenData;
},
}, },
methods: { methods: {
valueChanged (parameterData: IUpdateInformation) { valueChanged (parameterData: IUpdateInformation) {
@ -189,6 +219,48 @@ export default mixins(
this.$emit('credentialsCreated', result); this.$emit('credentialsCreated', result);
}, },
async oAuth2CredentialAuthorize () {
let url;
try {
url = await this.restApi().oAuth2CredentialAuthorize(this.credentialData) as string;
} catch (error) {
this.$showError(error, 'OAuth Authorization Error', 'Error generating authorization URL:');
return;
}
const params = `scrollbars=no,resizable=yes,status=no,titlebar=noe,location=no,toolbar=no,menubar=no,width=500,height=700`;
const oauthPopup = window.open(url, 'OAuth2 Authorization', params);
const receiveMessage = (event: MessageEvent) => {
// // TODO: Add check that it came from n8n
// if (event.origin !== 'http://example.org:8080') {
// return;
// }
if (event.data === 'success') {
// Set some kind of data that status changes.
// As data does not get displayed directly it does not matter what data.
this.credentialData.data.oauthTokenData = {};
// Close the window
if (oauthPopup) {
oauthPopup.close();
}
this.$showMessage({
title: 'Connected',
message: 'Got connected!',
type: 'success',
});
}
// Make sure that the event gets removed again
window.removeEventListener('message', receiveMessage, false);
};
window.addEventListener('message', receiveMessage, false);
},
async updateCredentials () { async updateCredentials () {
const nodesAccess: ICredentialNodeAccess[] = []; const nodesAccess: ICredentialNodeAccess[] = [];
const addedNodeTypes: string[] = []; const addedNodeTypes: string[] = [];
@ -301,6 +373,11 @@ export default mixins(
line-height: 1.75em; line-height: 1.75em;
} }
.oauth-information {
line-height: 2.5em;
margin-top: 2em;
}
.parameter-wrapper { .parameter-wrapper {
line-height: 3em; line-height: 3em;

View file

@ -25,12 +25,10 @@
<el-table-column property="updatedAt" label="Updated" class-name="clickable" sortable></el-table-column> <el-table-column property="updatedAt" label="Updated" class-name="clickable" sortable></el-table-column>
<el-table-column <el-table-column
label="Operations" label="Operations"
width="180"> width="120">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button title="Edit Credentials" @click.stop="editCredential(scope.row)" icon="el-icon-edit" circle></el-button> <el-button title="Edit Credentials" @click.stop="editCredential(scope.row)" icon="el-icon-edit" circle></el-button>
<el-button title="Delete Credentials" @click.stop="deleteCredential(scope.row)" type="danger" icon="el-icon-delete" circle></el-button> <el-button title="Delete Credentials" @click.stop="deleteCredential(scope.row)" type="danger" icon="el-icon-delete" circle></el-button>
<!-- Would be nice to have this button switch from connect to disconnect based on the credential status -->
<el-button title="Connect OAuth Credentials" @click.stop="OAuth2CredentialAuthorize(scope.row)" icon="el-icon-caret-right" v-if="scope.row.type == 'OAuth2Api'" circle></el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
@ -93,20 +91,6 @@ export default mixins(
this.editCredentials = null; this.editCredentials = null;
this.credentialEditDialogVisible = true; this.credentialEditDialogVisible = true;
}, },
async OAuth2CredentialAuthorize (credential: ICredentialsResponse) {
let url;
try {
url = await this.restApi().OAuth2CredentialAuthorize(credential) as string;
} catch (error) {
this.$showError(error, 'OAuth Authorization Error', 'Error generating authorization URL:');
return;
}
const params = `scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=0,height=0,left=-1000,top=-1000`;
const oauthPopup = window.open(url, 'OAuth2 Authorization', params);
console.log(oauthPopup);
},
editCredential (credential: ICredentialsResponse) { editCredential (credential: ICredentialsResponse) {
const editCredentials = { const editCredentials = {
id: credential.id, id: credential.id,

View file

@ -253,15 +253,15 @@ export const restApi = Vue.extend({
}, },
// Get OAuth2 Authorization URL using the stored credentials // Get OAuth2 Authorization URL using the stored credentials
OAuth2CredentialAuthorize: (sendData: ICredentialsResponse): Promise<string> => { oAuth2CredentialAuthorize: (sendData: ICredentialsResponse): Promise<string> => {
return self.restApi().makeRestApiRequest('GET', `/oauth2-credential/auth`, sendData); return self.restApi().makeRestApiRequest('GET', `/oauth2-credential/auth`, sendData);
}, },
// Verify OAuth2 provider callback and kick off token generation // Verify OAuth2 provider callback and kick off token generation
OAuth2Callback: (code: string, state: string): Promise<string> => { oAuth2Callback: (code: string, state: string): Promise<string> => {
const sendData = { const sendData = {
'code': code, 'code': code,
'state': state 'state': state,
}; };
return self.restApi().makeRestApiRequest('POST', `/oauth2-credential/callback`, sendData); return self.restApi().makeRestApiRequest('POST', `/oauth2-credential/callback`, sendData);

View file

@ -71,6 +71,7 @@ import {
faSave, faSave,
faSearchMinus, faSearchMinus,
faSearchPlus, faSearchPlus,
faSignInAlt,
faSlidersH, faSlidersH,
faSpinner, faSpinner,
faStop, faStop,
@ -145,6 +146,7 @@ library.add(faRss);
library.add(faSave); library.add(faSave);
library.add(faSearchMinus); library.add(faSearchMinus);
library.add(faSearchPlus); library.add(faSearchPlus);
library.add(faSignInAlt);
library.add(faSlidersH); library.add(faSlidersH);
library.add(faSpinner); library.add(faSpinner);
library.add(faStop); library.add(faStop);

View file

@ -21,7 +21,7 @@ export default new Router({
}, },
{ {
path: '/oauth2/callback', path: '/oauth2/callback',
name: 'OAuth2Callback', name: 'oAuth2Callback',
components: { components: {
}, },
}, },

View file

@ -1,56 +1,49 @@
import { import {
ICredentialType, ICredentialType,
NodePropertyTypes, NodePropertyTypes,
} from 'n8n-workflow'; } from 'n8n-workflow';
export class OAuth2Api implements ICredentialType { export class OAuth2Api implements ICredentialType {
name = 'OAuth2Api'; name = 'oAuth2Api';
displayName = 'OAuth2 API'; displayName = 'OAuth2 API';
properties = [ properties = [
{ {
displayName: 'Authorization URL', displayName: 'Authorization URL',
name: 'authUrl', name: 'authUrl',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
required: true, required: true,
}, },
{ {
displayName: 'Access Token URL', displayName: 'Access Token URL',
name: 'accessTokenUrl', name: 'accessTokenUrl',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
required: true, required: true,
}, },
{ {
displayName: 'Callback URL', displayName: 'Client ID',
name: 'callbackUrl', name: 'clientId',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
required: true, required: true,
}, },
{ {
displayName: 'Client ID', displayName: 'Client Secret',
name: 'clientId', name: 'clientSecret',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', typeOptions: {
required: true, password: true,
}, },
{ default: '',
displayName: 'Client Secret', required: true,
name: 'clientSecret', },
type: 'string' as NodePropertyTypes, {
typeOptions: { displayName: 'Scope',
password: true, name: 'scope',
}, type: 'string' as NodePropertyTypes,
default: '', default: '',
required: true, },
}, ];
{
displayName: 'Scope',
name: 'scope',
type: 'string' as NodePropertyTypes,
default: '',
},
];
} }

View file

@ -1,104 +1,69 @@
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
GenericValue, INodeExecutionData,
IDataObject, INodeType,
INodeExecutionData, INodeTypeDescription,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { set } from 'lodash';
import * as util from 'util';
import { connectionFields } from './ActiveCampaign/ConnectionDescription';
export class OAuth implements INodeType { export class OAuth implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'OAuth', displayName: 'OAuth',
name: 'oauth', name: 'oauth',
icon: 'fa:code-branch', icon: 'fa:code-branch',
group: ['input'], group: ['input'],
version: 1, version: 1,
description: 'Gets, sends data to Oauth API Endpoint and receives generic information.', description: 'Gets, sends data to Oauth API Endpoint and receives generic information.',
defaults: { defaults: {
name: 'OAuth', name: 'OAuth',
color: '#0033AA', color: '#0033AA',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
credentials: [ credentials: [
{ {
name: 'OAuth2Api', name: 'oAuth2Api',
required: true, required: true,
} }
], ],
properties: [ properties: [
{ {
displayName: 'Operation', displayName: 'Operation',
name: 'operation', name: 'operation',
type: 'options', type: 'options',
options: [ options: [
{ {
name: 'Get', name: 'Get',
value: 'get', value: 'get',
description: 'Returns the value of a key from oauth.', description: 'Returns the OAuth token data.',
}, },
], ],
default: 'get', default: 'get',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
// ---------------------------------- ]
// get };
// ----------------------------------
{
displayName: 'Name',
name: 'propertyName',
type: 'string',
displayOptions: {
show: {
operation: [
'get'
],
},
},
default: 'propertyName',
required: true,
description: 'Name of the property to write received data to.<br />Supports dot-notation.<br />Example: "data.person[0].name"',
},
]
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = this.getCredentials('OAuth2Api'); const credentials = this.getCredentials('oAuth2Api');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
if (credentials.oauthTokenData === undefined) { if (credentials.oauthTokenData === undefined) {
throw new Error('OAuth credentials not connected'); throw new Error('OAuth credentials not connected');
} }
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
if (operation === 'get') { if (operation === 'get') {
const items = this.getInputData(); // credentials.oauthTokenData has the refreshToken and accessToken available
const returnItems: INodeExecutionData[] = []; // it would be nice to have credentials.getOAuthToken() which returns the accessToken
// and also handles an error case where if the token is to be refreshed, it does so
// without knowledge of the node.
let item: INodeExecutionData; return [this.helpers.returnJsonArray(JSON.parse(credentials.oauthTokenData as string))];
} else {
// credentials.oauthTokenData has the refreshToken and accessToken available throw new Error('Unknown operation');
// it would be nice to have credentials.getOAuthToken() which returns the accessToken }
// and also handles an error case where if the token is to be refreshed, it does so }
// without knowledge of the node.
console.log('Got OAuth credentials!', credentials.oauthTokenData);
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
item = { json: { itemIndex } };
returnItems.push(item);
}
return [returnItems];
} else {
throw new Error('Unknown operation');
}
}
} }