summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/bonafide/_protocol.py
blob: ceb29efdc7e213cc72a398ac0a18f914998320f0 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
# _protocol.py
# Copyright (C) 2014-2015 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://www.gnu.org/licenses/>.
"""
Bonafide protocol.
"""
import os.path
from collections import defaultdict

from leap.bitmask.bonafide import config
from leap.bitmask.bonafide.provider import Api
from leap.bitmask.bonafide.session import Session, OK
from leap.common.config import get_path_prefix

from twisted.cred.credentials import UsernamePassword, Anonymous
from twisted.cred.checkers import ANONYMOUS
from twisted.internet.defer import fail
from twisted.logger import Logger


COMMANDS = 'signup', 'authenticate', 'logout', 'stats'
_preffix = os.path.join(get_path_prefix(), 'leap')


class BonafideProtocol(object):
    """
    Expose the protocol that interacts with the Bonafide Service API.
    """

    _apis = defaultdict(None)
    _sessions = defaultdict(None)

    log = Logger()

    def _get_api(self, provider):
        if provider.domain in self._apis:
            return self._apis[provider.domain]

        api = Api(provider.api_uri, provider.version)
        self._apis[provider.domain] = api
        return api

    def _get_or_create_session(self, provider, full_id, password=""):
        if full_id in self._sessions:
            return self._sessions[full_id]
        if full_id == ANONYMOUS:
            credentials = Anonymous()
            provider_id = provider.domain
        else:
            username, provider_id = config.get_username_and_provider(
                full_id)
            credentials = UsernamePassword(username, password)
        api = self._get_api(provider)
        provider_pem = config.get_ca_cert_path(_preffix, provider_id)
        session = Session(credentials, api, provider_pem)
        self._sessions[full_id] = session
        return session

    def _del_session_errback(self, failure, full_id):
        if full_id in self._sessions:
            del self._sessions[full_id]
        return failure

    # Service public methods

    def do_signup(self, full_id, password, invite=None, autoconf=False):
        self.log.debug('SIGNUP for %s' % full_id)
        _, provider_id = config.get_username_and_provider(full_id)

        provider = config.Provider.get(provider_id, autoconf=autoconf)
        d = provider.callWhenReady(
            self._do_signup, provider, full_id, password, invite)
        return d

    def _do_signup(self, provider, full_id, password, invite):

        # XXX check it's unauthenticated
        def return_user(result):
            return_code, user = result
            if return_code == OK:
                return user

        username, _ = config.get_username_and_provider(full_id)
        session = self._get_or_create_session(provider, full_id, password)
        d = session.signup(username, password, invite)
        d.addCallback(return_user)
        d.addErrback(self._del_session_errback, full_id)
        return d

    def do_authenticate(self, full_id, password, autoconf=False):
        _, provider_id = config.get_username_and_provider(full_id)

        provider = config.Provider.get(provider_id, autoconf=autoconf)

        def maybe_finish_provider_bootstrap(result):
            session = self._get_or_create_session(provider, full_id, password)
            d = provider.download_services_config_with_auth(session)
            d.addCallback(lambda _: result)
            return d

        d = provider.callWhenReady(
            self._do_authenticate, provider, full_id, password)
        d.addCallback(maybe_finish_provider_bootstrap)
        return d

    def _do_authenticate(self, provider, full_id, password):

        def return_token_and_uuid(result, _session):
            if result == OK:
                # TODO -- turn this into JSON response
                return str(_session.token), str(_session.uuid)

        self.log.debug('AUTH for %s' % full_id)

        session = self._get_or_create_session(provider, full_id, password)
        d = session.authenticate()
        d.addCallback(return_token_and_uuid, session)
        d.addErrback(self._del_session_errback, full_id)
        return d

    def do_logout(self, full_id):
        # XXX use the AVATAR here
        self.log.debug('LOGOUT for %s' % full_id)
        if (full_id not in self._sessions or
                not self._sessions[full_id].is_authenticated):
            return fail(RuntimeError("There is no session for such user"))
        session = self._sessions[full_id]

        d = session.logout()
        d.addCallback(lambda _: self._sessions.pop(full_id))
        d.addCallback(lambda _: '%s logged out' % full_id)
        return d

    def do_list_users(self):
        users = []
        for user, session in self._sessions.items():
            users.append({'userid': user,
                          'authenticated': session.is_authenticated})
        return users

    def do_change_password(self, full_id, current_password, new_password):
        self.log.debug('Change password for %s' % full_id)
        if (full_id not in self._sessions or
                not self._sessions[full_id].is_authenticated):
            return fail(RuntimeError("There is no session for such user"))
        session = self._sessions[full_id]

        if current_password != session.password:
            return fail(RuntimeError("The current password is not valid"))

        return session.change_password(new_password)

    def do_get_provider(self, provider_id, autoconf=False):
        provider = config.Provider.get(provider_id, autoconf=autoconf)
        return provider.callWhenMainConfigReady(provider.config)

    def do_get_service(self, provider_id, service, autoconf=False):
        provider = config.Provider.get(provider_id, autoconf=autoconf)
        return provider.callWhenMainConfigReady(provider.config, service)

    def do_provider_delete(self, provider_id):
        return config.delete_provider(provider_id)

    def do_provider_list(self, seeded=False):
        providers = config.list_providers()
        return [{"domain": p} for p in providers]

    def do_get_smtp_cert(self, full_id):
        if (full_id not in self._sessions or
                not self._sessions[full_id].is_authenticated):
            return fail(RuntimeError("There is no session for such user"))
        d = self._sessions[full_id].get_smtp_cert()
        return d

    def do_get_vpn_cert(self, full_id, anonymous=False):
        if anonymous:
            _, provider_id = full_id.split('@')
            provider = config.Provider.get(provider_id, autoconf=True)
            d = self._get_or_create_session(
                provider, ANONYMOUS).get_vpn_cert()
        else:
            if (full_id not in self._sessions or
                    not self._sessions[full_id].is_authenticated):
                return fail(RuntimeError("There is no session for such user"))
            d = self._sessions[full_id].get_vpn_cert()
        return d

    def do_update_user(self):
        # FIXME to be implemented
        pass