From 6904b5f21fb680b94fcf5ec38c295c4823e31445 Mon Sep 17 00:00:00 2001 From: drebs Date: Sat, 4 May 2013 10:46:46 -0300 Subject: Encapsulate token-based auth in a class. --- src/leap/soledad/auth.py | 62 +++++++++++++++++------------ src/leap/soledad/backends/leap_backend.py | 34 ++++++++++++---- src/leap/soledad/shared_db.py | 34 ++++++++++++---- src/leap/soledad/tests/test_leap_backend.py | 38 +++++++++--------- 4 files changed, 111 insertions(+), 57 deletions(-) diff --git a/src/leap/soledad/auth.py b/src/leap/soledad/auth.py index a099c1a6..1d8f1a42 100644 --- a/src/leap/soledad/auth.py +++ b/src/leap/soledad/auth.py @@ -27,32 +27,44 @@ they can do token-based auth requests to the Soledad server. from u1db.remote.http_client import HTTPClientBase -def set_token_credentials(self, uuid, token): +class TokenBasedAuth(object): """ - Store given credentials so we can sign the request later. - - @param uuid: The user's uuid. - @type uuid: str - @param token: The authentication token. - @type token: str + Encapsulate token-auth methods for classes that inherit from + u1db.remote.http_client.HTTPClient. """ - self._creds = {'token': (uuid, token)} + def set_token_credentials(self, uuid, token): + """ + Store given credentials so we can sign the request later. -def _sign_request(self, method, url_query, params): - """ - Return an authorization header to be included in the HTTP request. - - @param method: The HTTP method. - @type method: str - @param url_query: The URL query string. - @type url_query: str - @param params: A list with encoded query parameters. - @type param: list - """ - if 'token' in self._creds: - uuid, token = self._creds['token'] - auth = '%s:%s' % (uuid, token) - return [('Authorization', 'Token %s' % auth.encode('base64')[:-1])] - else: - return HTTPClientBase._sign_request(self, method, url_query, params) + @param uuid: The user's uuid. + @type uuid: str + @param token: The authentication token. + @type token: str + """ + self._creds = {'token': (uuid, token)} + + + def _sign_request(self, method, url_query, params): + """ + Return an authorization header to be included in the HTTP request, in + the form: + + [('Authorization', 'Token