Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Last active December 23, 2021 01:35
Show Gist options
  • Save djeikyb/30164d0dc84585ecb588952681b63cc7 to your computer and use it in GitHub Desktop.
Save djeikyb/30164d0dc84585ecb588952681b63cc7 to your computer and use it in GitHub Desktop.
decodes your jwt aka cli version of jwt.io
#!/bin/sh
SELF=${0##*/}
die() {
log "$@"
exit 1
}
log() {
printf "$SELF: %s\n" "$@" >&2
}
usage() {
cat <<EOF
usage: $self [--help] <jwt>
Examples:
$SELF --help
$SELF "\$someJwt"
$SELF 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
EOF
}
requireCommand() {
command -v $1 >/dev/null 2>&1 || die "'$1' is required, but not found"
}
requireCommand jq
if [ -t 0 ]; then
# invoked interactively!
case "$1" in
""|--help|help|-help|-h)
usage
exit 0
;;
esac
jwt="$1"
else
# not interactive, prolly a jwt being piped through
# TODO i'm going to be annoyed at myself when i forget this only reads a single line
read -r jwt;
fi
echo "$jwt" | jq -R 'split(".") | .[0],.[1] | @base64d | fromjson'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment