aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/middleware/authenticator.py
blob: 8c506f9f8ed9adb0e7859ce8bbce083ad8821928 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
import base64
import falcon
from szurubooru import db, errors
from szurubooru.util import auth, users

class Authenticator(object):
    '''
    Authenticates every request and put information on active user in the
    request context.
    '''

    def process_request(self, request, _response):
        ''' Bind the user to request. Update last login time if needed. '''
        request.context.user = self._get_user(request)
        if request.get_param_as_bool('bump-login') \
                and request.context.user.user_id:
            users.bump_login_time(request.context.user)
            request.context.session.commit()

    def _get_user(self, request):
        if not request.auth:
            return self._create_anonymous_user()

        try:
            auth_type, user_and_password = request.auth.split(' ', 1)
            if auth_type.lower() != 'basic':
                raise falcon.HTTPBadRequest(
                    'Invalid authentication type',
                    'Only basic authorization is supported.')
            username, password = base64.decodebytes(
                user_and_password.encode('ascii')).decode('utf8').split(':')
            return self._authenticate(
                request.context.session, username, password)
        except ValueError as err:
            msg = 'Basic authentication header value not properly formed. ' \
                + 'Supplied header {0}. Got error: {1}'
            raise falcon.HTTPBadRequest(
                'Malformed authentication request',
                msg.format(request.auth, str(err)))

    def _authenticate(self, session, username, password):
        ''' Try to authenticate user. Throw AuthError for invalid users. '''
        user = users.get_by_name(session, username)
        if not user:
            raise errors.AuthError('No such user.')
        if not auth.is_valid_password(user, password):
            raise errors.AuthError('Invalid password.')
        return user

    def _create_anonymous_user(self):
        user = db.User()
        user.name = None
        user.rank = 'anonymous'
        user.password = None
        return user

© 2015 - 2026 Jakob L. Kreuze