-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
37 lines (31 loc) · 1.15 KB
/
client.py
File metadata and controls
37 lines (31 loc) · 1.15 KB
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
import httpx
from mauth_client.config import Config
from mauth_client.signable import RequestSignable
from mauth_client.signer import Signer
class MAuthHttpx(httpx.Auth):
"""
HTTPX authentication for MAuth.
Adds MAuth headers based on method, URL, and body bytes.
"""
# We need the body bytes to sign the request
requires_request_body = True
def __init__(
self,
app_uuid: str,
private_key_data: str,
sign_versions: str = Config.SIGN_VERSIONS,
):
self.signer = Signer(app_uuid, private_key_data, sign_versions)
def _make_headers(self, request: httpx.Request) -> dict[str, str]:
# With requires_request_body=True, httpx ensures the content is buffered.
body = request.content or b""
req_signable = RequestSignable(
method=request.method,
url=str(request.url),
body=body,
)
return self.signer.signed_headers(req_signable)
def auth_flow(self, request: httpx.Request):
# Body is already read due to requires_request_body=True.
request.headers.update(self._make_headers(request))
yield request