Errors when generating JWT from API

I have an error when generating a JWT token using Bash script as noted in the API Reference.

The error is “bash-jwt.sh: 8: Syntax error: redirection unexpected”. The URL is listed below in the proper format but could it be something related to the “#” in my URL’s (which I didn’t set, was there by default, don’t know if this is normal) in for example “http://ghost.wrightway.tech/ghost/#/about”.

Help appreciated! Have tried a ton to get this fixed. Thank you!

Note, I’m posting my admin API key because may help and is a throwaway for testing purposes and not my main, so not a real security risk

#!/usr/bin/env bash

# Admin API key goes here

KEY="5f9071f672981463e5c0456a:43106a17b028e405ee4a6bc4668867299da71a89248e959d682f556746d92452"

# Split the key into ID and SECRET

TMPIFS=$IFS

IFS=':' read ID SECRET <<< "$KEY"

IFS=$TMPIFS

# Prepare header and payload

NOW=$(date +'%s')

FIVE_MINS=$(($NOW + 300))

HEADER="{\"alg\": \"HS256\",\"typ\": \"JWT\", \"kid\": \"$ID\"}"

PAYLOAD="{\"iat\":$NOW,\"exp\":$FIVE_MINS,\"aud\": \"/v2/admin/\"}"

# Helper function for perfoming base64 URL encoding

base64_url_encode() {

    declare input=${1:-$(</dev/stdin)}

    # Use `tr` to URL encode the output from base64.

    printf '%s' "${input}" | base64 | tr -d '=' | tr '+' '-' |  tr '/' '_'

}

# Prepare the token body

header_base64=$(base64_url_encode "$HEADER")

payload_base64=$(base64_url_encode "$PAYLOAD")

header_payload="${header_base64}.${payload_base64}"

# Create the signature

signature=$(printf '%s' "${header_payload}" | openssl dgst -binary -sha256 -mac HMAC -macopt hexkey:$SECRET | base64_url_encode)

# Concat payload and signature into a valid JWT token

TOKEN="${header_payload}.${signature}"

# Make an authenticated request to create a post

curl -H "Authorization: Ghost $TOKEN" \

-H "Content-Type: application/json" \

-d '{"posts":[{"title":"Hello world"}]}' \

"http://ghost.wrightway.tech/ghost/api/v2/admin/posts/"

If you’re looking for help, please provide information about your environment. If you delete this template and don’t provide any information, your topic will be closed.

If you aren’t running the latest version of Ghost, the first thing we will ask you to do is update to the latest version of Ghost.

  • What’s your URL? This is the easiest way for others to help you

http://ghost.wrightway.tech/

-*What version of Ghost are you using?

If it’s not the latest, please update Ghost first before opening your topic

  • Version 3.36.0
  • Environment production
  • Database mysql

And

  • How was Ghost installed and configured?

CLI install

  • What Node version, database, OS & browser are you using?

Ubuntu 20.04

  • What errors or information do you see in the console?

bash-jwt.sh: 8: Syntax error: redirection unexpected

  • What steps could someone else take to reproduce the issue you’re having?

sh bash-jwt.sh

It’s not a Ghost error but a bash syntax error. You’re running the script with sh which is not necessarily the same as bash, on Ubuntu sh will use a dash shell instead which doesn’t support the <<< redirection syntax.

If you Google “Syntax error: redirection unexpected” you should find a number of results that explain the problem further along with workarounds. I’d start with trying to run the script with bash bash-jwt.sh if you want to skip ahead before diving in deeper.

1 Like

Kevin, thanks for the response.

Ah yes, just learned on SO the difference between sh and bash.

Now a new problem. That command appears to run but just sits there hanging. Any ideas?:

image

Mod can close this. Used another method and issue resolved.