summaryrefslogtreecommitdiff
path: root/service/pixelated/bitmask_libraries/nicknym.py
blob: ef846bba51776f6eec4189b6e7480c04f6f4ca54 (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
#
# 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/>.
from leap.keymanager import KeyManager, openpgp, KeyNotFound
from .certs import which_api_CA_bundle


class NickNym(object):
    def __init__(self, provider, config, soledad_session, srp_session):
        nicknym_url = _discover_nicknym_server(provider)
        self._email = '%s@%s' % (srp_session.user_name, provider.domain)
        self.keymanager = KeyManager('%s@%s' % (srp_session.user_name, provider.domain), nicknym_url,
                                     soledad_session.soledad,
                                     srp_session.token, which_api_CA_bundle(provider), provider.api_uri,
                                     provider.api_version,
                                     srp_session.uuid, config.gpg_binary)

    def generate_openpgp_key(self):
        if not self._key_exists(self._email):
            print "Generating keys - this could take a while..."
            self._gen_key()
            self._send_key_to_leap()

    def _key_exists(self, email):
        try:
            self.keymanager.get_key(email, openpgp.OpenPGPKey, private=True, fetch_remote=False)
            return True
        except KeyNotFound:
            return False

    def _gen_key(self):
        self.keymanager.gen_key(openpgp.OpenPGPKey)

    def _send_key_to_leap(self):
        self.keymanager.send_key(openpgp.OpenPGPKey)


def _discover_nicknym_server(provider):
    return 'https://nicknym.%s:6425/' % provider.domain