Skip to content

Instantly share code, notes, and snippets.

@operatorequals
Last active May 7, 2024 20:22
Show Gist options
  • Save operatorequals/d9756a98a27207fd5694e7dd80b7007a to your computer and use it in GitHub Desktop.
Save operatorequals/d9756a98a27207fd5694e7dd80b7007a to your computer and use it in GitHub Desktop.
Github App JWT generator
#!/bin/bash
# Followed the post for JWT construction here:
# https://dev.to/kimmaida/signing-and-validating-json-web-tokens-jwt-for-everyone-25fb
APP_ID="$1"
PRIVATE_KEY="$2"
DATE=$(date +%s)
# All JWT parts must be in 'base64url' -> url compatible base64
# https://www.base64url.com/
# '=' -> ''
# '+' -> '-'
# '/' -> '_'
JWT_HEADER=$(echo -n "{\"alg\": \"RS256\"}" | base64 -w0 | tr -d '=')
JWT_CLAIMS=$(echo -n "{\"iat\": $(($DATE-10)),\"exp\":$(($DATE+600)), \"iss\":\"$APP_ID\"}" | base64 -w0 | tr -d '=')
JWT_PAYLOAD="$(echo -n $JWT_HEADER.$JWT_CLAIMS )"
JWT_SIGNATURE="$(echo -n $JWT_PAYLOAD | openssl sha256 -binary -sign $PRIVATE_KEY | base64 -w0)"
JWT="$(echo $JWT_HEADER.$JWT_CLAIMS.$JWT_SIGNATURE | tr -d '=' | tr '+/' '-_')"
echo "$JWT"
@Avalin
Copy link

Avalin commented May 7, 2024

I unfortunarely get this upon using the script:

base64: invalid option -- w
Usage: base64 [-Ddh] [-b num] [-i in_file] [-o out_file]
-b, --break break encoded string into num character lines
-Dd, --decode decodes input
-h, --help display this message
-i, --input input file (default: "-" for stdin)
-o, --output output file (default: "-" for stdout)
base64: invalid option -- w
Usage: base64 [-Ddh] [-b num] [-i in_file] [-o out_file]
-b, --break break encoded string into num character lines
-Dd, --decode decodes input
-h, --help display this message
-i, --input input file (default: "-" for stdin)
-o, --output output file (default: "-" for stdout)
base64: invalid option -- w
Usage: base64 [-Ddh] [-b num] [-i in_file] [-o out_file]
-b, --break break encoded string into num character lines
-Dd, --decode decodes input
-h, --help display this message
-i, --input input file (default: "-" for stdin)
-o, --output output file (default: "-" for stdout)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment