Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Invalid signature using Python script #115

Closed
virtusavivek opened this issue Apr 22, 2022 · 1 comment
Closed

Get Invalid signature using Python script #115

virtusavivek opened this issue Apr 22, 2022 · 1 comment

Comments

@virtusavivek
Copy link

virtusavivek commented Apr 22, 2022

I have attached the python program below. Please help up to resolve.
Thanks in advance.

import sys, os, base64, datetime, hashlib, hmac 
import requests
method = 'GET'
service = 'para'
host = '{host}'
region = 'us-east-1'
endpoint = 'https://{host}/v1/_setup/s-dev'
def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()

def getSignatureKey(key, dateStamp, regionName, serviceName):
    kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
    kRegion = sign(kDate, regionName)
    kService = sign(kRegion, serviceName)
    kSigning = sign(kService, 'aws4_request')
    return kSigning
access_key = 'app:{rootapp}'
secret_key = ""
if access_key is None or secret_key is None:
    print('No access key is available.')
    sys.exit()
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')
canonical_uri = '/' 
canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + amzdate + '\n'
signed_headers = 'host;x-amz-date'
payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()
canonical_request = method + '\n' + canonical_uri + '\n' +  canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope =  datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' +  amzdate + '\n' +  credential_scope + '\n' +  hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, datestamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' +  'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'x-amz-date':amzdate, 'Authorization':authorization_header}

request_url = endpoint

print('\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++')
print('Request URL = ' + request_url)
r = requests.get(request_url, headers=headers)

print('\nRESPONSE++++++++++++++++++++++++++++++++++++')
print('Response Headers:',headers)
print('Response code: %d\n' % r.status_code)
print(r.text)
@albogdano
Copy link
Member

Why aren't you using the Para client for Python? There is a method there invokeSignedRequest() for that purpose.
If you are stuck, just generate a new JWT super token to access the API, using your app's secret key, as described in the Para docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants