From 365c318872e433ee13eff29e37039c10b22ee685 Mon Sep 17 00:00:00 2001 From: drebs Date: Sat, 27 Apr 2013 00:35:22 -0300 Subject: Use 'requests' module in KeyManager. --- src/leap/common/keymanager/__init__.py | 38 ++++++++++------ src/leap/common/keymanager/http.py | 78 -------------------------------- src/leap/common/tests/test_keymanager.py | 3 +- 3 files changed, 25 insertions(+), 94 deletions(-) delete mode 100644 src/leap/common/keymanager/http.py diff --git a/src/leap/common/keymanager/__init__.py b/src/leap/common/keymanager/__init__.py index 82fa99b..8db3b3c 100644 --- a/src/leap/common/keymanager/__init__.py +++ b/src/leap/common/keymanager/__init__.py @@ -21,9 +21,7 @@ Key Manager is a Nicknym agent for LEAP client. """ import httplib - - -from u1db.errors import HTTPError +import requests from leap.common.check import leap_assert @@ -39,7 +37,6 @@ from leap.common.keymanager.openpgp import ( OpenPGPScheme, encrypt_sym, ) -from leap.common.keymanager.http import HTTPClient TAGS_INDEX = 'by-tags' @@ -52,7 +49,7 @@ INDEXES = { class KeyManager(object): - def __init__(self, address, url, soledad): + def __init__(self, address, nickserver_url, soledad): """ Initialize a Key Manager for user's C{address} with provider's nickserver reachable in C{url}. @@ -65,7 +62,7 @@ class KeyManager(object): @type soledad: leap.soledad.Soledad """ self._address = address - self._http_client = HTTPClient(url) + self._nickserver_url = nickserver_url self._soledad = soledad self._wrapper_map = { OpenPGPKey: OpenPGPScheme(soledad), @@ -105,6 +102,22 @@ class KeyManager(object): self._soledad.delete_index(name) self._soledad.create_index(name, *expression) + def _get_dict_from_http_json(self, path): + """ + Make a GET HTTP request and return a dictionary containing the + response. + """ + response = requests.get(self._nickserver_url+path) + leap_assert(r.status_code == 200, 'Invalid response.') + leap_assert( + response.headers['content-type'].startswith('application/json') + is True, + 'Content-type is not JSON.') + return r.json() + + # + # key management + # def send_key(self, ktype, send_private=False, password=None): """ @@ -143,12 +156,10 @@ class KeyManager(object): self.get_key(self._address, ktype, private=True).get_json()) privkey.key_data = encrypt_sym(data, passphrase) data['keys'].append(privkey) - headers = None # TODO: replace for token-based-auth - self._http_client.request( - 'PUT', - '/key/%s' % address, - json.dumps(data), - headers) + requests.put( + self._nickserver_url + '/key/' + address, + data=data, + auth=(self._address, None)) # TODO: replace for token-based auth. def get_key(self, address, ktype, private=False, fetch_remote=True): """ @@ -201,8 +212,7 @@ class KeyManager(object): @raise KeyNotFound: If the key was not found on nickserver. @raise httplib.HTTPException: """ - self._http_client.request('GET', '/key/%s' % address, None, None) - keydata = json.loads(self._http_client.read_response()) + keydata = self._get_dict_from_http_json('/key/%s' % address) leap_assert( keydata['address'] == address, "Fetched key for wrong address.") diff --git a/src/leap/common/keymanager/http.py b/src/leap/common/keymanager/http.py deleted file mode 100644 index 478137d..0000000 --- a/src/leap/common/keymanager/http.py +++ /dev/null @@ -1,78 +0,0 @@ -# -*- coding: utf-8 -*- -# http.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 utilities. -""" - - -import urlparse -import httplib - - -def HTTPClient(object): - """ - A simple HTTP client for making requests. - """ - - def __init__(self, url): - """ - Initialize the HTTP client. - """ - self._url = urlparse.urlsplit(url) - self._conn = None - - def _ensure_connection(self): - """ - Ensure the creation of the connection object. - """ - if self._conn is not None: - return - if self._url.scheme == 'https': - connClass = httplib.HTTPSConnection - else: - connClass = httplib.HTTPConnection - self._conn = connClass(self._url.hostname, self._url.port) - - def request(method, url_query, body, headers): - """ - Make an HTTP request. - - @param method: The method of the request. - @type method: str - @param url_query: The URL query string of the request. - @type url_query: str - @param body: The body of the request. - @type body: str - @param headers: Headers to be sent on the request. - @type headers: list of str - """ - self._ensure_connection() - return self._conn.request(mthod, url_query, body, headers) - - def response(self): - """ - Return the response of an HTTP request. - """ - return self._conn.getresponse() - - def read_response(self): - """ - Get the contents of a response for an HTTP request. - """ - return self.response().read() diff --git a/src/leap/common/tests/test_keymanager.py b/src/leap/common/tests/test_keymanager.py index 9bafb89..9670f9b 100644 --- a/src/leap/common/tests/test_keymanager.py +++ b/src/leap/common/tests/test_keymanager.py @@ -151,10 +151,9 @@ class KeyManagerWithSoledadTestCase(BaseLeapTest): pass - class OpenPGPCryptoTestCase(KeyManagerWithSoledadTestCase): - def _test_openpgp_gen_key(self): + def test_openpgp_gen_key(self): pgp = openpgp.OpenPGPScheme(self._soledad) self.assertRaises(KeyNotFound, pgp.get_key, 'user@leap.se') key = pgp.gen_key('user@leap.se') -- cgit v1.2.3