summaryrefslogtreecommitdiff
path: root/src/leap/soledad/server/auth.py
blob: da13dc8c9c123de8db75a0a627da8ee91ef095e9 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- coding: utf-8 -*-
# auth.py
# Copyright (C) 2013 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Twisted http token auth.
"""
import os
import binascii
import time

from hashlib import sha512
from zope.interface import implementer

from twisted.cred import error
from twisted.cred.checkers import ICredentialsChecker
from twisted.cred.credentials import IUsernamePassword
from twisted.cred.credentials import IAnonymous
from twisted.cred.credentials import Anonymous
from twisted.cred.credentials import UsernamePassword
from twisted.cred.portal import IRealm
from twisted.cred.portal import Portal
from twisted.internet import defer
from twisted.logger import Logger
from twisted.web.iweb import ICredentialFactory
from twisted.web.resource import IResource

from leap.soledad.common.couch import couch_server

from ._resource import PublicResource, AnonymousResource
from ._resource import LocalResource
from ._blobs import BlobsResource
from ._config import get_config


log = Logger()


@implementer(IRealm)
class SoledadRealm(object):

    def __init__(self, sync_pool, conf=None):
        assert sync_pool is not None
        if conf is None:
            conf = get_config()
        blobs = conf['blobs']
        blobs_resource = BlobsResource("filesystem",
                                       conf['blobs_path']) if blobs else None
        self.anon_resource = AnonymousResource(
            enable_blobs=blobs)
        self.auth_resource = PublicResource(
            blobs_resource=blobs_resource,
            sync_pool=sync_pool)

    def requestAvatar(self, avatarId, mind, *interfaces):

        # Anonymous access
        if IAnonymous.providedBy(avatarId):
            return (IResource, self.anon_resource,
                    lambda: None)

        # Authenticated access
        else:
            if IResource in interfaces:
                return (IResource, self.auth_resource,
                        lambda: None)
        raise NotImplementedError()


@implementer(IRealm)
class LocalServicesRealm(object):

    def __init__(self):
        conf = get_config()
        self.anon_resource = AnonymousResource(
            enable_blobs=conf['blobs'])
        self.auth_resource = LocalResource()

    def requestAvatar(self, avatarId, mind, *interfaces):

        # Anonymous access
        if IAnonymous.providedBy(avatarId):
            return (IResource, self.anon_resource,
                    lambda: None)

        # Authenticated access
        else:
            if IResource in interfaces:
                return (IResource, self.auth_resource,
                        lambda: None)
        raise NotImplementedError()


@implementer(ICredentialsChecker)
class FileTokenChecker(object):
    credentialInterfaces = [IUsernamePassword, IAnonymous]

    def __init__(self, conf=None):
        # conf parameter is only used during tests
        conf = conf or get_config()
        self._trusted_services_tokens = {}
        self._tokens_file_path = conf['services_tokens_file']
        self._reload_tokens()

    def _reload_tokens(self):
        if not os.path.isfile(self._tokens_file_path):
            log.warn("No local token auth file at %s" % self._tokens_file_path)
            return
        with open(self._tokens_file_path) as tokens_file:
            for line in tokens_file.readlines():
                line = line.strip()
                if not line.startswith('#'):
                    service, token = line.split(':')
                    log.info("Loaded credentials for service: %s" % service)
                    self._trusted_services_tokens[service] = token

    def requestAvatarId(self, credentials):
        if IAnonymous.providedBy(credentials):
            return defer.succeed(Anonymous())

        service = credentials.username
        token = credentials.password

        # TODO: Use constant time comparison
        if self._trusted_services_tokens[service] != token:
            return defer.fail(error.UnauthorizedLogin())

        return defer.succeed(service)


@implementer(ICredentialsChecker)
class CouchDBTokenChecker(object):

    credentialInterfaces = [IUsernamePassword, IAnonymous]

    TOKENS_DB_PREFIX = "tokens_"
    TOKENS_DB_EXPIRE = 30 * 24 * 3600  # 30 days in seconds
    TOKENS_TYPE_KEY = "type"
    TOKENS_TYPE_DEF = "Token"
    TOKENS_USER_ID_KEY = "user_id"

    def __init__(self):
        self._couch_url = get_config().get('couch_url')

    def _get_server(self):
        return couch_server(self._couch_url)

    def _tokens_dbname(self):
        # the tokens db rotates every 30 days, and the current db name is
        # "tokens_NNN", where NNN is the number of seconds since epoch
        # divide dby the rotate period in seconds. When rotating, old and
        # new tokens db coexist during a certain window of time and valid
        # tokens are replicated from the old db to the new one. See:
        # https://leap.se/code/issues/6785
        dbname = self.TOKENS_DB_PREFIX + \
            str(int(time.time() / self.TOKENS_DB_EXPIRE))
        return dbname

    def _tokens_db(self):
        dbname = self._tokens_dbname()

        # TODO -- leaking abstraction here: this module shouldn't need
        # to known anything about the context manager. hide that in the couch
        # module
        with self._get_server() as server:
            db = server[dbname]
        return db

    def requestAvatarId(self, credentials):
        if IAnonymous.providedBy(credentials):
            return defer.succeed(Anonymous())

        uuid = credentials.username
        token = credentials.password

        # lookup key is a hash of the token to prevent timing attacks.
        # TODO cache the tokens already!

        db = self._tokens_db()
        token = db.get(sha512(token).hexdigest())
        if token is None:
            return defer.fail(error.UnauthorizedLogin())

        # TODO -- use cryptography constant time builtin comparison.
        # we compare uuid hashes to avoid possible timing attacks that
        # might exploit python's builtin comparison operator behaviour,
        # which fails immediatelly when non-matching bytes are found.
        couch_uuid_hash = sha512(token[self.TOKENS_USER_ID_KEY]).digest()
        req_uuid_hash = sha512(uuid).digest()
        if token[self.TOKENS_TYPE_KEY] != self.TOKENS_TYPE_DEF \
                or couch_uuid_hash != req_uuid_hash:
            return defer.fail(error.UnauthorizedLogin())

        return defer.succeed(uuid)


@implementer(ICredentialFactory)
class TokenCredentialFactory(object):

    scheme = 'token'

    def getChallenge(self, request):
        return {}

    def decode(self, response, request):
        try:
            creds = binascii.a2b_base64(response + b'===')
        except binascii.Error:
            raise error.LoginFailed('Invalid credentials')

        creds = creds.split(b':', 1)
        if len(creds) == 2:
            return UsernamePassword(*creds)
        else:
            raise error.LoginFailed('Invalid credentials')


def publicPortal(sync_pool):
    database_checker = CouchDBTokenChecker()
    realm = SoledadRealm(sync_pool=sync_pool)
    auth_checkers = [database_checker]
    return Portal(realm, auth_checkers)


def localPortal():
    file_checker = FileTokenChecker()
    realm = LocalServicesRealm()
    auth_checkers = [file_checker]
    return Portal(realm, auth_checkers)


credentialFactory = TokenCredentialFactory()