Scripts by rvisharma

Decode JWT

Gist | Install Decode JWT

jwt.io is too slow to load for me. This is instant!

  • Extracts out the token from the entered string and then decodes it.
  • Provides live hint of entered string if contains valid token or not.

https://user-images.githubusercontent.com/5556114/116467402-4071ff80-a88d-11eb-86b4-ad58771d7bd6.mov

// Menu: Decode JWT
// Description: Enter any string which contains valid jwt.
// Author: Ravi Sharma
// Twitter: @irvisharma
/** @type typeof import("jsonwebtoken") */
const jwt = await npm('jsonwebtoken');
try {
const dirtyEncodedToken = await arg('Paste token here', generateLiveHint);
const decoded = decodeToken(dirtyEncodedToken)
if (decoded === null) {
throw 'cannot parse, invalid token'
}
showOutput(decoded);
console.log(decoded);
} catch (e) {
showError(e)
}
function decodeToken(dirtyEncodedToken){
try{
const extractedToken = dirtyEncodedToken.match(/[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*/)[0]
return jwt.decode(extractedToken);
} catch (e) {
return null;
}
}
async function generateLiveHint(userInput){
const tokenValidHint = decodeToken(userInput) !== null ? '✅ Found valid token' : `❌ Could not parse <br><br> ${userInput}`;
return md(`
_Press enter after entering to token show details_
<br/><br/>
${userInput && tokenValidHint}
`)
}
function showOutput(value) {
return show(`
<pre style="font-family: Menlo; background: darkslategray; color: navajowhite; font-size: 1.2rem; padding: 1.5em;">
${JSON.stringify(value, null, 2)}
</pre>
`);
}
function showError(e){
show(`
<h1 style="background: darkslategray; color: white; font-family: Menlo;">Could not decode, Did you entered valid token? <br/><br/> ${e}</h1>
`);
}
Discuss Post

ScriptKit is cool! - Here's how it is helping me with manual repetitive tasks.

I had a use-case where the local development server needs the oauth token from a deployed application to be stored on a local file. Problem is that the expiry of token is short which interrupts my flow of work and then i have to again do the following steps..

  • Open the deployed application
  • Login in with credentials
  • Snip the token from devtools
  • Switch over local file and replace the token.

Although, it just takes one minute, but this task is dreadful to me.

My initial idea was to create a chrome extension which will snip the token from the website and will ping a local server endpoint which in turn will update the local-file with new token using fs

Enter Scriptkit

It does everything i needed with a solid framework to invoke 💪🏻

I'm using, Playwright for browser automation (open -> login -> read token) and then using fs update the local file. I can invoke it via terminal OR via prompt. It just works.

CLI | Prompt ------------ | ------------- in-cli | in-prompt

Thank you for the script kit. I'm already thinking about my next task to automate.

Discuss Post