summaryrefslogtreecommitdiff
path: root/server/src/leap/soledad/server/auth.py
blob: 3bcfcf042dd8f67d6954008bc31db8aed72d9630 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# -*- 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/>.


"""
Authentication facilities for Soledad Server.
"""


import httplib
import simplejson as json


from u1db import DBNAME_CONSTRAINTS
from abc import ABCMeta, abstractmethod
from routes.mapper import Mapper
from couchdb.client import Server
from twisted.python import log


#-----------------------------------------------------------------------------
# Authentication
#-----------------------------------------------------------------------------

class Unauthorized(Exception):
    """
    User authentication failed.
    """


class URLToAuthorization(object):
    """
    Verify if actions can be performed by a user.
    """

    HTTP_METHOD_GET = 'GET'
    HTTP_METHOD_PUT = 'PUT'
    HTTP_METHOD_DELETE = 'DELETE'
    HTTP_METHOD_POST = 'POST'

    def __init__(self, uuid, shared_db_name, user_db_prefix):
        """
        Initialize the mapper.

        The C{uuid} is used to create the rules that will either allow or
        disallow the user to perform specific actions.

        @param uuid: The user uuid.
        @type uuid: str
        @param shared_db_name: The name of the shared database that holds
            user's encrypted secrets.
        @type shared_db_name: str
        @param user_db_prefix: The string prefix of users' databases.
        @type user_db_prefix: str
        """
        self._map = Mapper(controller_scan=None)
        self._user_db_prefix = user_db_prefix
        self._shared_db_name = shared_db_name
        self._register_auth_info(self._uuid_dbname(uuid))

    def is_authorized(self, environ):
        """
        Return whether an HTTP request that produced the CGI C{environ}
        corresponds to an authorized action.

        @param environ: Dictionary containing CGI variables.
        @type environ: dict

        @return: Whether the action is authorized or not.
        @rtype: bool
        """
        return self._map.match(environ=environ) is not None

    def _register(self, pattern, http_methods):
        """
        Register a C{pattern} in the mapper as valid for C{http_methods}.

        @param pattern: The URL pattern that corresponds to the user action.
        @type pattern: str
        @param http_methods: A list of authorized HTTP methods.
        @type http_methods: list of str
        """
        self._map.connect(
            None, pattern, http_methods=http_methods,
            conditions=dict(method=http_methods),
            requirements={'dbname': DBNAME_CONSTRAINTS})

    def _uuid_dbname(self, uuid):
        """
        Return the database name corresponding to C{uuid}.

        @param uuid: The user uid.
        @type uuid: str

        @return: The database name corresponding to C{uuid}.
        @rtype: str
        """
        return '%s%s' % (self._user_db_prefix, uuid)

    def _register_auth_info(self, dbname):
        """
        Register the authorization info in the mapper using C{dbname} as the
        user's database name.

        This method sets up the following authorization rules:

            URL path                      | Authorized actions
            --------------------------------------------------
            /                             | GET
            /shared-db                    | GET
            /shared-db/docs               | -
            /shared-db/doc/{id}           | GET, PUT, DELETE
            /shared-db/sync-from/{source} | -
            /user-db                      | GET, PUT, DELETE
            /user-db/docs                 | -
            /user-db/doc/{id}             | -
            /user-db/sync-from/{source}   | GET, PUT, POST

        @param dbname: The name of the user's database.
        @type dbname: str
        """
        # auth info for global resource
        self._register('/', [self.HTTP_METHOD_GET])
        # auth info for shared-db database resource
        self._register(
            '/%s' % self._shared_db_name,
            [self.HTTP_METHOD_GET])
        # auth info for shared-db doc resource
        self._register(
            '/%s/doc/{id:.*}' % self._shared_db_name,
            [self.HTTP_METHOD_GET, self.HTTP_METHOD_PUT,
             self.HTTP_METHOD_DELETE])
        # auth info for user-db database resource
        self._register(
            '/%s' % dbname,
            [self.HTTP_METHOD_GET, self.HTTP_METHOD_PUT,
             self.HTTP_METHOD_DELETE])
        # auth info for user-db sync resource
        self._register(
            '/%s/sync-from/{source_replica_uid}' % dbname,
            [self.HTTP_METHOD_GET, self.HTTP_METHOD_PUT,
             self.HTTP_METHOD_POST])
        # generate the regular expressions
        self._map.create_regs()


class SoledadAuthMiddleware(object):
    """
    Soledad Authentication WSGI middleware.

    This class must be extended to implement specific authentication methods
    (see SoledadTokenAuthMiddleware below).

    It expects an HTTP_AUTHORIZATION header containing the the concatenation of
    the following strings:

        1. The authentication scheme. It will be verified by the
           _verify_authentication_scheme() method.

        2. A space character.

        3. The base64 encoded string of the concatenation of the user uuid with
           the authentication data, separated by a collon, like this:

               base64("<uuid>:<auth_data>")

    After authentication check, the class performs an authorization check to
    verify whether the user is authorized to perform the requested action.

    On client-side, 2 methods must be implemented so the soledad client knows
    how to send authentication headers to server:

        * set_<method>_credentials: store authentication credentials in the
          class.

        * _sign_request: format and include custom authentication data in
          the HTTP_AUTHORIZATION header.

    See leap.soledad.auth and u1db.remote.http_client.HTTPClient to understand
    how to do it.
    """

    __metaclass__ = ABCMeta

    HTTP_AUTH_KEY = "HTTP_AUTHORIZATION"
    PATH_INFO_KEY = "PATH_INFO"

    CONTENT_TYPE_JSON = ('content-type', 'application/json')

    def __init__(self, app):
        """
        Initialize the Soledad Authentication Middleware.

        @param app: The application to run on successfull authentication.
        @type app: u1db.remote.http_app.HTTPApp
        @param prefix: Auth app path prefix.
        @type prefix: str
        """
        self._app = app

    def _error(self, start_response, status, description, message=None):
        """
        Send a JSON serialized error to WSGI client.

        @param start_response: Callable of the form start_response(status,
            response_headers, exc_info=None).
        @type start_response: callable
        @param status: Status string of the form "999 Message here"
        @type status: str
        @param response_headers: A list of (header_name, header_value) tuples
            describing the HTTP response header.
        @type response_headers: list
        @param description: The error description.
        @type description: str
        @param message: The error message.
        @type message: str

        @return: List with JSON serialized error message.
        @rtype list
        """
        start_response("%d %s" % (status, httplib.responses[status]),
                       [self.CONTENT_TYPE_JSON])
        err = {"error": description}
        if message:
            err['message'] = message
        return [json.dumps(err)]

    def _unauthorized_error(self, start_response, message):
        """
        Send a unauth error.

        @param message: The error message.
        @type message: str
        @param start_response: Callable of the form start_response(status,
            response_headers, exc_info=None).
        @type start_response: callable

        @return: List with JSON serialized error message.
        @rtype list
        """
        return self._error(
            start_response,
            401,
            "unauthorized",
            message)

    def __call__(self, environ, start_response):
        """
        Handle a WSGI call to the authentication application.

        @param environ: Dictionary containing CGI variables.
        @type environ: dict
        @param start_response: Callable of the form start_response(status,
            response_headers, exc_info=None).
        @type start_response: callable

        @return: Target application results if authentication succeeds, an
        error message otherwise.
        @rtype: list
        """
        # check for authentication header
        auth = environ.get(self.HTTP_AUTH_KEY)
        if not auth:
            return self._unauthorized_error(
                start_response, "Missing authentication header.")

        # get authentication data
        scheme, encoded = auth.split(None, 1)
        uuid, auth_data = encoded.decode('base64').split(':', 1)
        if not self._verify_authentication_scheme(scheme):
            return self._unauthorized_error("Wrong authentication scheme")

        # verify if user is athenticated
        if not self._verify_authentication_data(uuid, auth_data):
            return self._unauthorized_error(
                start_response,
                self._get_auth_error_string())

        # verify if user is authorized to perform action
        if not self._verify_authorization(environ, uuid):
            return self._unauthorized_error(
                start_response,
                "Unauthorized action.")

        # move on to the real Soledad app
        del environ[self.HTTP_AUTH_KEY]
        return self._app(environ, start_response)

    @abstractmethod
    def _verify_authentication_scheme(self, scheme):
        """
        Verify if authentication scheme is valid.

        @param scheme: Auth scheme extracted from the HTTP_AUTHORIZATION
            header.
        @type scheme: str

        @return: Whether the authentitcation scheme is valid.
        """
        return None

    @abstractmethod
    def _verify_authentication_data(self, uuid, auth_data):
        """
        Verify valid authenticatiion for this request.

        @param uuid: The user's uuid.
        @type uuid: str
        @param auth_data: Authentication data.
        @type auth_data: str

        @return: Whether the token is valid for authenticating the request.
        @rtype: bool
        """
        return None

    def _verify_authorization(self, environ, uuid):
        """
        Verify if the user is authorized to perform the requested action over
        the requested database.

        @param environ: Dictionary containing CGI variables.
        @type environ: dict
        @param uuid: The user's uuid.
        @type uuid: str

        @return: Whether the user is authorize to perform the requested action
            over the requested db.
        @rtype: bool
        """
        return URLToAuthorization(
            uuid, self._app.SHARED_DB_NAME,
            self._app.USER_DB_PREFIX
        ).is_authorized(environ)

    @abstractmethod
    def _get_auth_error_string(self):
        """
        Return an error string specific for each kind of authentication method.

        @return: The error string.
        """
        return None


