Add this to your .profile, .bashrc, .zshrc...
decode_base64_url() {
local len=$((${#1} % 4))
local result="$1"
if [ $len -eq 2 ]; then result="$1"'=='
elif [ $len -eq 3 ]; then result="$1"'='
fi
echo "$result" | tr '_-' '/+' | openssl enc -d -base64
}
decode_jwt(){
decode_base64_url $(echo -n $2 | cut -d "." -f $1) | jq .
}
# Decode JWT header
alias jwth="decode_jwt 1"
# Decode JWT Payload
alias jwtp="decode_jwt 2"
jwtp eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
Thanks for the helpful gist
I've adjusted it a bit
base64
instead ofopenssl
(so I don't have to install it on analpine
docker image)decode_jwt
method, expecting the jwt first, and defaulting the second argument to2
(as we usually need to decode the body)Link to the fork: https://gist.github.com/a-magdy/a771f1426043ab4b66b19cc9a652908b