summaryrefslogtreecommitdiff
path: root/distixapi/authn.py
blob: a7fbbcf28a1f967b8cb23baaaed2c244b43a1e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Functions for checking authantication for API clients.


import base64


def get_credentials(request):
    '''Return username, password of API client.

    They're assumed to be conveyed in an Authorization header using
    Basic Auth.

    '''

    header = request.get_header('Authorization')
    if header is None:
        raise AuthenticationError('No Authorization header')

    words = header.split()
    if len(words) == 0:
        raise AuthenticationError('Authorization header has no value')

    if len(words) != 2 or words[0].lower() != 'basic':
        raise AuthenticationError('Authorization header is not for Basic Auth')

    decoded = base64.b64decode(words[1])
    return decoded.split(':', 1)


class AuthenticationError(Exception):

    pass