Skip to content

Instantly share code, notes, and snippets.

@Mihai-P
Created July 22, 2021 19:18
Show Gist options
  • Select an option

  • Save Mihai-P/15942407d2f118fc095af4319b3ae406 to your computer and use it in GitHub Desktop.

Select an option

Save Mihai-P/15942407d2f118fc095af4319b3ae406 to your computer and use it in GitHub Desktop.
JWT
#! /usr/bin/env bash
# creates a JWT in bash
# Static header fields.
HEADER='{
"type": "JWT",
"alg": "RS256"
}'
# PAYLOAD=$1
PAYLOAD='{
"uuid": "TEST",
"role": "system"
}'
# # Use jq to set the dynamic `iat` and `exp`
# # fields on the payload using the current time.
# # `iat` is set to now, and `exp` is now + 1 second.
# PAYLOAD=$(
# echo "${payload}")
function b64enc() { openssl enc -base64 -A | tr '+/' '-_' | tr -d '='; }
function rs_sign() { openssl dgst -binary -sha256 -sign ./private.pem ; }
JWT_HDR_B64="$(echo -n "$HEADER" | b64enc)"
JWT_PAY_B64="$(echo -n "$PAYLOAD" | b64enc)"
UNSIGNED_JWT="$JWT_HDR_B64.$JWT_PAY_B64"
SIGNATURE=$(echo -n "$UNSIGNED_JWT" | rs_sign | b64enc)
echo "$UNSIGNED_JWT.$SIGNATURE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment