Skip to content
Advertisement

Unable to substitute a custom variable in Authorization header value in curl

I am trying to use $timestamp and generate hash. I see $timestamp value is not getting assigned and it’s always same.

My request:–

curl  -H "Authorization:EG1-HMAC-SHA256;timestamp=$(date +%Y%m%dT%H:%M:%S+0000);nonce=`cat /proc/sys/kernel/random/uuid`;signature=$(echo -n $timestamp | openssl dgst -binary -sha256 -hmac '+abhishek' | openssl enc -base64)"  https://test.com/abcd/v1/test/123/impulse -vv

Output[Request Authorization headers]

Authorization:EG1-HMAC-SHA256;timestamp=20180701T12:33:56+0000;nonce=8ed3eddf-27ac-4f19-ba81-0ecd07aeb50a;signature=6vMTTitxbU0/3DRXAEPej4HyqQTG+V/F5NLGNcqG2ys=

20180701T12:33:56+0000 is the correct value which i want to get substituted in ‘$(echo -n $timestamp’ but it’s not happening. $timestamp is not changing at all after the curl command execution.

To Test:– I initially assigned 0 to timestamp. Executed the above curl command. checked echo $timestamp, it is still same[0].

I am new to linux shell. Any help will be highly appreciated.

Advertisement

Answer

It becomes simple when you clean it up and break it down into multiple lines:

url="https://test.com/abcd/v1/test/123/impulse"
timestamp="$(date +%Y%m%dT%H:%M:%S+0000)"
nonce="$(cat /proc/sys/kernel/random/uuid)"
signature="$(openssl dgst -binary -sha256 -hmac '+abhishek' <<< "${timestamp}" 
             | openssl enc -base64)"  

auth_header="Authorization:EG1-HMAC-SHA256"
auth_header="${auth_header};timestamp=${timestamp}"
auth_header="${auth_header};nonce=${nonce}"
auth_header="${auth_header};signature=${signature}"

curl -H "${auth_header}" "${url}" -vv
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement