aiohttp: How to update request headers according to request body?

Viewed 158

I am trying to implement a type of custom authentication by using aiohttp something like the example in this link but I also need request body. Here is an example for requests:

class CustomAuth(AuthBase):
    def __init__(self, secretkey):
        self.secretkey = secretkey

    def get_hash(self, request):
        if request.body:
            data = request.body.decode('utf-8')
        else:
            data = "{}"

        signature = hmac.new(
            str.encode(self.secretkey),
            msg=str.encode(data),
            digestmod=hashlib.sha256
        ).hexdigest().upper()
        return signature

    def __call__(self, request):
        request.headers["CUSTOM-AUTH"] = self.get_hash(request)
        return request

I've looked into tracing and BasicAuth but they are useless in my situation. On on_request_start request body is not ready, on on_request_chunk_sent headers have already been sent. A solution like BasicAuth don't have access the request data at all.

Do you have any idea?

Thanks in advance.

0 Answers
Related