From cb2d3fd580b9b000c4d9002822935eb891b3d9c1 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 28 Jul 2023 01:21:24 +0800 Subject: [PATCH] [perf] activity auth & logger --- demo/activitypub.py | 25 ++++++++++++++++++++++++- demo/httpsig.py | 5 +++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/demo/activitypub.py b/demo/activitypub.py index 85db5d9..daf8199 100644 --- a/demo/activitypub.py +++ b/demo/activitypub.py @@ -3,6 +3,8 @@ import httpx from demo import config from demo.utils.key import get_pubkey_as_pem +from demo.httpsig import auth +from loguru import logger AS_CTX = "https://www.w3.org/ns/activitystreams" @@ -63,7 +65,7 @@ def fetch( url: str, ) -> dict: """Fetch url and return json""" - print(f"fetch {url}") + logger.debug(f"fetch {url}") with httpx.Client() as client: resp = client.get( @@ -75,3 +77,24 @@ def fetch( ) return resp.json() + + +def post( + url: str, + payload : dict, +) -> httpx.Response : + logger.debug(f"send post, {payload=}") + + with httpx.Client() as client: + resp = client.post( + url, + headers={ + "User-Agent": config.USER_AGENT, + "Content-Type": config.AP_CONTENT_TYPE, + }, + json=payload, + auth=auth, + ) + + resp.raise_for_status() + return resp diff --git a/demo/httpsig.py b/demo/httpsig.py index 65e7a29..253895d 100644 --- a/demo/httpsig.py +++ b/demo/httpsig.py @@ -136,7 +136,7 @@ class HTTPXSigAuth(httpx.Auth): else: sigheaders = "(request-target) user-agent host date accept" - logger.warning(request.headers) + logger.info(request.headers) sigdate = SignedData( method = request.method, @@ -150,13 +150,14 @@ class HTTPXSigAuth(httpx.Auth): sigdate ) + logger.debug(f"{to_be_signed}") + if not self.key: raise ValueError("Should never happen") signer = PKCS1_v1_5.new(self.key) digest = SHA256.new() digest.update(to_be_signed.encode("utf-8")) sig = base64.b64encode(signer.sign(digest)) - logger.debug(f"{sig=}") key_id = f"{ID}#main-key"