# will not work in all cases, see https://gist.github.com/angelo-v/e0208a18d455e2e6ea3c40ad637aac53#gistcomment-3439904 | |
function jwt-decode() { | |
sed 's/\./\n/g' <<< $(cut -d. -f1,2 <<< $1) | base64 --decode | jq | |
} | |
JWT=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ | |
jwt-decode $JWT |
This comment has been minimized.
This comment has been minimized.
With just jq: |
This comment has been minimized.
This comment has been minimized.
@lukaslihotzki, nice work :)
|
This comment has been minimized.
This comment has been minimized.
JWTs use base64url encoding, which neither |
This comment has been minimized.
This comment has been minimized.
Valid point @philpennock. Do you have a reliable solution? |
This comment has been minimized.
This comment has been minimized.
Just replace the characters? |
This comment has been minimized.
This comment has been minimized.
@philpennock is correct. Here is my solution in a shell script: #!/bin/bash
# pad base64URL encoded to base64
paddit() {
input=$1
l=`echo -n $input | wc -c`
while [ `expr $l % 4` -ne 0 ]
do
input="${input}="
l=`echo -n $input | wc -c`
done
echo $input
}
# read and split the token and do some base64URL translation
read jwt
read h p s <<< $(echo $jwt | tr [-_] [+/] | sed 's/\./ /g')
h=`paddit $h`
p=`paddit $p`
# assuming we have jq installed
echo $h | base64 -d | jq
echo $p | base64 -d | jq |
This comment has been minimized.
This comment has been minimized.
Oh, I never spoke up, sorry: I use shell around Perl because Perl had the easiest access to base64url when I last went looking, but I was impressed by the solution from @lukaslihotzki using |
This comment has been minimized.
This comment has been minimized.
I was also trying to decode a JWT token. I could decode a JWT with: for line in `echo $JWT | tr "." "\n"`; do echo $line | base64 --decode | jq && echo;done |
This comment has been minimized.
This comment has been minimized.
@seva-ramin's script is fantastic, but just a small bug: |
This comment has been minimized.
This comment has been minimized.
Ugh! what a silly mistake! @danmactough, Thank you for catching that. I have updated my post. |
This comment has been minimized.
why not just
cut -d"." -f1,2 <<< $1 | sed 's/\./\n/g' | base64 --decode | jq
?