Universal Binary JSON
I'm on a vue.js v2 project. I need to parse a token after logged in. It is not a JWT token. It's a
CodePudding user response:
These solutions also work for me with fewer lines of code.
var ubjson = require("@shelacek/ubjson")
const token = '123***XYZ'
const buffer = Buffer.from(token, 'base64')
const obj = ubjson.decode(buffer)
console.log(obj)
CodePudding user response:
There is a UBJSON encoder/decoder on NPM
See code-8's answer
Try something like this:
See in action [https://runkit.com/embed/3ngoupnl0unj]=
const ubjson = require('@shelacek/ubjson');
// https://stackoverflow.com/a/62364519/3330981
function base64ToBytesArr(str) {
const abc = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /"]; // base64 alphabet
let result = [];
for(let i=0; i<str.length/4; i ) {
let chunk = [...str.slice(4*i,4*i 4)]
let bin = chunk.map(x=> abc.indexOf(x).toString(2).padStart(6,0)).join('');
let bytes = bin.match(/.{1,8}/g).map(x=> ('0b' x));
result.push(...bytes.slice(0,3 - (str[4*i 2]=="=") - (str[4*i 3]=="=")));
}
return result;
}
const token = "e2kEdmVyc0kAAWkEdXNlcntpBG5hbWVTaRNiaGVuZ0B2aTNnbG9iYWwuY29taQRhcHBsU2kCVkNpBHNlcnZbJFUjaRC8L g8i58TZQsAWbfrIuGgaQRwZXJtW3tpBG5hbWVTaQNFSURpBHBhcm1be2kEbmFtZVNpA0FQUGkEZGF0YVNpASp9XWkEZGF0YVNpSkJST08sQ05WRixDTlZaLERGUUwsREoxTCxGVjhaLEhZMEIsTjk0WCxSRDhMLFczWFYsWDNDWSxYUEg0LFlYNE4sWlIxMCxDT0xNfXtpBG5hbWVTaQNFSURpBHBhcm1be2kEbmFtZVNpA0FQUGkEZGF0YVNpAlZDfV1pBGRhdGFTaQlDT0xNLERGUUx9kEbmFtZVNpClZDX0dFVF9FSURpBHBhcm1be2kEbmFtZVNpBUJSQU5EaQRkYXRhU2kJQlJPTyxDT0xNfV1pBGRhdGFUfV19aQRyZWxtWyRVI2kQqBr68fUnmySYz31zLMgdOWkEYWxnb1NpClNIQTI1Ni1SU0FpBHNpZ25bJFUjSQCAokw8V8p06MkGqbm4Dry0ymUNQ2AM9TFd77vjFfJS3qHaGf/DYyYmkZamXUhOqFaucdVtkEgyQYMrDM2vOMILH7YnUVmgty8nH5Oim5w4aRQRepLcd14E98BnRjHovRS3KqeHoiON78gsTTcwhX1CE1hXlZxZGr6EJleEr8HbmMt9"
const obj = ubjson.decode(base64ToBytesArr(token));
console.log(obj);