summaryrefslogtreecommitdiff
path: root/src/leap/gui/wizard.py
blob: 7dcc8dd61f280120c1303b48107000639e540b7c (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# -*- coding: utf-8 -*-
# wizard.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://www.gnu.org/licenses/>.

"""
First run wizard
"""
import os
import logging

from PySide import QtCore, QtGui

from ui_wizard import Ui_Wizard
from leap.config.providerconfig import ProviderConfig
from leap.crypto.srpregister import SRPRegister
from leap.services.eip.providerbootstrapper import ProviderBootstrapper
from leap.services.eip.eipbootstrapper import EIPBootstrapper

logger = logging.getLogger(__name__)


class Wizard(QtGui.QWizard):
    """
    First run wizard to register a user and setup a provider
    """

    INTRO_PAGE = 0
    SELECT_PROVIDER_PAGE = 1
    PRESENT_PROVIDER_PAGE = 2
    SETUP_PROVIDER_PAGE = 3
    REGISTER_USER_PAGE = 4
    SETUP_EIP_PAGE = 5
    FINISH_PATH = 6

    WEAK_PASSWORDS = ("1234", "12345", "123456",
                      "password")

    def __init__(self):
        QtGui.QWizard.__init__(self)

        self.ui = Ui_Wizard()
        self.ui.setupUi(self)

        self.QUESTION_ICON = QtGui.QPixmap(":/images/Emblem-question.png")
        self.ERROR_ICON = QtGui.QPixmap(":/images/Dialog-error.png")
        self.OK_ICON = QtGui.QPixmap(":/images/Dialog-accept.png")

        self._show_register = False

        self.ui.grpCheckProvider.setVisible(False)
        self.ui.btnCheck.clicked.connect(self._check_provider)
        self.ui.lnProvider.returnPressed.connect(self._check_provider)

        self._provider_bootstrapper = ProviderBootstrapper()
        self._provider_bootstrapper.name_resolution.connect(
            self._name_resolution)
        self._provider_bootstrapper.https_connection.connect(
            self._https_connection)
        self._provider_bootstrapper.download_provider_info.connect(
            self._download_provider_info)

        self._provider_bootstrapper.download_ca_cert.connect(
            self._download_ca_cert)
        self._provider_bootstrapper.check_ca_fingerprint.connect(
            self._check_ca_fingerprint)
        self._provider_bootstrapper.check_api_certificate.connect(
            self._check_api_certificate)

        self._eip_bootstrapper = EIPBootstrapper()

        self._eip_bootstrapper.download_config.connect(
            self._download_eip_config)
        self._eip_bootstrapper.download_client_certificate.connect(
            self._download_client_certificate)

        self._domain = None
        self._provider_config = ProviderConfig()

        self.currentIdChanged.connect(self._current_id_changed)

        self.ui.lblPassword.setEchoMode(QtGui.QLineEdit.Password)
        self.ui.lblPassword2.setEchoMode(QtGui.QLineEdit.Password)

        self.ui.lblUser.returnPressed.connect(
            self._focus_password)
        self.ui.lblPassword.returnPressed.connect(
            self._focus_second_password)
        self.ui.lblPassword2.returnPressed.connect(
            self._register)
        self.ui.btnRegister.clicked.connect(
            self._register)

        self._username = None

    def __del__(self):
        self._provider_bootstrapper.set_should_quit()
        self._eip_bootstrapper.set_should_quit()
        self._provider_bootstrapper.wait()
        self._eip_bootstrapper.wait()

    def get_username(self):
        return self._username

    def _focus_password(self):
        """
        Focuses at the password lineedit for the registration page
        """
        self.ui.lblPassword.setFocus()

    def _focus_second_password(self):
        """
        Focuses at the second password lineedit for the registration page
        """
        self.ui.lblPassword2.setFocus()

    def _basic_password_checks(self, username, password, password2):
        """
        Performs basic password checks to avoid really easy passwords.

        @param username: username provided at the registrarion form
        @type username: str
        @param password: password from the registration form
        @type password: str
        @param password2: second password from the registration form
        @type password: str

        @return: returns True if all the checks pass, False otherwise
        @rtype: bool
        """
        message = None

        try:
            username.encode("ascii")
            password.encode("ascii")
        except:
            message = u"Refrain from using non ASCII áñ characters"

        if message is not None and password != password2:
            message = "Passwords don't match"

        if message is not None and len(password) < 4:
            message = "Password too short"

        if message is not None and password in self.WEAK_PASSWORDS:
            message = "Password too easy"

        if message is not None and username == password:
            message = "Password equal to username"

        if message is not None:
            self._set_register_status(message)
            self._focus_password()
            return False

        return True

    def _register(self):
        """
        Performs the registration based on the values provided in the form
        """
        self.ui.btnRegister.setEnabled(False)
        # See the disabled button
        while QtGui.QApplication.instance().hasPendingEvents():
            QtGui.QApplication.instance().processEvents()
        self.button(QtGui.QWizard.NextButton).setFocus()

        username = self.ui.lblUser.text()
        password = self.ui.lblPassword.text()
        password2 = self.ui.lblPassword2.text()

        if self._basic_password_checks(username, password, password2):
            register = SRPRegister(provider_config=self._provider_config)
            ok, req = register.register_user(username, password)
            if ok:
                self._set_register_status("<b>User registration OK</b>")
                self._username = username
                self.ui.lblPassword2.clearFocus()
                # Detach this call to allow UI updates briefly
                QtCore.QTimer.singleShot(1,
                                         self.page(self.REGISTER_USER_PAGE)
                                         .set_completed)
            else:
                print req.content
                error_msg = "Unknown error"
                try:
                    error_msg = req.json().get("errors").get("login")[0]
                except:
                    logger.error("Unknown error: %r" % (req.content,))
                self._set_register_status(error_msg)
                self.ui.btnRegister.setEnabled(True)
        else:
            self.ui.btnRegister.setEnabled(True)

    def _set_register_status(self, status):
        """
        Sets the status label in the registration page to status

        @param status: status message to display, can be HTML
        @type status: str
        """
        self.ui.lblRegisterStatus.setText(status)

    def _check_provider(self):
        """
        SLOT
        TRIGGERS:
          self.ui.btnCheck.clicked
          self.ui.lnProvider.returnPressed

        Starts the checks for a given provider
        """
        self.ui.grpCheckProvider.setVisible(True)
        self.ui.btnCheck.setEnabled(False)
        self._domain = self.ui.lnProvider.text()

        self._provider_bootstrapper.start()
        self._provider_bootstrapper.run_provider_select_checks(self._domain)

    def _complete_task(self, data, label, complete=False, complete_page=-1):
        """
        Checks a task and completes a page if specified

        @param data: data as it comes from the bootstrapper thread for
        a specific check
        @type data: dict
        @param label: label that displays the status icon for a
        specific check that corresponds to the data
        @type label: QtGui.QLabel
        @param complete: if True, it completes the page specified,
        which must be of type WizardPage
        @type complete: bool
        @param complete_page: page id to complete
        @type complete_page: int
        """
        passed = data[self._provider_bootstrapper.PASSED_KEY]
        error = data[self._provider_bootstrapper.ERROR_KEY]
        if passed:
            label.setPixmap(self.OK_ICON)
            if complete:
                self.page(complete_page).set_completed()
                self.button(QtGui.QWizard.NextButton).setFocus()
        else:
            label.setPixmap(self.ERROR_ICON)
            logger.error(error)

    def _name_resolution(self, data):
        """
        SLOT
        TRIGGER: self._provider_bootstrapper.name_resolution

        Sets the status for the name resolution check
        """
        self._complete_task(data, self.ui.lblNameResolution)

    def _https_connection(self, data):
        """
        SLOT
        TRIGGER: self._provider_bootstrapper.https_connection

        Sets the status for the https connection check
        """
        self._complete_task(data, self.ui.lblHTTPS)

    def _download_provider_info(self, data):
        """
        SLOT
        TRIGGER: self._provider_bootstrapper.download_provider_info

        Sets the status for the provider information download
        check. Since this check is the last of this set, it also
        completes the page if passed
        """
        if self._provider_config.load(os.path.join("leap",
                                                   "providers",
                                                   self._domain,
                                                   "provider.json")):
            self._complete_task(data, self.ui.lblProviderInfo,
                                True, self.SELECT_PROVIDER_PAGE)
        else:
            new_data = {
                self._provider_bootstrapper.PASSED_KEY: False,
                self._provider_bootstrapper.ERROR_KEY:
                "Unable to load provider configuration"
            }
            self._complete_task(new_data, self.ui.lblProviderInfo)

        self.ui.btnCheck.setEnabled(True)

    def _download_ca_cert(self, data):
        """
        SLOT
        TRIGGER: self._provider_bootstrapper.download_ca_cert

        Sets the status for the download of the CA certificate check
        """
        self._complete_task(data, self.ui.lblDownloadCaCert)

    def _check_ca_fingerprint(self, data):
        """
        SLOT
        TRIGGER: self._provider_bootstrapper.check_ca_fingerprint

        Sets the status for the CA fingerprint check
        """
        self._complete_task(data, self.ui.lblCheckCaFpr)

    def _check_api_certificate(self, data):
        """
        SLOT
        TRIGGER: self._provider_bootstrapper.check_api_certificate

        Sets the status for the API certificate check. Also finishes
        the provider bootstrapper thread since it's not needed anymore
        from this point on, unless the whole check chain is restarted
        """
        self._complete_task(data, self.ui.lblCheckApiCert,
                            True, self.SETUP_PROVIDER_PAGE)
        self._provider_bootstrapper.set_should_quit()

    def _download_eip_config(self, data):
        """
        SLOT
        TRIGGER: self._eip_bootstrapper.download_config

        Sets the status for the EIP config downloading check
        """
        self._complete_task(data, self.ui.lblDownloadEIPConfig)

    def _download_client_certificate(self, data):
        """
        SLOT
        TRIGGER: self._provider_bootstrapper.download_client_certificate

        Sets the status for the download client certificate check and
        completes the page if passed. Also stops the eip bootstrapper
        thread since it's not needed from this point on unless the
        check chain is restarted
        """
        self._complete_task(data, self.ui.lblDownloadClientCert,
                            True, self.SETUP_EIP_PAGE)
        self._eip_bootstrapper.set_should_quit()

    def _current_id_changed(self, pageId):
        """
        SLOT
        TRIGGER: self.currentIdChanged

        Prepares the pages when they appear
        """
        if pageId == self.SELECT_PROVIDER_PAGE:
            self.ui.grpCheckProvider.setVisible(False)
            self.ui.lblNameResolution.setPixmap(self.QUESTION_ICON)
            self.ui.lblHTTPS.setPixmap(self.QUESTION_ICON)
            self.ui.lblProviderInfo.setPixmap(self.QUESTION_ICON)

        if pageId == self.SETUP_PROVIDER_PAGE:
            self._provider_bootstrapper.\
                run_provider_setup_checks(self._provider_config)

        if pageId == self.SETUP_EIP_PAGE:
            self._eip_bootstrapper.start()
            self._eip_bootstrapper.run_eip_setup_checks(self._provider_config)

        if pageId == self.PRESENT_PROVIDER_PAGE:
            # TODO: get the right lang for these
            self.ui.lblProviderName.setText(
                "<b>%s</b>" %
                (self._provider_config.get_name(),))
            self.ui.lblProviderURL.setText(self._provider_config.get_domain())
            self.ui.lblProviderDesc.setText(
                "<i>%s</i>" %
                (self._provider_config.get_description(),))
            self.ui.lblProviderPolicy.setText(self._provider_config
                                              .get_enrollment_policy())

    def nextId(self):
        """
        Sets the next page id for the wizard based on wether the user
        wants to register a new identity or uses an existing one
        """
        if self.currentPage() == self.page(self.INTRO_PAGE):
            self._show_register = self.ui.rdoRegister.isChecked()

        if self.currentPage() == self.page(self.SETUP_PROVIDER_PAGE):
            if self._show_register:
                return self.REGISTER_USER_PAGE
            else:
                return self.SETUP_EIP_PAGE

        return QtGui.QWizard.nextId(self)