summaryrefslogtreecommitdiff
path: root/service/pixelated/extensions/keymanager_fetch_key.py
blob: d39d1f964c4bdeef9ca382c7675f0480473f12b3 (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
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
import leap.keymanager
import requests
import logging
from leap.keymanager.errors import KeyNotFound
from leap.keymanager.openpgp import OpenPGPKey


logger = logging.getLogger(__name__)


def patched_fetch_keys_from_server(self, address):
    """
    Fetch keys bound to C{address} from nickserver and insert them in
    local database.

    Instead of raising a KeyNotFound only for 404 responses, this implementation
    raises a KeyNotFound exception for all problems.

    For original see: https://github.com/leapcode/keymanager/blob/develop/src/leap/keymanager/__init__.py

    :param address: The address bound to the keys.
    :type address: str

    :raise KeyNotFound: If the key was not found on nickserver.
    """
    # request keys from the nickserver
    res = None
    try:
        res = self._get(self._nickserver_uri, {'address': address})
        res.raise_for_status()
        server_keys = res.json()
        # insert keys in local database
        if self.OPENPGP_KEY in server_keys:
            self._wrapper_map[OpenPGPKey].put_ascii_key(
                server_keys['openpgp'])
    except requests.exceptions.HTTPError as e:
        logger.warning("HTTP error retrieving key: %r" % (e,))
        logger.warning("%s" % (res.content,))
        raise KeyNotFound(address)
    except Exception as e:
        logger.warning("Error retrieving key: %r" % (e,))
        raise KeyNotFound(address)


leap.keymanager.KeyManager._fetch_keys_from_server = patched_fetch_keys_from_server