Mohawk is an alternate Python implementation of the Hawk HTTP authorization scheme. Hawk allows two parties to securely communicate with each other using messages signed by a shared key. It is based on HTTP MAC access authentication (which was derived from parts of OAuth 1.0). The library's API was designed to be intuitive, less prone to security problems, and more Pythonic compared to other implementations. The current version is 1.1.0, with the last major release in late 2019, suggesting a stable, mature library.
pip install mohawkVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a basic Hawk authentication flow using `mohawk.Sender` to generate an authenticated request and `mohawk.Receiver` to verify it. It simulates the HTTP request and response headers and body. Note that `lookup_credentials` and `seen_nonce` callbacks are simplified for demonstration; a production application would integrate these with a secure credential store and a persistent, atomic nonce-checking mechanism to prevent replay attacks.
Ensure that Hawk header values are correctly encoded and do not contain disallowed escape characters. Review the Hawk specification for valid header content.
Always provide `content` and `content_type` when there is content, or explicitly set `accept_untrusted_content=True` if content hashing is intentionally skipped. Handle `mohawk.exc.MissingContent` if relevant.
Update your `seen_nonce` callback function to accept `sender_id` as the first argument, e.g., `def seen_nonce(sender_id, nonce, timestamp):`.
Implement a `seen_nonce(sender_id, nonce, timestamp)` callable that checks a persistent, atomic store (e.g., database, Redis) to determine if a nonce for a given sender and timestamp has already been processed. Return `True` if seen, `False` otherwise.
Ensure all servers involved in Hawk communication have their clocks synchronized using a reliable service (e.g., NTP, TLSdate). Hawk provides mechanisms for senders to adjust timestamps, but proper server clock sync is foundational.
Only disable content hashing if you fully understand the security implications and have alternative integrity checks in place. For most use cases, content hashing should remain enabled to prevent tampering.
Ensure that the server and client clocks are synchronized (e.g., using NTP or TLSdate). The receiver should respond with a `WWW-Authenticate` header including the server's current timestamp and MAC, allowing compliant clients to adjust their clocks and retry the request.
Double-check that both the sender and receiver are using the exact same credentials (ID, key, algorithm) and that all components of the request (method, URL, headers, content, content-type) are correctly included in the MAC calculation. Setting `mohawk` logging to DEBUG can provide more detailed information for debugging.
Implement a robust `seen_nonce` callback function that persistently stores and checks nonces to prevent replay attacks. This function should return `True` if the `(sender_id, nonce, timestamp)` combination has been seen recently.
Install the `mohawk` library using pip: `pip install mohawk`.