diff options
author | drebs <drebs@leap.se> | 2013-05-04 10:46:46 -0300 |
---|---|---|
committer | drebs <drebs@leap.se> | 2013-05-04 10:46:46 -0300 |
commit | 6904b5f21fb680b94fcf5ec38c295c4823e31445 (patch) | |
tree | ca4d29315c9e14a4680a5055a60cdeb6b4f3ba60 /src/leap/soledad/auth.py | |
parent | 08b4d702d4c3d7427d8f794fa3176a5c63a52d18 (diff) |
Encapsulate token-based auth in a class.
Diffstat (limited to 'src/leap/soledad/auth.py')
-rw-r--r-- | src/leap/soledad/auth.py | 62 |
1 files changed, 37 insertions, 25 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 <base64 encoded creds')] + + @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 + + @return: The Authorization header. + @rtype: list of tuple + """ + 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) |