summaryrefslogtreecommitdiff
path: root/distixapi/authn.py
blob: 9875929081144ef7593a4059c25be96025b88a43 (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
39
40
41
42
# 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)


def get_scopes(users, request):
    raise AuthenticationError('foo')