Skip to content

Instantly share code, notes, and snippets.

@rlipscombe
Created July 19, 2018 10:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlipscombe/ada19c6b2abaabcef21500c3c56db482 to your computer and use it in GitHub Desktop.
Save rlipscombe/ada19c6b2abaabcef21500c3c56db482 to your computer and use it in GitHub Desktop.
Generating HS256 JWT in bash (jq, openssl)
#!/usr/bin/env bash
# This script uses the same secret key and example as jwt.io,
# so that you can verify that it's correct.
secret_key="your-256-bit-secret"
base64url() {
# Don't wrap, make URL-safe, delete trailer.
base64 -w 0 | tr '+/' '-_' | tr -d '='
}
jwt_header=$(echo -n '{"alg":"HS256","typ":"JWT"}' | base64url)
jwt_claims=$(cat <<EOF |
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
EOF
jq -Mcj '.' | base64url)
# jq -Mcj => Monochrome output, compact output, join lines
jwt_signature=$(echo -n "${jwt_header}.${jwt_claims}" | \
openssl dgst -sha256 -hmac "$secret_key" -binary | base64url)
# Use the same colours as jwt.io, more-or-less.
echo "$(tput setaf 1)${jwt_header}$(tput sgr0).$(tput setaf 5)${jwt_claims}$(tput sgr0).$(tput setaf 6)${jwt_signature}$(tput sgr0)"
jwt="${jwt_header}.${jwt_claims}.${jwt_signature}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment