Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fetch from 'node-fetch';
  2. import uuid from 'uuid/v4';
  3. import crypto from 'crypto';
  4.  
  5. export default class Modular{
  6.     async createAccount() {
  7.         const body = JSON.stringify({
  8.             type: 'INDIVIDUAL',
  9.             tcsVersion: 0,
  10.             expectedMonthlySpend: 0
  11.         });
  12.         const hmac = this.hmac();
  13.         var res = await fetch(process.env.modularApiUrl+'/customers', {
  14.             method:"POST",
  15.             headers: {
  16.                 'Date': hmac.date,
  17.                 'x-mod-nonce': hmac.nonce,
  18.                 'Authorization': hmac.authorization,
  19.                 'Content-Type': 'application/json',
  20.             },
  21.             body
  22.         });
  23.         if(res.status === 200)
  24.             return await res.json();
  25.         else
  26.             console.error(await res.text())
  27.         return null;
  28.     }
  29.     hmac(){
  30.         const date = (new Date()).toUTCString();
  31.         const nonce = uuid();
  32.         const signatureString = `date: ${date}\nx-mod-nonce: ${nonce}`;
  33.  
  34.         console.log("Signature string: "+signatureString);
  35.         console.log("Secret: "+ process.env.modulrApiHmacSecret)
  36.         const signature = crypto.createHmac('sha1', process.env.modulrApiHmacSecret
  37.         ).update(signatureString).digest('base64');
  38.        
  39.         console.log("Signature: "+ JSON.stringify(signature));
  40.         const base64Signnature = signature.toString('base64');
  41.         console.log("Base64 signature: " + base64Signnature);
  42.         const encodedSignature = encodeURIComponent(base64Signnature);
  43.         console.log("Encoded signature: " + encodedSignature);
  44.         const authorization = `Signature keyId="${process.env.modulrApiKey}",algorithm="hmac-sha1",headers="date x-mod-nonce",signature="${encodedSignature}"`
  45.         console.log("Authorization header: "+authorization);
  46.  
  47.         return {
  48.             date,
  49.             nonce,
  50.             authorization
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement