refactor(core): Google service account remove duplicated functions (no-changelog) (#6368)

This commit is contained in:
Michael Kret
2023-06-06 19:19:24 +03:00
committed by GitHub
parent 3a6af3b2a2
commit 05c4229cd7
14 changed files with 134 additions and 719 deletions

View File

@@ -8,72 +8,12 @@ import type {
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import moment from 'moment-timezone';
import jwt from 'jsonwebtoken';
interface IGoogleAuthCredentials {
delegatedEmail?: string;
email: string;
inpersonate: boolean;
privateKey: string;
}
async function getAccessToken(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
credentials: IGoogleAuthCredentials,
): Promise<IDataObject> {
//https://developers.google.com/identity/protocols/oauth2/service-account#httprest
const scopes = ['https://www.googleapis.com/auth/books'];
const now = moment().unix();
credentials.email = credentials.email.trim();
const privateKey = credentials.privateKey.replace(/\\n/g, '\n').trim();
const signature = jwt.sign(
{
iss: credentials.email,
sub: credentials.delegatedEmail || credentials.email,
scope: scopes.join(' '),
aud: 'https://oauth2.googleapis.com/token',
iat: now,
exp: now + 3600,
},
privateKey,
{
algorithm: 'RS256',
header: {
kid: privateKey,
typ: 'JWT',
alg: 'RS256',
},
},
);
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'POST',
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: signature,
},
uri: 'https://oauth2.googleapis.com/token',
json: true,
};
return this.helpers.request(options);
}
import { getGoogleAccessToken } from '../GenericFunctions';
export async function googleApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
@@ -108,10 +48,7 @@ export async function googleApiRequest(
privateKey: string;
};
const { access_token } = await getAccessToken.call(
this,
credentials as unknown as IGoogleAuthCredentials,
);
const { access_token } = await getGoogleAccessToken.call(this, credentials, 'books');
options.headers!.Authorization = `Bearer ${access_token}`;