summary refs log tree commit diff
path: root/decode-jwt
blob: 0d4ddb398ddde92e27eca8e33fb7401d4a53ccef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env zsh
set -euo pipefail
usage="usage: `basename $ZSH_ARGZERO` token"

function pretty_json {
	[ $# -ge 1 ] && str=$1 || str=$(<&0)

	# could read a JSON_PRETTY_PRINTER env variable in case of fancy stuff like `echo $str | underscore print --color`
	# but we leave this as an exercise to the reader

	if command -v jq &>/dev/null; then
		print $str | jq --color-output \
			|| print $str
	elif python -m json.tool -h &>/dev/null; then
		print $str | python -m json.tool \
			|| print $str
	else
		print $str
	fi
}

[ $# -ne 1 ] && print $usage >&2 && exit 1
[ $1 = "-h" ] && print $usage && exit 0

IFS='.' read header payload signature <<<$1

echo $payload | sed "s/_/\//g" | sed "s/-/+/g" | base64 -d | pretty_json