summary refs log tree commit diff
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2023-11-01 19:02:29 -0500
committerStarfall <us@starfall.systems>2023-11-01 19:02:29 -0500
commitfd9cabe50168ce1b5a4a09a6d0477933f392eb12 (patch)
treead6d7049e26edde45e2a6cc22cbee11cf9968fe8
parentc8022a3a249d80792a03c748064aac805e56f729 (diff)
decode-jwt
-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
+