Skip to content

Instantly share code, notes, and snippets.

@rgmccaw
Created April 12, 2019 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgmccaw/dc99a3dcd35c353ef13d54a216aa062f to your computer and use it in GitHub Desktop.
Save rgmccaw/dc99a3dcd35c353ef13d54a216aa062f to your computer and use it in GitHub Desktop.
Quickly decode JWTs on the command line. Does not check signature. DO NOT use in production. Created for debbugging purposes.
#!/bin/bash
function usage
{
cat <<EOH
usage:
<command which outputs JWT> | ${0##*/}
example: echo -n "<jwt>" | ${0##*/}
If you have the JWT on your pasteboard you can just call: ${0##*/}
EOH
}
if [ -p /dev/stdin ]; then
read jwt
else
pb=$(pbpaste)
jwt="${1:-$pb}"
fi
if [[ -z $jwt ]]; then
echo -e "\033[31mNo input to decode\033[0m" 1>&2
usage 1>&2
exit 1
fi
OIFS=$IFS
IFS=.
for part in $jwt; do
while :; do
if [[ $(echo "${#part} * 6 % 8" | bc) -eq 6 ]]; then
echo "VERY invalid Base64 input, EXITING" 1>&2
exit 1
fi
if [[ $(echo "${#part} * 6 % 8" | bc) -eq 0 ]]; then
break
fi
part="${part}="
done
decoded=$(echo -n $part | tr "-" "/" | base64 -d 2> /dev/null)
if [[ "${decoded:0:1}" == "{" ]]; then
echo $decoded | jq .
echo
fi
done
IFS=$OIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment