Skip to content

Instantly share code, notes, and snippets.

@briceburg
Last active December 9, 2023 03:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save briceburg/0d6589714862004609daf77f4fc4aac9 to your computer and use it in GitHub Desktop.
Save briceburg/0d6589714862004609daf77f4fc4aac9 to your computer and use it in GitHub Desktop.
mk-jwt-token -- bash shell script for generating JWT tokens. requires `openssl` and `base64` which you have.
#!/usr/bin/env bash
#
# usage: JWT_SECRET="silly" mk-jwt-token
# @WARN: modify the payload and header to your needs.
#
main(){
set -eo pipefail
[ -n "$JWT_SECRET" ] || die "JWT_SECRET environment variable is not set."
# number of seconds to expire token. default 1h
expire_seconds="${JWT_EXPIRATION_IN_SECONDS:-3600}"
# pass JWT_SECRET_BASE64_ENCODED as true if secret is base64 encoded
${JWT_SECRET_BASE64_ENCODED:-false} && \
JWT_SECRET=$(printf %s "$JWT_SECRET" | base64 --decode)
header='{
"alg": "HS256",
"typ": "JWT"
}'
payload="{
\"iss\": \"testing.iceburg.net\",
\"iat\": $(date +%s),
\"exp\": $(($(date +%s)+expire_seconds)),
\"nbf\": $(($(date +%s)-1))
}"
header_base64=$(printf %s "$header" | base64_urlencode)
payload_base64=$(printf %s "$payload" | base64_urlencode)
signed_content="${header_base64}.${payload_base64}"
signature=$(printf %s "$signed_content" | openssl dgst -binary -sha256 -hmac "$JWT_SECRET" | base64_urlencode)
log "generated JWT token. expires in $expire_seconds seconds -->\\n\\n"
printf '%s' "${signed_content}.${signature}"
}
base64_urlencode() { openssl enc -base64 -A | tr '+/' '-_' | tr -d '='; }
readonly __entry=$(basename "$0")
log(){ echo -e "$__entry: $*" >&2; }
die(){ log "$*"; exit 1; }
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment