UNPKG

@skriptfabrik/n8n-nodes-google-enhanced

Version:
78 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMultipartForm = createMultipartForm; exports.parseBodyData = parseBodyData; exports.requestAccessToken = requestAccessToken; exports.requestServiceAccount = requestServiceAccount; const tslib_1 = require("tslib"); const form_data_1 = tslib_1.__importDefault(require("form-data")); const jwt = tslib_1.__importStar(require("jsonwebtoken")); const moment_timezone_1 = tslib_1.__importDefault(require("moment-timezone")); const utilities_1 = require("n8n-nodes-base/dist/utils/utilities"); function createMultipartForm(metadata, content, contentType, knownLength) { const body = new form_data_1.default(); body.append('metadata', JSON.stringify(metadata), { contentType: 'application/json', }); body.append('file', content, { contentType, knownLength, }); return body; } function parseBodyData(bodyData, fields) { for (const field of fields) { if (!bodyData[field]) { continue; } try { bodyData[field] = JSON.parse(bodyData[field]); } catch (_a) { continue; } } return bodyData; } function requestAccessToken(credentialsType, scopes) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const now = (0, moment_timezone_1.default)().unix(); const credentials = yield this.getCredentials(credentialsType); const privateKey = (0, utilities_1.formatPrivateKey)(credentials['privateKey']); credentials['email'] = (credentials['email'] || '').trim(); const options = { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, method: 'POST', form: { grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion: 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', }, }), }, uri: 'https://oauth2.googleapis.com/token', json: true, }; return this.helpers.request(options); }); } function requestServiceAccount(credentialsType, options, scopes) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { access_token } = yield requestAccessToken.call(this, credentialsType, scopes); return this.helpers.request(Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, options.headers), { Authorization: `Bearer ${access_token}` }) })); }); } //# sourceMappingURL=GenericFunctions.js.map