From 185bc04436a2cb565469c16995fd27822d4d686e Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Fri, 24 Dec 2021 10:48:23 +0100 Subject: [PATCH] :zap: Automatically remove new lines from Google credentials --- .../nodes-base/nodes/Google/Books/GenericFunctions.ts | 6 ++++-- .../nodes-base/nodes/Google/Docs/GenericFunctions.ts | 6 ++++-- .../nodes-base/nodes/Google/Drive/GenericFunctions.ts | 8 +++++--- .../nodes-base/nodes/Google/Gmail/GenericFunctions.ts | 7 ++++--- .../nodes-base/nodes/Google/Sheet/GenericFunctions.ts | 6 ++++-- .../nodes-base/nodes/Google/Slides/GenericFunctions.ts | 9 ++++++--- .../nodes/Google/Translate/GenericFunctions.ts | 6 ++++-- 7 files changed, 31 insertions(+), 17 deletions(-) diff --git a/packages/nodes-base/nodes/Google/Books/GenericFunctions.ts b/packages/nodes-base/nodes/Google/Books/GenericFunctions.ts index 830eff470..c3ed803d3 100644 --- a/packages/nodes-base/nodes/Google/Books/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Google/Books/GenericFunctions.ts @@ -87,6 +87,8 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa const now = moment().unix(); + const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n'); + const signature = jwt.sign( { 'iss': credentials.email as string, @@ -96,11 +98,11 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa 'iat': now, 'exp': now + 3600, }, - credentials.privateKey as string, + privateKey as string, { algorithm: 'RS256', header: { - 'kid': credentials.privateKey as string, + 'kid': privateKey as string, 'typ': 'JWT', 'alg': 'RS256', }, diff --git a/packages/nodes-base/nodes/Google/Docs/GenericFunctions.ts b/packages/nodes-base/nodes/Google/Docs/GenericFunctions.ts index 736845d2d..7e64e2794 100644 --- a/packages/nodes-base/nodes/Google/Docs/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Google/Docs/GenericFunctions.ts @@ -95,6 +95,8 @@ function getAccessToken(this: IExecuteFunctions | ILoadOptionsFunctions, credent const now = moment().unix(); + const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n'); + const signature = jwt.sign( { 'iss': credentials.email as string, @@ -104,11 +106,11 @@ function getAccessToken(this: IExecuteFunctions | ILoadOptionsFunctions, credent 'iat': now, 'exp': now + 3600, }, - credentials.privateKey as string, + privateKey as string, { algorithm: 'RS256', header: { - 'kid': credentials.privateKey as string, + 'kid': privateKey as string, 'typ': 'JWT', 'alg': 'RS256', }, diff --git a/packages/nodes-base/nodes/Google/Drive/GenericFunctions.ts b/packages/nodes-base/nodes/Google/Drive/GenericFunctions.ts index 185e00da2..d762a5f07 100644 --- a/packages/nodes-base/nodes/Google/Drive/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Google/Drive/GenericFunctions.ts @@ -32,7 +32,7 @@ export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleF uri: uri || `https://www.googleapis.com${resource}`, json: true, }; - + options = Object.assign({}, options, option); try { @@ -94,6 +94,8 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa const now = moment().unix(); + const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n'); + const signature = jwt.sign( { 'iss': credentials.email as string, @@ -103,11 +105,11 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa 'iat': now, 'exp': now + 3600, }, - credentials.privateKey as string, + privateKey as string, { algorithm: 'RS256', header: { - 'kid': credentials.privateKey as string, + 'kid': privateKey as string, 'typ': 'JWT', 'alg': 'RS256', }, diff --git a/packages/nodes-base/nodes/Google/Gmail/GenericFunctions.ts b/packages/nodes-base/nodes/Google/Gmail/GenericFunctions.ts index 3301d5731..353cbf5f9 100644 --- a/packages/nodes-base/nodes/Google/Gmail/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Google/Gmail/GenericFunctions.ts @@ -216,6 +216,8 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa const now = moment().unix(); + const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n'); + const signature = jwt.sign( { 'iss': credentials.email as string, @@ -225,11 +227,11 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa 'iat': now, 'exp': now + 3600, }, - credentials.privateKey as string, + privateKey, { algorithm: 'RS256', header: { - 'kid': credentials.privateKey as string, + 'kid': privateKey, 'typ': 'JWT', 'alg': 'RS256', }, @@ -252,4 +254,3 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa //@ts-ignore return this.helpers.request(options); } - diff --git a/packages/nodes-base/nodes/Google/Sheet/GenericFunctions.ts b/packages/nodes-base/nodes/Google/Sheet/GenericFunctions.ts index bd5113d82..96d8a6530 100644 --- a/packages/nodes-base/nodes/Google/Sheet/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Google/Sheet/GenericFunctions.ts @@ -93,6 +93,8 @@ export function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions const now = moment().unix(); + const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n'); + const signature = jwt.sign( { 'iss': credentials.email as string, @@ -102,11 +104,11 @@ export function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions 'iat': now, 'exp': now + 3600, }, - credentials.privateKey as string, + privateKey, { algorithm: 'RS256', header: { - 'kid': credentials.privateKey as string, + 'kid': privateKey, 'typ': 'JWT', 'alg': 'RS256', }, diff --git a/packages/nodes-base/nodes/Google/Slides/GenericFunctions.ts b/packages/nodes-base/nodes/Google/Slides/GenericFunctions.ts index 245cc2467..71f950568 100644 --- a/packages/nodes-base/nodes/Google/Slides/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Google/Slides/GenericFunctions.ts @@ -8,6 +8,7 @@ import { } from 'n8n-core'; import { + ICredentialDataDecryptedObject, IDataObject, NodeApiError, } from 'n8n-workflow'; @@ -64,7 +65,7 @@ export async function googleApiRequest( function getAccessToken( this: IExecuteFunctions | ILoadOptionsFunctions, - { email, privateKey }: { email: string, privateKey: string }, + credentials: ICredentialDataDecryptedObject, ) { // https://developers.google.com/identity/protocols/oauth2/service-account#httprest @@ -75,10 +76,12 @@ function getAccessToken( const now = moment().unix(); + const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n'); + const signature = jwt.sign( { - iss: email, - sub: email, + iss: credentials.email, + sub: credentials.email, scope: scopes.join(' '), aud: 'https://oauth2.googleapis.com/token', iat: now, diff --git a/packages/nodes-base/nodes/Google/Translate/GenericFunctions.ts b/packages/nodes-base/nodes/Google/Translate/GenericFunctions.ts index ec5bbaf27..5bc3b9831 100644 --- a/packages/nodes-base/nodes/Google/Translate/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Google/Translate/GenericFunctions.ts @@ -86,6 +86,8 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa const now = moment().unix(); + const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n'); + const signature = jwt.sign( { 'iss': credentials.email as string, @@ -95,11 +97,11 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa 'iat': now, 'exp': now + 3600, }, - credentials.privateKey as string, + privateKey as string, { algorithm: 'RS256', header: { - 'kid': credentials.privateKey as string, + 'kid': privateKey as string, 'typ': 'JWT', 'alg': 'RS256', },