class SoledadTokenAuthMiddleware(SoledadAuthMiddleware):
    """
    Token based authentication.
    """

    TOKENS_DB = "tokens"
    TOKENS_TYPE_KEY = "type"
    TOKENS_TYPE_DEF = "Token"
    TOKENS_USER_ID_KEY = "user_id"

    TOKEN_AUTH_ERROR_STRING = "Incorrect address or token."

    def _verify_authentication_scheme(self, scheme):
        """
        Verify if authentication scheme is valid.

        @param scheme: Auth scheme extracted from the HTTP_AUTHORIZATION
            header.
        @type scheme: str

        @return: Whether the authentitcation scheme is valid.
        """
        if scheme.lower() != 'token':
            return False
        return True

    def _verify_authentication_data(self, uuid, auth_data):
        """
        Extract token from C{auth_data} and proceed with verification of
        C{uuid} authentication.

        @param uuid: The user UID.
        @type uuid: str
        @param auth_data: Authentication data (i.e. the token).
        @type auth_data: str

        @return: Whether the token is valid for authenticating the request.
        @rtype: bool
        """
        token = auth_data  # we expect a cleartext token at this point
        return self._verify_token_in_couchdb(uuid, token)

    def _verify_token_in_couchdb(self, uuid, token):
        """
        Query couchdb to decide if C{token} is valid for C{uuid}.

        @param uuid: The user uuid.
        @type uuid: str
        @param token: The token.
        @type token: str
        """
        server = Server(url=self._app.state.couch_url)
        try:
            dbname = self.TOKENS_DB
            db = server[dbname]
            token = db.get(token)
            if token is None:
                return False
            return token[self.TOKENS_TYPE_KEY] == self.TOKENS_TYPE_DEF and \
                token[self.TOKENS_USER_ID_KEY] == uuid
        except Exception as e:
            log.err(e)
            return False
        return True

    def _get_auth_error_string(self):
        """
        Get the error string for token auth.

        @return: The error string.
        """
        return self.TOKEN_AUTH_ERROR_STRING