n8n-nodes-gmail-custom 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,8 @@
1
1
  const crypto = require('crypto');
2
2
 
3
+ let jwtLib;
4
+ try { jwtLib = require('jsonwebtoken'); } catch (e) {}
5
+
3
6
  const TOKEN_CACHE = {};
4
7
 
5
8
  function formatPrivateKey(privateKey) {
@@ -357,30 +360,54 @@ class GmailCustom {
357
360
  if (!accessToken || Date.now() >= expiresAt) {
358
361
  const privateKey = formatPrivateKey(credentials.privateKey);
359
362
  const now = Math.floor(Date.now() / 1000);
360
- const header = { alg: 'RS256', typ: 'JWT', kid: privateKey };
361
- const payload = {
362
- iss: credentials.email,
363
- scope: 'https://mail.google.com/',
364
- aud: 'https://oauth2.googleapis.com/token',
365
- exp: now + 3600,
366
- iat: now,
367
- sub: fromEmail,
368
- };
369
363
 
370
- const signatureInput = base64UrlEncode(JSON.stringify(header)) + '.' + base64UrlEncode(JSON.stringify(payload));
371
- const signer = crypto.createSign('RSA-SHA256');
372
- signer.update(signatureInput);
373
- const sig = signer.sign(privateKey);
374
- const jwt = signatureInput + '.' + base64urlEscape(sig.toString('base64'));
364
+ let jwt;
365
+ if (jwtLib) {
366
+ jwt = jwtLib.sign(
367
+ {
368
+ iss: credentials.email,
369
+ scope: 'https://mail.google.com/',
370
+ aud: 'https://oauth2.googleapis.com/token',
371
+ exp: now + 3600,
372
+ iat: now,
373
+ sub: fromEmail,
374
+ },
375
+ privateKey,
376
+ {
377
+ algorithm: 'RS256',
378
+ header: {
379
+ kid: privateKey,
380
+ typ: 'JWT',
381
+ alg: 'RS256',
382
+ },
383
+ },
384
+ );
385
+ } else {
386
+ const header = { alg: 'RS256', typ: 'JWT', kid: privateKey };
387
+ const payload = {
388
+ iss: credentials.email,
389
+ scope: 'https://mail.google.com/',
390
+ aud: 'https://oauth2.googleapis.com/token',
391
+ exp: now + 3600,
392
+ iat: now,
393
+ sub: fromEmail,
394
+ };
395
+ const signatureInput = base64UrlEncode(JSON.stringify(header)) + '.' + base64UrlEncode(JSON.stringify(payload));
396
+ const signer = crypto.createSign('RSA-SHA256');
397
+ signer.update(signatureInput);
398
+ const sig = signer.sign(privateKey);
399
+ jwt = signatureInput + '.' + base64urlEscape(sig.toString('base64'));
400
+ }
375
401
 
376
- const response = await this.helpers.httpRequest({
402
+ const response = await this.helpers.request({
377
403
  method: 'POST',
378
- url: 'https://oauth2.googleapis.com/token',
404
+ uri: 'https://oauth2.googleapis.com/token',
379
405
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
380
- body: new URLSearchParams({
406
+ form: {
381
407
  grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
382
408
  assertion: jwt,
383
- }).toString(),
409
+ },
410
+ json: true,
384
411
  });
385
412
 
386
413
  accessToken = response.access_token;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-gmail-custom",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Custom Gmail node for n8n with token caching and 429 retry",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",