summaryrefslogtreecommitdiff
path: root/websockify/auth_plugins.py
blob: 924d5de2f35afe1f1e94358daf619211b14ad007 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class BasePlugin(object):
    def __init__(self, src=None):
        self.source = src

    def authenticate(self, headers, target_host, target_port):
        pass


class AuthenticationError(Exception):
    def __init__(self, log_msg=None, response_code=403, response_headers={}, response_msg=None):
        self.code = response_code
        self.headers = response_headers
        self.msg = response_msg

        if log_msg is None:
            log_msg = response_msg

        super(AuthenticationError, self).__init__('%s %s' % (self.code, log_msg))


class InvalidOriginError(AuthenticationError):
    def __init__(self, expected, actual):
        self.expected_origin = expected
        self.actual_origin = actual

        super(InvalidOriginError, self).__init__(
            response_msg='Invalid Origin',
            log_msg="Invalid Origin Header: Expected one of "
                    "%s, got '%s'" % (expected, actual))


class BasicHTTPAuth(object):
    def __init__(self, src=None):
        self.src = src

    def authenticate(self, headers, target_host, target_port):
        import base64

        auth_header = headers.get('Authorization')
        if auth_header:
            if not auth_header.startswith('Basic '):
                raise AuthenticationError(response_code=403)

            try:
                user_pass_raw = base64.b64decode(auth_header[6:])
            except TypeError:
                raise AuthenticationError(response_code=403)

            user_pass = user_pass_raw.split(':', 1)
            if len(user_pass) != 2:
                raise AuthenticationError(response_code=403)

            if not self.validate_creds:
                raise AuthenticationError(response_code=403)

        else:
            raise AuthenticationError(response_code=401,
                                      response_headers={'WWW-Authenticate': 'Basic realm="Websockify"'})

    def validate_creds(username, password):
        if '%s:%s' % (username, password) == self.src:
            return True
        else:
            return False

class ExpectOrigin(object):
    def __init__(self, src=None):
        if src is None:
            self.source = []
        else:
            self.source = src.split()

    def authenticate(self, headers, target_host, target_port):
        origin = headers.get('Origin', None)
        if origin is None or origin not in self.source:
            raise InvalidOriginError(expected=self.source, actual=origin)