SHARE
TWEET

Untitled

a guest Oct 10th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function b64encode(str) {
  2.     return Buffer.from(str).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
  3. }
  4.  
  5. function gentoken(endpoint, subject, key) {
  6.     var message = b64encode(JSON.stringify({
  7.             typ: 'JWT',
  8.             alg: 'ES256'
  9.         })) +
  10.         '.' +
  11.         b64encode(JSON.stringify({
  12.             aud: endpoint,
  13.             exp: Math.floor(Date.now() / 1000) + 43200, // 12 hours
  14.             sub: subject
  15.         }));
  16.        
  17.     return message + '.' + (function () {
  18.         var sign = require('crypto').createSign('RSA-SHA256');
  19.         sign.update(message);
  20.         return require('ecdsa-sig-formatter').derToJose(sign.sign(
  21.             '-----BEGIN EC PRIVATE KEY-----\n' +
  22.             Buffer.from('30310201010420' + key.toString('hex') + 'A00A06082A8648CE3D030107', 'hex').toString('base64') + '\n' +
  23.             '-----END EC PRIVATE KEY-----'
  24.         ), 'ES256');
  25.     })();
  26. }
  27.  
  28. module.exports = gentoken;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top