Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import base64
- import datetime
- import hashlib
- import json
- import requests
- from cryptography.hazmat.backends import default_backend
- from cryptography.hazmat.primitives import serialization
- from cryptography.hazmat.primitives.asymmetric import padding
- from cryptography.hazmat.primitives import hashes
- DEBUG = True
- SANDBOX = True
- # Endpoint/Host
- ENDPOINT = "https://staging.authservices.satispay.com" if SANDBOX else "https://authservices.satispay.com"
- HOST = ENDPOINT.replace("https://", "")
- # Key 1 got from server for token 1
- TOKEN_1 = "STJ2P2"
- KEY_ID_1 = "9v77oksdn6d1pktlgmt6a4eg8j8r6ue1rvfd9nu6pnmmtnev57ckuuer9n25edq4k59f1r4smn84k103s9ndevei07bf43la0du967qqu2uu298shvf1916n85i2925kigu847arg9tf5ukr345gbkt4oppij03rqtovvs6jqoq9grhc0g7jhrldpr2hhjkv211bl3ef"
- # Key 2 got from server for token 2
- TOKEN_2 = "U5C3RN"
- KEY_ID_2 = "ldr6m73p4kiitg5ftr2v2abugud6qi6thq8k62lmu7425n52c0c4gvqa0fl6hn118qpfj4oon9gka8jn1orskd4lp16ksrrnv6lj1alrn93pvpnq8ctj380fnm5ef2al126598npec1cckitjm682l9ukjqpt9qilddlbo1b1jhne9gvjplko2cuq4qn29nlacet2kf6"
- # To be used
- TOKEN = TOKEN_2
- KEY_ID = KEY_ID_2
- def debug(msg, *args):
- if DEBUG:
- print(msg, *args)
- def load_pub_key():
- with open("public.pem", "r") as fin:
- return fin.read()
- def load_priv_key():
- with open("private.pem", "rb") as fin:
- return serialization.load_pem_private_key(
- fin.read(),
- password=None,
- backend=default_backend()
- )
- def get_key_id():
- print("*** GET KEY ID ***")
- target = "/g_business/v1/authentication_keys"
- headers = {
- "content-type": "application/json",
- }
- data = {
- "public_key": load_pub_key(),
- "token": TOKEN,
- }
- debug("Headers:\n" + json.dumps(headers, indent=4))
- debug("Data:\n" + json.dumps(data, indent=4))
- response = requests.post(
- ENDPOINT + target,
- json=data,
- headers=headers
- )
- print("Response:\n:", response.json())
- def get_authorization_header(body_json, target):
- # Digest
- body = json.dumps(body_json)
- digest = "SHA-256=" + base64.b64encode(hashlib.sha256(body.encode()).digest()).decode()
- debug("Digest:\n", digest)
- # Date
- date = datetime.datetime.now(datetime.timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT')
- debug("Date:\n", date)
- # Message
- message = "(request-target): post " + target + "\n\
- host: " + HOST + "\n\
- date: " + date + "\n\
- digest: " + digest
- debug("Message:\n", message)
- # Signature
- signature = load_priv_key().sign(
- message.encode(),
- padding.PKCS1v15(),
- hashes.SHA256()
- )
- signature = base64.b64encode(signature).decode()
- debug("Signature:\n", signature)
- # Authorization
- auth = f'Signature keyId="{KEY_ID}", algorithm="rsa-sha256", headers="(request-target) host date digest", signature="{signature}"'
- debug("Authorization:\n", auth)
- debug ("")
- return {
- "date": date,
- "digest": digest,
- "Authorization": auth,
- }
- def test_authentication(body_json):
- print("*** TEST AUTHENTICATION ***")
- target = "/wally-services/protocol/tests/signature"
- headers = {
- "content-type": "application/json",
- "host": HOST,
- **get_authorization_header(body_json, target),
- }
- data = body_json
- debug("Headers:\n" + json.dumps(headers, indent=4))
- debug("Data:\n" + json.dumps(data, indent=4))
- response = requests.post(
- ENDPOINT + target,
- json=data,
- headers=headers
- )
- print("Response:\n", response.json())
- if __name__ == "__main__":
- body_json = {
- "flow": "MATCH_CODE",
- "amount_unit": 100,
- "currency": "EUR"
- }
- print("Insert function:")
- print(" 1. Get key ID")
- print(" 2. Test authorization")
- fct = int(input())
- print("")
- if fct == 1:
- get_key_id()
- elif fct == 2:
- test_authentication(body_json)
- else:
- print("Unsupported function")
Add Comment
Please, Sign In to add comment