Guest User

Untitled

a guest
Sep 18th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | Source Code | 0 0
  1. import base64
  2. import datetime
  3. import hashlib
  4. import json
  5. import requests
  6. from cryptography.hazmat.backends import default_backend
  7. from cryptography.hazmat.primitives import serialization
  8. from cryptography.hazmat.primitives.asymmetric import padding
  9. from cryptography.hazmat.primitives import hashes
  10.  
  11.  
  12. DEBUG = True
  13. SANDBOX = True
  14.  
  15. # Endpoint/Host
  16. ENDPOINT = "https://staging.authservices.satispay.com" if SANDBOX else "https://authservices.satispay.com"
  17. HOST = ENDPOINT.replace("https://", "")
  18. # Key 1 got from server for token 1
  19. TOKEN_1 = "STJ2P2"
  20. KEY_ID_1 = "9v77oksdn6d1pktlgmt6a4eg8j8r6ue1rvfd9nu6pnmmtnev57ckuuer9n25edq4k59f1r4smn84k103s9ndevei07bf43la0du967qqu2uu298shvf1916n85i2925kigu847arg9tf5ukr345gbkt4oppij03rqtovvs6jqoq9grhc0g7jhrldpr2hhjkv211bl3ef"
  21. # Key 2 got from server for token 2
  22. TOKEN_2 = "U5C3RN"
  23. KEY_ID_2 = "ldr6m73p4kiitg5ftr2v2abugud6qi6thq8k62lmu7425n52c0c4gvqa0fl6hn118qpfj4oon9gka8jn1orskd4lp16ksrrnv6lj1alrn93pvpnq8ctj380fnm5ef2al126598npec1cckitjm682l9ukjqpt9qilddlbo1b1jhne9gvjplko2cuq4qn29nlacet2kf6"
  24.  
  25. # To be used
  26. TOKEN = TOKEN_2
  27. KEY_ID = KEY_ID_2
  28.  
  29.  
  30. def debug(msg, *args):
  31.     if DEBUG:
  32.         print(msg, *args)
  33.  
  34.  
  35. def load_pub_key():
  36.     with open("public.pem", "r") as fin:
  37.         return fin.read()
  38.  
  39.  
  40. def load_priv_key():
  41.     with open("private.pem", "rb") as fin:
  42.         return serialization.load_pem_private_key(
  43.             fin.read(),
  44.             password=None,
  45.             backend=default_backend()
  46.         )
  47.  
  48.  
  49. def get_key_id():
  50.     print("*** GET KEY ID ***")
  51.  
  52.     target = "/g_business/v1/authentication_keys"
  53.     headers = {
  54.         "content-type": "application/json",
  55.     }
  56.     data = {
  57.         "public_key": load_pub_key(),
  58.         "token": TOKEN,
  59.     }
  60.  
  61.     debug("Headers:\n" + json.dumps(headers, indent=4))
  62.     debug("Data:\n" + json.dumps(data, indent=4))
  63.  
  64.     response = requests.post(
  65.         ENDPOINT + target,
  66.         json=data,
  67.         headers=headers
  68.     )
  69.  
  70.     print("Response:\n:", response.json())
  71.  
  72.  
  73. def get_authorization_header(body_json, target):
  74.     # Digest
  75.     body = json.dumps(body_json)
  76.     digest = "SHA-256=" + base64.b64encode(hashlib.sha256(body.encode()).digest()).decode()
  77.     debug("Digest:\n", digest)
  78.  
  79.     # Date
  80.     date = datetime.datetime.now(datetime.timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT')
  81.     debug("Date:\n", date)
  82.  
  83.     # Message
  84.     message = "(request-target): post " + target + "\n\
  85.    host: " + HOST + "\n\
  86.    date: " + date + "\n\
  87.    digest: " + digest
  88.     debug("Message:\n", message)
  89.  
  90.     # Signature
  91.     signature = load_priv_key().sign(
  92.         message.encode(),
  93.         padding.PKCS1v15(),
  94.         hashes.SHA256()
  95.     )
  96.     signature = base64.b64encode(signature).decode()
  97.     debug("Signature:\n", signature)
  98.  
  99.     # Authorization
  100.     auth = f'Signature keyId="{KEY_ID}", algorithm="rsa-sha256", headers="(request-target) host date digest", signature="{signature}"'
  101.     debug("Authorization:\n", auth)
  102.     debug ("")
  103.  
  104.     return {
  105.         "date": date,
  106.         "digest": digest,
  107.         "Authorization": auth,
  108.     }
  109.  
  110.  
  111. def test_authentication(body_json):
  112.     print("*** TEST AUTHENTICATION ***")
  113.  
  114.     target = "/wally-services/protocol/tests/signature"
  115.     headers = {
  116.         "content-type": "application/json",
  117.         "host": HOST,
  118.         **get_authorization_header(body_json, target),
  119.     }
  120.     data = body_json
  121.  
  122.     debug("Headers:\n" + json.dumps(headers, indent=4))
  123.     debug("Data:\n" + json.dumps(data, indent=4))
  124.  
  125.     response = requests.post(
  126.         ENDPOINT + target,
  127.         json=data,
  128.         headers=headers
  129.     )
  130.  
  131.     print("Response:\n", response.json())
  132.  
  133.  
  134. if __name__ == "__main__":
  135.     body_json = {
  136.         "flow": "MATCH_CODE",
  137.         "amount_unit": 100,
  138.         "currency": "EUR"
  139.     }
  140.  
  141.     print("Insert function:")
  142.     print("  1. Get key ID")
  143.     print("  2. Test authorization")
  144.     fct = int(input())
  145.  
  146.     print("")
  147.  
  148.     if fct == 1:
  149.         get_key_id()
  150.     elif fct == 2:
  151.         test_authentication(body_json)
  152.     else:
  153.         print("Unsupported function")
  154.  
Add Comment
Please, Sign In to add comment