Generate RS256 JWT

Good afternoon community, how are you doing?

Here I come with a new query, I wanted to know if it could be generated through n8n token with JWT Grant RS256 I read this Docu: link DocuSign but I am not finding the return to be able to generate it. If you have any info or something that can help, thank you very much!

Workflow:

And when I put it in this URL https://jwt.io/ the JWT tells me that it is incorrect in Base64

This would be what you should put together in the code node

Thank you very much as always for your willingness to help!

Information on your n8n setup

  • **n8n version:1.27.2
  • **Database (default: SQLite):postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):Kubernetes
  • Operating system:

hello @Internalit_Automatio

Well, It didn’t work for me with RSA256, but it works with HMAC256

Example below:

2 Likes

Hi @barn4k

I was able to solve it with RS256, I’ll pass you the code!.

const crypto = require('crypto');

// Definimos Header y Body
const header = {
  alg: 'RS256',
  typ: 'JWT'
};

const body = {
  iss: 'XXXX-XXX',
  sub: 'XXXX-XXXX',
  aud: 'account.docusign.com',
  iat: Math.floor(Date.now() / 1000), // Tiempo actual en formato UNIX epoch
  exp: Math.floor(Date.now() / 1000) + 6000, // Tiempo actual + 6000 segundos
  scope: 'signature impersonation'
}

// Codificamos el Header y Body
const base64UrlEncode = (data) => {
  return Buffer.from(JSON.stringify(data)).toString('base64')
    .replace(/=/g, '')
    .replace(/\+/g, '-')
    .replace(/\//g, '_');
};

const encodedHeader = base64UrlEncode(header);
const encodedBody = base64UrlEncode(body);

// Combinamos el Header + Body
const payload = `${encodedHeader}.${encodedBody}`;

// Colocamos la llave Pública y Privada
const privateKey = `-----BEGIN RSA PRIVATE KEY-----

-----END RSA PRIVATE KEY-----`;

const publicKey = `-----BEGIN PUBLIC KEY-----

-----END PUBLIC KEY-----`;

// Firmamos el payload con la clave privada
const signer = crypto.createSign('RSA-SHA256');
signer.update(payload);
const signature = signer.sign(privateKey, 'base64');

// Codificar la firma utilizando base64url
const encodedSignature = signature.toString('base64')
    .replace(/=/g, '')
    .replace(/\+/g, '-')
    .replace(/\//g, '_');

//Resultado Final
const jwtToken = `${payload}.${encodedSignature}`;

return [
  {
    key: 'JWT generado',
    value: jwtToken
  }
];

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.