0

I am trying to generate a JWT and sign it with node private key. Private key is in txt format.

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

token_body=echo -n '{"exp": '1706896597',"iat":'1706896701',"iss":"123456-us-xxx","jti":"'123456_xxx_123'","SystemInitiated":true}' | base64 | sed s/\+/-/ | sed -E s/=+$// 

token_signature=echo -n "$token_header.$token_body" | openssl dgst -sha256 -binary -sign PrivateKey.key | openssl enc -base64 | tr -d '\n=' | tr -- '+/' '-_' 

complete_jwt_token=echo -n "$token_header.$token_body.$token_signature"

Using a curl command, to pass this "complete_jwt_token" and invoke an API request.

Result of curl:

{"error":"invalid_request","error_description":"Invalid SCA","error_uri":"http://some-uri?code=401"}

Kindly advise. Please let me know in which format private key should be.

curl command should post the request and receive a response.

6
  • Your posted script won't work at all without either backticks or $( ... ) for process substitution; assuming you just added that mistake just to impede any assistance, your first two conversions to base64 need to translate both + to - AND / to _ and delete (trailing) = AND any nontrailing newline (unless you use base64 -w0 instead), like your third does. It's easiest to use the same method for all of them; I prefer tr. Commented Mar 23, 2024 at 7:10
  • @Charles Duffy Kindly advice. Commented Mar 24, 2024 at 14:54
  • @Darren Smith Kindly advice. Commented Mar 24, 2024 at 14:54
  • Thank You @dave_thompson_085 Still curl cmd results Invalid SCA(-w0 and tr). Any advice will be very helpful. export issdt=date +%s; export expdt=expr $issdt + 1800; hdr=$(echo -n '{"alg":"RS256","typ":"JWT"}' | base64 | tr '+/' '-' | tr -d '='); body=$(echo -n '{"exp": "${expdt}","iat": "${issdt}","iss":"123593-xxxcm-pds","jti":"'123593_XXX_123'","SystemInitiated":true}' | base64 | tr '+/' '-' | tr -d '='); sign=$(echo -n "$hdr.$body" | openssl dgst -sha256 -hmac decrypted_PrivateKey.key.txt_1743byte -binary | base64 | tr '+/' '-_' | tr -d '='); jwt="$jwt.$sign"; Commented Mar 24, 2024 at 14:56
  • echo $sign is too short. 3x0SdtM13gB2BhPQnIjlIwZon38cqIA01i6PdSUzlDc Commented Mar 24, 2024 at 14:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.