How to decode a jwt token

There is a native way to decode jwt token using the k6 libs? as for now i did not find a way to do so.
i am looking for equivalent functionality of:

import {decode} = jsonwebtoken 

i try the following imlimintation but since Buffer is not supported it wont work.

 const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'

    const base64Url = token.split('.')[1];
    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    const buff = new Buffer(base64, 'base64');
    const payloadinit = buff.toString('ascii');
    const payload = JSON.parse(payloadinit);
    console.log(payload);

never mind i found the following code as a good solution

const parts = token.split('.');
return JSON.parse(encoding.b64decode(parts[1].toString(), "rawstd", 's'))
2 Likes