Last active
September 7, 2020 09:08
-
-
Save KevCui/767ebcdf8afb1df2a2abb4e95d9a70e3 to your computer and use it in GitHub Desktop.
A Bash script to decode JWT token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# HOW TO USE: | |
# ~$ chmod +x jwtDecoder.sh | |
# ~$ ./jwtDecoder.sh "<JWT token>" | |
padding() { | |
# $1: base64 string | |
local m p="" | |
m=$(( ${#1} % 4 )) | |
[[ "$m" == 2 ]] && p="==" | |
[[ "$m" == 3 ]] && p="=" | |
echo "${1}${p}" | |
} | |
if [[ -z $(command -v jq) ]]; then | |
echo "This script will NOT work on your machine." | |
echo "Please install jq first: https://stedolan.github.io/jq/download/" | |
exit 1 | |
fi | |
clear | |
input=("${@}") | |
input=("${input//$'\n'/}") | |
input=("${input//' '/}") | |
token=$(IFS=$'\n'; echo "${input[*]}") | |
echo -e "JWT token:\\n${token}" | |
IFS='.' read -ra ADDR <<< "$token" | |
base64 -d <<< "$(padding "${ADDR[0]}")" | jq | |
base64 -d <<< "$(padding "${ADDR[1]}")" | jq | |
echo "Signature: ${ADDR[2]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also related and may be useful:
https://github.com/emcrisostomo/jwt-cli
https://willhaley.com/blog/generate-jwt-with-bash/
https://www.jvt.me/posts/2019/06/13/pretty-printing-jwt-openssl/
Here is an example with verifying a signature
https://gist.github.com/rolandyoung/176dd310a6948e094be6
An example of base64 URL encode/decode
https://github.com/Moodstocks/moodstocks-api-clients/blob/master/bash/base64url.sh