Skip to content

Instantly share code, notes, and snippets.

@lmammino
Last active June 19, 2023 14:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmammino/920ee0699af627a3492f86c607c859f6 to your computer and use it in GitHub Desktop.
Save lmammino/920ee0699af627a3492f86c607c859f6 to your computer and use it in GitHub Desktop.
Decode Body of JWT tokens with bash

jwtinfo.sh

A simple debugger for JWT tokens written in bash. It easily allows you to extract the body of a given token.

Usage

jwtinfo.sh "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

Install

curl -o jwtinfo.sh "https://gist.githubusercontent.com/lmammino/920ee0699af627a3492f86c607c859f6/raw/jwtinfo.sh"
chmod +x jwtinfo.sh
mv jwtinfo.sh /usr/local/bin
#!/usr/bin/env bash
# Example usage:
# jwtinfo.sh "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
function decode {
read token
_l=$((${#token} % 4))
if [ $_l -eq 2 ]; then _s="$token"'=='
elif [ $_l -eq 3 ]; then _s="$token"'='
else _s="$token" ; fi
echo "$_s" | tr '_-' '/+' | openssl enc -d -a -A
}
jwtinfo ()
{
echo "$1" | cut -d'.' -f 2 | decode 2> /dev/null
}
jwtinfo "$1"
# Inspired by code from @Moodstocks, @deltheil and @xatier
# References:
# - https://github.com/Moodstocks/moodstocks-api-clients/blob/master/bash/base64url.sh
# - https://github.com/lmammino/jwtinfo/issues/5
The `decode` function has been re-adapted from code provided by Moodstocks SAS and
therefore the following license applies to it:
Copyright (C) 2014 by Moodstocks SAS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---
The remaining code is available under MIT license in respect to the
following license:
MIT License
Copyright (c) 2019 Luciano Mammino & Stefano Abalsamo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@xatier
Copy link

xatier commented Mar 14, 2020

You would need to include the LICENSE section to the decode function if you are taking the code from @Moodstocks.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

@lmammino
Copy link
Author

Updated, thanks a lot for the due diligence @xatier! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment