UNPKG

n8n-ikaue-nodes

Version:

IKAUE nodes for N8N, such as a custom BigQuery node or a Google Search Console node.

106 lines 4.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccessToken = exports.googleApiRequest = void 0; const n8n_workflow_1 = require("n8n-workflow"); const moment = __importStar(require("moment-timezone")); const jwt = __importStar(require("jsonwebtoken")); async function googleApiRequest(method, resource, body = {}, qs = {}, uri, headers = {}) { const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount'); const options = { headers: { 'Content-Type': 'application/json', }, method, body, qs, uri: uri || `https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fn8n.io%2Fblog%2F/searchAnalytics/query?key=AIzaSyBqsxEM7qZ59IXj3uf9GhxC0nvQ8F4LZ_E`, json: true, }; try { if (Object.keys(headers).length !== 0) { options.headers = Object.assign({}, options.headers, headers); } if (Object.keys(body).length === 0) { delete options.body; } if (authenticationMethod === 'serviceAccount') { const credentials = await this.getCredentials('googleApi'); if (credentials === undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No credentials got returned!'); } const { access_token } = await getAccessToken.call(this, credentials); options.headers.Authorization = `Bearer ${access_token}`; return await this.helpers.request(options); } else { return await this.helpers.requestOAuth2.call(this, 'googleSearchConsoleOAuth2Api', options); } } catch (error) { if (error.code === 'ERR_OSSL_PEM_NO_START_LINE') { error.statusCode = '401'; } throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } exports.googleApiRequest = googleApiRequest; function getAccessToken(credentials) { const scopes = [ 'https://www.googleapis.com/auth/webmasters', 'https://www.googleapis.com/auth/webmasters.readonly', ]; const now = moment().unix(); 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, }, credentials.privateKey, { algorithm: 'RS256', header: { 'kid': credentials.privateKey, 'typ': 'JWT', 'alg': 'RS256', }, }); const options = { 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); } exports.getAccessToken = getAccessToken; //# sourceMappingURL=GenericFunctions.js.map