summaryrefslogtreecommitdiff
path: root/distixapi/authn.py
blob: f95f74d7d1ce191ce9d468bd34d0d91831537132 (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
33
34
35
36
37
38
# Functions for checking authantication for API clients.


import base64
import scrypt


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



def encrypt_password(salt, password):
    return scrypt.hash(password, salt)