Skip to content

Instantly share code, notes, and snippets.

@korydraughn
Forked from thomasdarimont/readme.md
Created January 25, 2024 13:47
Show Gist options
  • Save korydraughn/41279c215d9738d4d8793ce53bb806f5 to your computer and use it in GitHub Desktop.
Save korydraughn/41279c215d9738d4d8793ce53bb806f5 to your computer and use it in GitHub Desktop.
Example for decoding a JWT Payload with your Shell (bash, zsh...)

Setup

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"

Usage

jwtp eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

Output

{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment