Skip to content

Instantly share code, notes, and snippets.

@briceburg
Last active December 20, 2025 15:04
Show Gist options
  • Select an option

  • Save briceburg/0d6589714862004609daf77f4fc4aac9 to your computer and use it in GitHub Desktop.

Select an option

Save briceburg/0d6589714862004609daf77f4fc4aac9 to your computer and use it in GitHub Desktop.

Revisions

  1. briceburg revised this gist Mar 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mk-jwt-token
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ main(){

    # pass JWT_SECRET_BASE64_ENCODED as true if secret is base64 encoded
    ${JWT_SECRET_BASE64_ENCODED:-false} && \
    JWT_SECRET=$(printf %s "$JWT_SECRET" | base64 -D)
    JWT_SECRET=$(printf %s "$JWT_SECRET" | base64 --decode)

    header='{
    "alg": "HS256",
  2. briceburg renamed this gist Mar 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mk-jwt.sh → mk-jwt-token
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env bash
    #
    # usage: JWT_SECRET="silly" mk-jwt.sh
    # usage: JWT_SECRET="silly" mk-jwt-token
    # @WARN: modify the payload and header to your needs.
    #
    main(){
  3. briceburg created this gist Mar 5, 2020.
    43 changes: 43 additions & 0 deletions mk-jwt.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/usr/bin/env bash
    #
    # usage: JWT_SECRET="silly" mk-jwt.sh
    # @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 -D)

    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 "$@"