6b9ebf28a4b1fd57cbaa28558e805233ac152f02
[pixelated-user-agent.git] / service / pixelated / config / leap.py
1 #
2 # Copyright (c) 2015 ThoughtWorks, Inc.
3 #
4 # Pixelated is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # Pixelated is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
16
17 from __future__ import absolute_import
18
19 from leap.common.events import (server as events_server)
20 from pixelated.adapter.welcome_mail import add_welcome_mail
21 from pixelated.authentication import Authenticator
22 from pixelated.bitmask_libraries.certs import LeapCertificate
23 from pixelated.bitmask_libraries.provider import LeapProvider
24 from pixelated.config import credentials
25 from pixelated.config import leap_config
26 from pixelated.config.sessions import LeapSessionFactory
27 from twisted.internet import defer
28 from twisted.logger import Logger
29
30 log = Logger()
31
32
33 def initialize_leap_provider(provider_hostname, provider_cert, provider_fingerprint, leap_home):
34     LeapCertificate.set_cert_and_fingerprint(provider_cert,
35                                              provider_fingerprint)
36     leap_config.set_leap_home(leap_home)
37     provider = LeapProvider(provider_hostname)
38     provider.setup_ca()
39     provider.download_settings()
40     return provider
41
42
43 @defer.inlineCallbacks
44 def initialize_leap_multi_user(provider_hostname,
45                                leap_provider_cert,
46                                leap_provider_cert_fingerprint,
47                                credentials_file,
48                                leap_home):
49
50     config, provider = initialize_leap_provider(provider_hostname, leap_provider_cert, leap_provider_cert_fingerprint, leap_home)
51
52     defer.returnValue((config, provider))
53
54
55 @defer.inlineCallbacks
56 def create_leap_session(provider, username, password, auth=None):
57     leap_session = yield LeapSessionFactory(provider).create(username, password, auth)
58     defer.returnValue(leap_session)
59
60
61 @defer.inlineCallbacks
62 def initialize_leap_single_user(leap_provider_cert,
63                                 leap_provider_cert_fingerprint,
64                                 credentials_file,
65                                 leap_home):
66
67     init_monkeypatches()
68     events_server.ensure_server()
69
70     provider, username, password = credentials.read(credentials_file)
71
72     provider = initialize_leap_provider(provider, leap_provider_cert, leap_provider_cert_fingerprint, leap_home)
73
74     auth = yield Authenticator(provider).authenticate(username, password)
75
76     leap_session = yield create_leap_session(provider, username, password, auth)
77
78     defer.returnValue(leap_session)
79
80
81 def init_monkeypatches():
82     import pixelated.extensions.requests_urllib3
83
84
85 class BootstrapUserServices(object):
86
87     def __init__(self, services_factory, provider):
88         self._services_factory = services_factory
89         self._provider = provider
90
91     @defer.inlineCallbacks
92     def setup(self, user_auth, password, language='pt-BR'):
93         leap_session = yield create_leap_session(self._provider, user_auth.username, password, user_auth)
94         yield self._setup_user_services(leap_session)
95         yield self._add_welcome_email(leap_session, language)
96
97     @defer.inlineCallbacks
98     def _setup_user_services(self, leap_session):
99         user_id = leap_session.user_auth.uuid
100         if not self._services_factory.has_session(user_id):
101             yield self._services_factory.create_services_from(leap_session)
102             self._services_factory.map_email(leap_session.user_auth.username, user_id)
103
104     @defer.inlineCallbacks
105     def _add_welcome_email(self, leap_session, language):
106         if leap_session.fresh_account:
107             yield add_welcome_mail(leap_session.mail_store, language)