signing.py 1012 Bytes
Newer Older
1
2
3
4
5
6
7
import requests
import requests_http_signature


def verify(request, public_key):
    return requests_http_signature.HTTPSignatureAuth.verify(
        request,
8
9
        key_resolver=lambda **kwargs: public_key,
        use_auth_header=False,
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    )


def verify_django(django_request, public_key):
    """
    Given a django WSGI request, create an underlying requests.PreparedRequest
    instance we can verify
    """
    headers = django_request.META.get('headers', {}).copy()
    for h, v in list(headers.items()):
        # we include lower-cased version of the headers for compatibility
        # with requests_http_signature
        headers[h.lower()] = v
    try:
24
        signature = headers['signature']
25
26
27
28
29
30
31
32
33
34
35
    except KeyError:
        raise exceptions.MissingSignature

    request = requests.Request(
        method=django_request.method,
        url='http://noop',
        data=django_request.body,
        headers=headers)

    prepared_request = request.prepare()
    return verify(request, public_key)