Last active
June 13, 2022 15:32
-
-
Save jeremiehuchet/4c260459a827f835ca387464e13e1aea to your computer and use it in GitHub Desktop.
generate JWT token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| # see https://techdocs.akamai.com/iot-token-access-control/reference/generate-jwt-rsa-keys | |
| PRIVATE_KEY_PEM="$1" | |
| b64encode() { | |
| echo -n "$*" | base64 -w 0 | sed s/\+/-/ | sed -E s/=+$// | |
| } | |
| sign() { | |
| echo -n "$*" | openssl dgst -sha256 -binary -sign "$PRIVATE_KEY_PEM" | openssl enc -base64 | tr -d '\n=' | tr -- '+/' '-_' | |
| } | |
| header='{ | |
| "alg": "RS256", | |
| "typ": "JWT" | |
| }' | |
| iat=$(date +%s) | |
| exp=$(expr $iat + 600) | |
| payload=$(cat - <<EOF | |
| { | |
| "jti": "$(uuidgen)", | |
| "iat": $iat, | |
| "exp": $exp | |
| } | |
| EOF | |
| ) | |
| content=$(echo -n "$(b64encode $header).$(b64encode $payload)") | |
| echo $header | |
| echo $payload | |
| echo "$content.$(sign $content)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment