forked from facebookresearch/detectron2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
41 lines (34 loc) · 1.23 KB
/
auth.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
import hashlib
import hmac
from requests import Request
from requests.auth import AuthBase
from config.constants import SMARTDATA_API_KEY
def compute_signature(endpoint: str, method: str, body: str) -> hmac.HMAC:
"""
Computes the signature for a signed request to smartdata, uses the DETECTION_SERVICE_API_KEY environment variable
:param endpoint:
:param method:
:param body:
:return:
@author: Conor Brosnan <c.brosnan@nationaldrones.com>
"""
params = [endpoint, method, body, SMARTDATA_API_KEY]
data = "".join(params)
computed_sig = hmac.new(SMARTDATA_API_KEY.encode('utf-8'), data.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
return computed_sig
class SignatureAuth(AuthBase):
"""
Adds a signature to a celery request for sending to smartdata
@author: Conor Brosnan <c.brosnan@nationaldrones.com>
"""
def __call__(self, request) -> Request:
if request.body is None:
body = ""
else:
body = request.body
request.headers = {
'Accept': 'application/json',
'Signature': compute_signature(request.path_url, request.method, body),
'Content-Type': 'application/json',
}
return request