0

Short version:

This unix command (from this tutorial https://techdocs.akamai.com/iot-token-access-control/docs/generate-jwt-rsa-keys):

$ echo -n '{ "alg": "RS256",   "typ": "JWT" }' | base64 | sed s/\+/-/ | sed -E s/=+$//

Prints this:

eyAiYWxnIjogIlJTMjU2IiwgICAidHlwIjogIkpXVCIgfQ

I am trying to move this to a bash script:

#!/bin/bash
HEADER='{ "alg": "RS256",   "typ": "JWT" }' | base64 | sed s/\+/-/ | sed -E s/=+$//
echo -n $HEADER

But the output is an error:

 sed: -e expression #1, char 8: unknown option to `s'

Is there something that needs to be done to make SSH scripts that work in the shell work in bash?

(I'm completely new to bash scripts)

Long version:

I need to build a bash script that creates a JWT and then calls a Rest API from a Linux server. I have never built a bash script before, but the basic format was going to be:

#!/bin/bash
HEADER= ... encoded header
SIXTYSECONDS=date -d '1 min' +%s #<-- this also works in echo, but not in bash -- supposed to be current time+60 seconds as epoch time
PAYLOAD= ... encoded payload with $SIXTYSECONDS as expiration time  
SIGNATURE=... encoded signature from keycerts
JWT= $HEADER.$PAYLOAD.$SIGNATURE
... Will tackle the code to call the API once I have the JWT code down

I am following this tutorial (https://techdocs.akamai.com/iot-token-access-control/docs/generate-jwt-rsa-keys) but it doesn't help me programmatically concatenate the various JWT pieces. Also I'm not sure how to integrate the 60 second expiration.

ryvantage
  • 13,064
  • 15
  • 63
  • 112
  • You're missing the command substitution syntax shown in the linked duplicate. – Charles Duffy Mar 16 '22 at 02:53
  • Also, note that you can't have a space after the `=` in an assignment. – Charles Duffy Mar 16 '22 at 02:54
  • You may also find [How do you create an RS256 JWT assertion with bash/shell scripting?](https://stackoverflow.com/questions/46657001/how-do-you-create-an-rs256-jwt-assertion-with-bash-shell-scripting) to be of interest. – Charles Duffy Mar 16 '22 at 02:55
  • Also, http://shellcheck.net is your friend. As a rule, read the wiki page linked for each warning it throws before asking questions here. – Charles Duffy Mar 16 '22 at 02:57
  • @CharlesDuffy Ok thanks. I notice in the comments for (https://stackoverflow.com/questions/46657001/how-do-you-create-an-rs256-jwt-assertion-with-bash-shell-scripting) you try to persuade the author to use Python but he refuses. I am able and would like to explore that solution. Do you have any tutorials that would accomplish the JWT creation process using Python? (new to that as well) – ryvantage Mar 16 '22 at 03:30
  • https://github.com/davedoesdev/python-jwt has examples included in its README. It also has a test suite you can refer to. There's also a second, unrelated Python library for the same purpose at https://github.com/jpadilla/pyjwt, which _also_ has examples in its README, and a larger set at https://pyjwt.readthedocs.io/en/stable/usage.html – Charles Duffy Mar 16 '22 at 04:08

0 Answers0