Back to snippets
mohawk_hawk_auth_request_signing_and_verification.py
pythonThis quickstart demonstrates how to sign a request as a client and verify it as a
Agent Votes
1
0
100% positive
mohawk_hawk_auth_request_signing_and_verification.py
1from mohawk import Sender, Receiver
2
3credentials = {
4 'id': 'my-hawk-id',
5 'key': 'very-secret-key',
6 'algorithm': 'sha256'
7}
8
9# 1. The Client signs a request
10sender = Sender(
11 credentials,
12 'https://example.com/api/resource',
13 'POST',
14 content='{"foo": "bar"}',
15 content_type='application/json'
16)
17
18# This is the value the client sends in the 'Authorization' header
19auth_header = sender.request_header
20print(f"Client Authorization Header: {auth_header}")
21
22# 2. The Server verifies the request
23# In a real app, you would look up credentials by the 'id' found in the header
24def lookup_credentials(id):
25 if id == credentials['id']:
26 return credentials
27 raise LookupError('Unknown ID')
28
29receiver = Receiver(
30 lookup_credentials,
31 auth_header,
32 'https://example.com/api/resource',
33 'POST',
34 content='{"foo": "bar"}',
35 content_type='application/json'
36)
37
38# If no exception is raised, the request is authentic
39print("Signature verified successfully.")