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
@stokito Thanks for the info 👍