Parse single-line private key for Google service account (#2132)

*  Parse single-line private key

* ✏️ Update description and placeholder

*  Some improvements

Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Iván Ovejero
2021-12-24 16:12:18 +01:00
committed by GitHub
parent 231c760ef5
commit 26eac80d49
9 changed files with 93 additions and 29 deletions

View File

@@ -11,12 +11,20 @@ import {
ICredentialDataDecryptedObject,
IDataObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import * as moment from 'moment-timezone';
import * as jwt from 'jsonwebtoken';
interface IGoogleAuthCredentials {
delegatedEmail?: string;
email: string;
inpersonate: boolean;
privateKey: string;
}
export async function googleApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
@@ -46,8 +54,13 @@ export async function googleApiRequest(
try {
if (authenticationMethod === 'serviceAccount') {
const credentials = await this.getCredentials('googleApi') as { access_token: string, email: string, privateKey: string };
const { access_token } = await getAccessToken.call(this, credentials);
const credentials = await this.getCredentials('googleApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
const { access_token } = await getAccessToken.call(this, credentials as unknown as IGoogleAuthCredentials);
options.headers.Authorization = `Bearer ${access_token}`;
return await this.helpers.request!(options);
@@ -65,7 +78,7 @@ export async function googleApiRequest(
function getAccessToken(
this: IExecuteFunctions | ILoadOptionsFunctions,
credentials: ICredentialDataDecryptedObject,
credentials: IGoogleAuthCredentials,
) {
// https://developers.google.com/identity/protocols/oauth2/service-account#httprest
@@ -76,7 +89,8 @@ function getAccessToken(
const now = moment().unix();
const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n');
credentials.email = credentials.email.trim();
const privateKey = (credentials.privateKey as string).replace(/\\n/g, '\n').trim();
const signature = jwt.sign(
{