diff --git a/packages/cli/src/CredentialsHelper.ts b/packages/cli/src/CredentialsHelper.ts index a5dee733f2..d9ee2b6baf 100644 --- a/packages/cli/src/CredentialsHelper.ts +++ b/packages/cli/src/CredentialsHelper.ts @@ -66,6 +66,12 @@ export class CredentialsHelper extends ICredentialsHelper { async updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise { const credentials = await this.getCredentials(name, type); + if (Db.collections!.Credentials === null) { + // The first time executeWorkflow gets called the Database has + // to get initialized first + await Db.init(); + } + credentials.setData(data, this.encryptionKey); const newCredentialsData = credentials.getDataToSave() as ICredentialsDb; @@ -75,7 +81,12 @@ export class CredentialsHelper extends ICredentialsHelper { // TODO: also add user automatically depending on who is logged in, if anybody is logged in // Save the credentials in DB - await Db.collections.Credentials!.save(newCredentialsData); + const findQuery = { + name, + type, + }; + + await Db.collections.Credentials!.update(findQuery, newCredentialsData); } } diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 1d82c4d807..365bb1765b 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -969,7 +969,7 @@ class App { return ResponseHelper.sendErrorResponse(res, errorResponse); } - savedCredentialsData.oauthTokenData = JSON.stringify(oauthToken.data); + savedCredentialsData.oauthTokenData = oauthToken.data; _.unset(savedCredentialsData, 'csrfSecret'); credentials.setData(savedCredentialsData, encryptionKey); diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 2c13b2c389..69dd2c92e1 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -36,8 +36,8 @@ import { } from 'n8n-workflow'; import * as clientOAuth2 from 'client-oauth2'; -import { get, unset } from 'lodash'; -import * as express from "express"; +import { get } from 'lodash'; +import * as express from 'express'; import * as path from 'path'; import { OptionsWithUri } from 'request'; import * as requestPromise from 'request-promise-native'; @@ -126,8 +126,13 @@ export function requestOAuth(this: IAllExecuteFunctions, credentialsType: string throw new Error('OAuth credentials not connected!'); } - const oAuthClient = new clientOAuth2({}); - const oauthTokenData = JSON.parse(credentials.oauthTokenData as string); + const oAuthClient = new clientOAuth2({ + clientId: credentials.clientId as string, + clientSecret: credentials.clientSecret as string, + accessTokenUri: credentials.accessTokenUrl as string, + }); + + const oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data; const token = oAuthClient.createToken(oauthTokenData); // Signs the request by adding authorization headers or query parameters depending @@ -142,9 +147,7 @@ export function requestOAuth(this: IAllExecuteFunctions, credentialsType: string // Token is probably not valid anymore. So try refresh it. const newToken = await token.refresh(); - const newCredentialsData = newToken.data; - unset(newCredentialsData, 'csrfSecret'); - unset(newCredentialsData, 'oauthTokenData'); + credentials.oauthTokenData = newToken.data; // Find the name of the credentials if (!node.credentials || !node.credentials[credentialsType]) { @@ -153,7 +156,7 @@ export function requestOAuth(this: IAllExecuteFunctions, credentialsType: string const name = node.credentials[credentialsType]; // Save the refreshed token - await additionalData.credentialsHelper.updateCredentials(name, credentialsType, newCredentialsData); + await additionalData.credentialsHelper.updateCredentials(name, credentialsType, credentials); // Make the request again with the new token const newRequestOptions = newToken.sign(requestOptions as clientOAuth2.RequestObject);