How to perform URL64decode and split in custom actions

Hi
I do not manage to perform a URL64 decode in my custom action
I create a new custom action from scratch and I’m trying to
1 - extract the payload of a token ( between the 2 dots )
This value is the result of an API call , which works. The value is stored into {{temp.response.body.data.token}}. Variable is populated, no issues at this point.
2 - perform a URLdecode64 on the paylaod of the token.

I have the following code in my action, what am I missing ? :

/**

  • @title convert URL64 URL
  • @category Turorial.
    */
    var Base64={_keyStr:“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”,encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN®){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode®}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode®}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode®;n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

var decodedString = Base64.decode(’{{temp.response.body.data.token}}’);

// We assign the response to the session variable so we can use it later
temp.decoded = decodedString
}

// Actions are async, so make sure to return a promise
return URL64C()
~

Thanks a lot !

Hey @smalldragoon !

You can see examples of decoding a base64 string here https://stackabuse.com/encoding-and-decoding-base64-strings-in-node-js/

Let me know if you are still having trouble after reading this article.

Have a nice day!

Hi Spgin,
Thanks a lot for your answer. I’m not a great coder, so I guess I’m missing some basic things .
From what I understand from the link, here is the code I created in my custom action :

/**

  • @title convert URL64 URL
  • @category Turorial
    */

‘use strict’;

let data = ‘{{temp.response.body.data.token}}’;
let buff = new Buffer(data, ‘base64’);
let text = buff.toString(‘ascii’);

let temp.decoded = text;
return URL64C()

What am I doing wrong ? anyway to debug it wihtout going through the chatbot each time ?
Thanks

Just replace the following line:

let data = ‘{{temp.response.body.data.token}}’;

with

let data = temp.response.body.data.token;

and it should work.

Let me know if it does not!

No, still not.
how can I troubleshoot ? I don’t know where the code is failing

for ref :

ex of token value : eyJraWQiOiJUUyIsImFsZyI6IkhTMjU2In0.eyJjdXJyZW50X2FkZHJlc3MiOiIzIHJ1ZSBwaXBvIiwic3ViIjoiam9obiIsImF1ZCI6InZlcml6b24iLCJvcCI6ImF1dGgiLCJkc2lkIjoiYmFiZThlMmMtOWVmNS00MjgzLWE5M2MtNWJkNWMwMzRkN2ZlIiwiaXNzIjoiVFMiLCJwYXJhbXMiOnsidGlja2V0X2lkIjoiY2E1ZGQyMDgtOWNiNy00YjVmLWExOTMtZDRiYzBkYjk1OTIwIiwidXJsIjoiXC9ob21lIn0sImV4cCI6MTU3MzIzNDg0NywiaWF0IjoxNTczMjMzMDQ3LCJqdGkiOiJlODhiZDFmZC03NTEyLTQzZmQtOGZiZC0yMjc1OGU5YmEwNzQiLCJzaWQiOiJjODk0Mjk4NC01ZTNlLTQ2ZWItYjIyNi1iY2VmMTZmZWE5NGMifQ.SCwOIsMwUpdYC1-XmKJElybpcu1aCaNA1Q9sQmncg

/**

  • @title convert URL64 URL
  • @category Turorial
    */

‘use strict’;

let data = temp.response.body.data.token;
let buff = new Buffer(data, ‘base64’);
let text = buff.toString(‘ascii’);

let temp.decoded = text;

return URL64C()

Thanks

There may be 2 issues at play here:

  1. your token might not be properly encoded in base64
    and/or
  2. the encoding might not be ascii

So, make sure the token is properly encoded in base64, and that you choose the right encoding (the string parameter in buff.toString(‘ascii’))

You can test the validity of your payload and the encoding on this website: https://www.base64decode.org/

Let me know if this helps!

Got it working !
I used this online javascript sandbox. https://repl.it/repls/AustereThreadbareSpyware

the " return URL64C()" was the issue. I removed it and it works perfectly
Thanks a lot for your help !