-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
53 lines (38 loc) · 1.13 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import hashlib
import hmac
import json
import os
from validators import ValidationFailure, url as v_url
def is_url(source):
try:
return v_url(source or "")
except ValidationFailure:
return False
def _hmac_generate_signature(message):
return hmac.new(
os.getenv("AUTH_PRIVATE_KEY").encode("utf-8"),
message.encode("utf-8"),
hashlib.sha256,
).hexdigest()
def hmac_generate(data: dict):
message = json.dumps(data)
signature = _hmac_generate_signature(message)
return message, {
"X-Api-Key": os.getenv("AUTH_PUBLIC_KEY"),
"X-Api-Signature": signature,
}
def hmac_validate():
def _validate(value, request):
if os.getenv("APP_ENV") == "local":
return True
message = json.dumps(request.json)
signature = _hmac_generate_signature(message)
return hmac.compare_digest(value or "", signature)
return {
"headers": {
"x-api-signature": _validate,
},
"messages": {"_validate": "Invalid authentication credentials"},
"status_code": 401,
"return_valid": False,
}