summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xdecode-jwt28
1 files changed, 28 insertions, 0 deletions
diff --git a/decode-jwt b/decode-jwt
new file mode 100755
index 0000000..0d4ddb3
--- /dev/null
+++ b/decode-jwt
@@ -0,0 +1,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
+