Skip to content

Instantly share code, notes, and snippets.

@mathieuancelin
Last active December 16, 2021 08:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathieuancelin/3a4430fe0809c0726d41424f677581e4 to your computer and use it in GitHub Desktop.
Save mathieuancelin/3a4430fe0809c0726d41424f677581e4 to your computer and use it in GitHub Desktop.

Terminal JWT decoder

just add the function to your .bashrc or .zshrc and use it like :

$ jwt_decode eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

{
  "alg": "HS256",
  "typ": "JWT"
}
{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
}
function jwt_decode() {
jq -R 'split(".") | .[0] | @base64d | fromjson' <<< "$1"
jq -R 'split(".") | .[1] | @base64d | fromjson | if .iat then (.iat_str = (.iat|gmtime|strftime("%Y-%m-%dT%H:%M:%S %Z"))) else . end | if .exp then (.exp_str = (.exp|gmtime|strftime("%Y-%m-%dT%H:%M:%S %Z"))) else . end | if .nbf then (.nbf_str = (.nbf|gmtime|strftime("%Y-%m-%dT%H:%M:%S %Z"))) else . end' <<< "$1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment