~mvo/ubuntu-sso-client/strawman-lp711413

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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# -*- coding: utf-8 -*-
# Author: Manuel de la Pena <manuel@canonical.com>
#
# Copyright 2011 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
"""Qt implementation of the UI."""

from PyQt4.QtCore import pyqtSignal
from PyQt4.QtGui import (
    QApplication,
    QColor,
    QCursor,
    QHBoxLayout,
    QPalette,
    QPixmap,
    QPushButton,
    QStyle,
    QWizard,
    QWizardPage)

from ubuntu_sso.logger import setup_logging
# pylint: disable=F0401,E0611
from ubuntu_sso.qt.choose_sign_in_ui import Ui_ChooseSignInPage
from ubuntu_sso.qt.current_user_sign_in_ui import Ui_CurrentUserSignInPage
from ubuntu_sso.qt.email_verification_ui import Ui_EmailVerificationPage
from ubuntu_sso.qt.error_message_ui import Ui_ErrorPage
from ubuntu_sso.qt.setup_account_ui import Ui_SetUpAccountPage
from ubuntu_sso.qt.terms_and_conditions_ui import Ui_TosPage
from ubuntu_sso.qt.success_message_ui import Ui_SuccessPage
from ubuntu_sso.qt.forgotten_password_ui import Ui_ForgottenPasswordPage
from ubuntu_sso.qt.reset_password_ui import Ui_ResetPasswordPage
# pylint: enable=F0401,E0611
from ubuntu_sso.qt.controllers import (
    ChooseSignInController,
    CurrentUserController,
    EmailVerificationController,
    ErrorController,
    ForgottenPasswordController,
    ResetPasswordController,
    SetUpAccountController,
    SuccessController,
    TosController,
    UbuntuSSOWizardController)


logger = setup_logging('ubuntu_sso.gui')

# colors used to define the password stenght
WEAK_COLOR = QColor(220, 20, 60)
MEDIUM_COLOR = QColor(255, 215, 0)
STRONG_COLOR = QColor(50, 205, 50)


class SSOWizardPage(QWizardPage):
    """Root class for all wizard pages."""

    def __init__(self, ui, controller, parent=None):
        """Create a new instance."""
        QWizardPage.__init__(self, parent)
        self.ui = ui
        self.ui.setupUi(self)
        self.controller = controller
        self.controller.setupUi(self)
        self.next = -1

    # pylint: disable=C0103
    def nextId(self):
        """Provide the next id."""
        return self.next
    # pylint: enable=C0103


class EnhancedLineEdit(object):
    """Represents and enhanced lineedit.

    This class works on an already added lineedit to the widget so
    that we are just adding extra items to it.
    """

    def __init__(self, line_edit, valid_cb=lambda x: False):
        """Create an instance."""
        self._line_edit = line_edit
        layout = QHBoxLayout(self._line_edit)
        layout.setMargin(0)
        self._line_edit.setLayout(layout)
        self.valid_cb = valid_cb
        layout.addStretch()
        self.clear_button = QPushButton(self._line_edit)
        layout.addWidget(self.clear_button)
        self.clear_button.setMinimumSize(16, 16)
        self.clear_button.setVisible(False)
        self.clear_button.setFlat(True)
        self.clear_button.setCursor(QCursor(0))
        self.clear_button.setIcon(QApplication.style().standardIcon(
                                                QStyle.SP_MessageBoxWarning))
        # connect the change of text to the cation that will check if the
        # text is valid and if the icon should be shown.
        self._line_edit.textChanged.connect(self.show_button)

    def show_button(self, string):
        """Decide if we show the button or not."""
        if not self.valid_cb(string):
            self.clear_button.setVisible(True)
        else:
            self.clear_button.setVisible(False)


class SSOWizardEnhancedEditPage(SSOWizardPage):
    """Page that contains enhanced line edits."""

    def __init__(self, ui, controller, parent=None):
        """Create a new instance."""
        SSOWizardPage.__init__(self, ui, controller, parent)
        self._enhanced_edits = {}

    def set_line_edit_validation_rule(self, edit, cb):
        """Set a new enhanced edit so that we can show an icon."""
        if edit in self._enhanced_edits:
            self._enhanced_edits[edit].valid_cb = cb
        else:
            # create a new enhanced edit
            enhanced_edit = EnhancedLineEdit(edit, cb)
            self._enhanced_edits[edit] = enhanced_edit


class ChooseSignInPage(SSOWizardPage):
    """Widget that allows the user to choose how to sign in."""

    def __init__(self, ui, controller, parent=None):
        """Create a new widget to be used."""
        SSOWizardPage.__init__(self, ui, controller, parent)

    # allow to access to the different useful children
    @property
    def existing_account_button(self):
        """Return the button used to sign in using an existing account."""
        return self.ui.existing_account_button

    @property
    def new_account_button(self):
        """Return the button used to sign in with a new account."""
        return self.ui.setup_account_button


class CurrentUserSignInPage(SSOWizardPage):
    """Widget that allows to get the data of user to sign in."""

    def __init__(self, ui, controller, parent=None):
        """Create a new widget to be used."""
        SSOWizardPage.__init__(self, ui, controller, parent)

    # allow to access to the different useful data
    @property
    def email(self):
        """Return the email data in the edit field."""
        return str(self.ui.email_edit.text())

    @property
    def email_edit(self):
        """Return the email edit that is used in the view."""
        return self.ui.email_edit

    @property
    def password(self):
        """Return the password data in the edit field."""
        return str(self.ui.password_edit.text())

    @property
    def password_edit(self):
        """Return the password edit used in the view."""
        return self.ui.password_edit

    @property
    def forgot_password_label(self):
        """Return the forgot password label."""
        return self.ui.forgot_label

    @property
    def sign_in_button(self):
        """Return the sign in button."""
        return self.ui.sign_in_button


class EmailVerificationPage(SSOWizardPage):
    """Widget used to input the email verification code."""

    def __init__(self, ui, controller, parent=None):
        """Create a new widget to be used."""
        SSOWizardPage.__init__(self, ui, controller, parent)

    @property
    def verification_code(self):
        """Return the content of the verification code edit."""
        return str(self.ui.verification_code_edit.text())

    @property
    def verification_code_edit(self):
        """Return the edit used for the verification code."""
        return self.ui.verification_code_edit

    @property
    def next_button(self):
        """Return the button that move to the next stage."""
        return self.ui.next_button


class ErrorPage(SSOWizardPage):
    """Widget used to show the diff errors."""

    def __init__(self, ui, controller, parent=None):
        """Create a new widget to be used."""
        SSOWizardPage.__init__(self, ui, controller, parent)
        self.ui = ui
        self.ui.setupUi(self)
        self.controller = controller
        self.controller.setupUi(self)
        self.next = -1

    # pylint: disable=C0103
    def nextId(self):
        """Return the next page id."""
        return self.next
    # pylint: enable=C0103

    @property
    def error_message_label(self):
        """Return the label used to show the error."""
        return self.ui.error_message_label


class ForgottenPasswordPage(SSOWizardEnhancedEditPage):
    """Widget used to deal with users that forgot the password."""

    def __init__(self, ui, controller, parent=None):
        """Create a new instance."""
        SSOWizardEnhancedEditPage.__init__(self, ui, controller, parent)

    @property
    def email_widget(self):
        """Return the widget used to show the email information."""
        return self.ui.email_widget

    @property
    def forgotted_password_intro_label(self):
        """Return the intro label that lets the user know the issue."""
        return self.ui.forgotted_password_intro_label

    @property
    def error_label(self):
        """Return the label used to show error."""
        return self.ui.error_label

    @property
    def email_address_label(self):
        """Return the lable used to state the use of the line edit."""
        return self.ui.email_address_label

    @property
    def email_address(self):
        """Return the email address provided by the user."""
        return str(self.ui.email_line_edit.text())

    @property
    def email_address_line_edit(self):
        """Return the line edit with the content."""
        return self.ui.email_line_edit

    @property
    def send_button(self):
        """Return the button used to request the new password."""
        return self.ui.send_button

    @property
    def try_again_widget(self):
        """Return the widget used to display the try again button."""
        return self.ui.try_again_widget

    @property
    def try_again_button(self):
        """Return the button used to try again the reset password."""
        return self.ui.try_again_button


class ResetPasswordPage(SSOWizardEnhancedEditPage):
    """Widget used to allow the user change his password."""

    def __init__(self, ui, controller, parent=None):
        """Create a new instance."""
        SSOWizardEnhancedEditPage.__init__(self, ui, controller, parent)

    @property
    def reset_code_line_edit(self):
        """Return the  line edit where the reset code is given."""
        return self.ui.reset_code_line_edit

    @property
    def reset_code(self):
        """Return the reset code typed by the user."""
        return str(self.ui.reset_code_line_edit.text())

    @property
    def password_line_edit(self):
        """Return the line edit where the password is typed."""
        return self.ui.password_line_edit

    @property
    def password(self):
        """Return the passsword typed by the user."""
        return str(self.ui.password_line_edit.text())

    @property
    def confirm_password_line_edit(self):
        """Return the line edit used to confirm the password."""
        return self.ui.confirm_password_line_edit

    @property
    def retyped_password(self):
        """Return the password confirmation given by the user."""
        return str(self.ui.confirm_password_line_edit.text())

    @property
    def reset_password_button(self):
        """Return the button used to reset the password."""
        return self.ui.reset_password_button


class TosPage(SSOWizardPage):
    """Widget used to show the tos."""

    def __init__(self, ui, controller, parent=None):
        """Create a new instance."""
        SSOWizardPage.__init__(self, ui, controller, parent)

    @property
    def webkit(self):
        """Return the webkit widget used to load the terms."""
        return self.ui.terms_webkit


class SetupAccountPage(SSOWizardEnhancedEditPage):
    """Widget used to create a new account."""

    def __init__(self, ui, controller, parent=None):
        """Create a new widget to be used."""
        SSOWizardEnhancedEditPage.__init__(self, ui, controller, parent)
        self._enhanced_edits = {}
        self.ui = ui
        self.ui.setupUi(self)
        # palettes that will be used to set the colors of the password strength
        original_palette = self.ui.strength_frame.palette()
        self._original_color = original_palette.background().color()
        self.captcha_id = None
        self.captcha_file = None
        self.ui.captcha_view.setPixmap(QPixmap())
        self.controller = controller
        self.controller.setupUi(self)

    def set_strength_level(self, level, password):
        """Set the strength level colors."""
        if password != '':
            if level <= 1:
                # low password
                self._set_frame_color(self.ui.weak_frame, WEAK_COLOR)
                self._set_frame_color(self.ui.medium_frame,
                                      self._original_color)
                self._set_frame_color(self.ui.strong_frame,
                                      self._original_color)
            elif level <= 3:
                # medium password
                self._set_frame_color(self.ui.weak_frame, MEDIUM_COLOR)
                self._set_frame_color(self.ui.medium_frame, MEDIUM_COLOR)
                self._set_frame_color(self.ui.strong_frame,
                                      self._original_color)
            else:
                # nice!
                self._set_frame_color(self.ui.weak_frame, STRONG_COLOR)
                self._set_frame_color(self.ui.medium_frame, STRONG_COLOR)
                self._set_frame_color(self.ui.strong_frame, STRONG_COLOR)
        else:
            # set it to the minimum
            self._set_frame_color(self.ui.weak_frame, self._original_color)
            self._set_frame_color(self.ui.medium_frame, self._original_color)
            self._set_frame_color(self.ui.strong_frame, self._original_color)

    def _set_frame_color(self, frame, color):
        """Set the color of a frame to indicated a strength."""
        # change the color background for the given frame
        palette = frame.palette()
        palette.setColor(QPalette.Background, color)
        frame.setPalette(palette)
        frame.setAutoFillBackground(True)

    @property
    def name(self):
        """Return the name input."""
        return str(self.ui.first_name_edit.text())

    @property
    def name_edit(self):
        """Return the edit used for the name."""
        return self.ui.first_name_edit

    @property
    def last_name(self):
        """Return the last name input."""
        return str(self.ui.last_name_edit.text())

    @property
    def last_name_edit(self):
        """Return the edit used for the last name."""
        return self.ui.last_name_edit

    @property
    def email(self):
        """Return the email input."""
        return str(self.ui.email_edit.text())

    @property
    def email_edit(self):
        """Return the edit used for the email."""
        return self.ui.email_edit

    @property
    def retyped_email(self):
        """Return the retyped email."""
        return str(self.ui.confirm_email_edit.text())

    @property
    def confirm_email_edit(self):
        """Return the edit used for the retype of the email."""
        return self.ui.confirm_email_edit

    @property
    def password_info_label(self):
        """Return the password used to show the info of the label."""
        return self.ui.password_info_label

    @property
    def password(self):
        """Return the password data."""
        return str(self.ui.password_edit.text())

    @property
    def password_edit(self):
        """Return the edit used for the password."""
        return self.ui.password_edit

    @property
    def retyped_password(self):
        """Return the retyped password."""
        return str(self.ui.confirm_password_edit.text())

    @property
    def confirm_password_edit(self):
        """Return the edit used to confirm the password."""
        return self.ui.confirm_password_edit

    @property
    def captcha_refresh_label(self):
        """Return the refresh label."""
        return self.ui.refresh_label

    @property
    def captcha_solution(self):
        """Return the provided captcha solution."""
        return str(self.ui.captcha_solution_edit.text())

    @property
    def captcha_solution_edit(self):
        """Return the edit used for the captcha solution."""
        return self.ui.captcha_solution_edit

    def get_captcha_image(self):
        """Return the path to the captcha image."""
        return self.ui.captcha_view.pixmap()

    def set_captcha_image(self, pixmap_image):
        """Set the new image of the captcha."""
        # lets set the QPixmap for the label
        self.ui.captcha_view.setPixmap(pixmap_image)

    captcha_image = property(get_captcha_image, set_captcha_image)

    @property
    def terms_and_conditions_agreed(self):
        """Return if the user agreed the terms."""
        return self.ui.terms_checkbox.isChecked()

    @property
    def terms_and_conditions_check_box(self):
        """Return the terms and codition item."""
        return self.ui.terms_checkbox

    @property
    def terms_and_conditions_button(self):
        """Return the terms and conditions button."""
        return self.ui.terms_button

    @property
    def set_up_button(self):
        """Return the set up button."""
        return self.ui.set_up_button


class SuccessPage(SSOWizardPage):
    """Page used to display success message."""

    def __init__(self, ui, controller, parent=None):
        """Create a new instance."""
        SSOWizardPage.__init__(self, ui, controller, parent)
        self.ui = ui
        self.ui.setupUi(self)
        self.controller = controller
        self.controller.setupUi(self)
        self.next = -1

    # pylint: disable=C0103
    def nextId(self):
        """Return the next page id."""
        return self.next
    # pylint: enable=C0103

    @property
    def success_message_label(self):
        """Return the label used to show the message."""
        return self.ui.success_message_label


class UbuntuSSOWizard(QWizard):
    """Wizard used to create or use sso."""

    # definition of the signals raised by the widget
    recoverableError = pyqtSignal('QString', 'QString')
    loginSuccess = pyqtSignal('QString', 'QString')
    registrationSuccess = pyqtSignal('QString', 'QString')

    def __init__(self, controller, parent=None, app_name='', tos_url='',
                 help_text=''):
        """Create a new wizard."""
        QWizard.__init__(self, parent)
        # store common useful data provided by the app
        self.app_name = app_name
        self.tos_url = tos_url
        self.help_text = help_text

        # set the diff pages of the QWizard
        self.sign_in_controller = ChooseSignInController(title='Sign In')
        self.sign_in_page = ChooseSignInPage(Ui_ChooseSignInPage(),
                                             self.sign_in_controller,
                                             parent=self)
        self.setup_controller = SetUpAccountController()
        self.setup_account = SetupAccountPage(Ui_SetUpAccountPage(),
                                              self.setup_controller,
                                              parent=self)
        self.tos = TosPage(Ui_TosPage(), TosController(tos_url=tos_url),
                           parent=self)
        self.email_verification = EmailVerificationPage(
                                                Ui_EmailVerificationPage(),
                                                EmailVerificationController())
        self.current_user_controller = CurrentUserController(title='Sign in')
        self.current_user = CurrentUserSignInPage(Ui_CurrentUserSignInPage(),
                                                  self.current_user_controller,
                                                  parent=self)
        self.success_controller = SuccessController()
        self.success = SuccessPage(Ui_SuccessPage(), self.success_controller,
                                   parent=self)
        self.error_controller = ErrorController()
        self.error = ErrorPage(Ui_ErrorPage(), self.error_controller)
        self.forgotte_pwd_controller = ForgottenPasswordController()
        self.forgotten = ForgottenPasswordPage(Ui_ForgottenPasswordPage(),
                                               self.forgotte_pwd_controller,
                                               parent=self)
        self.reset_password_controller = ResetPasswordController()
        self.reset_password = ResetPasswordPage(Ui_ResetPasswordPage(),
                                                self.reset_password_controller,
                                                parent=self)
        # store the ids of the pages so that it is easier to access them later
        self._pages = {}
        for page in [self.sign_in_page, self.setup_account, self.tos,
                     self.email_verification, self.current_user, self.success,
                     self.error, self.forgotten, self.reset_password]:
            self._pages[page] = self.addPage(page)

        # set the buttons layout to only have cancel and back since the next
        # buttons are the ones used in the diff pages.
        buttons_layout = []
        buttons_layout.append(QWizard.Stretch)
        buttons_layout.append(QWizard.BackButton)
        buttons_layout.append(QWizard.CancelButton)
        self.setButtonLayout(buttons_layout)
        self.setWindowTitle(app_name)
        self.controller = controller
        self.controller.setupUi(self)

    @property
    def sign_in_page_id(self):
        """Return the id of the page used for choosing sign in type."""
        return self._pages[self.sign_in_page]

    @property
    def setup_account_page_id(self):
        """Return the id of the page used for sign in."""
        return self._pages[self.setup_account]

    @property
    def tos_page_id(self):
        """Return the id of the tos page."""
        return self._pages[self.tos]

    @property
    def email_verification_page_id(self):
        """Return the id of the verification page."""
        return self._pages[self.email_verification]

    @property
    def current_user_page_id(self):
        """Return the id used to signin by a current user."""
        return self._pages[self.current_user]

    @property
    def success_page_id(self):
        """Return the id of the success page."""
        return self._pages[self.success]

    @property
    def forgotten_password_page_id(self):
        """Return the id of the forgotten password page."""
        return self._pages[self.forgotten]

    @property
    def reset_password_page_id(self):
        """Return the id of the reset password page."""
        return self._pages[self.reset_password]

    @property
    def error_page_id(self):
        """Return the id of the error page."""
        return self._pages[self.error]


class UbuntuSSOClientGUI(object):
    """Ubuntu single sign-on GUI."""

    def __init__(self, app_name, tc_url='http://one.ubuntu.com', help_text='',
                 window_id=0, login_only=False):
        """Create a new instance."""
        # create the controller and the ui, then set the cb and call the show
        # method so that we can work
        self.controller = UbuntuSSOWizardController(app_name)
        self.view = UbuntuSSOWizard(self.controller, app_name=app_name,
                                    tos_url=tc_url, help_text=help_text)
        self.view.show()

    def get_login_success_callback(self):
        """Return the log in success cb."""
        return self.controller.login_success_callback

    def set_login_success_callback(self, cb):
        """Set log in success cb."""
        self.controller.login_success_callback = cb

    login_success_callback = property(get_login_success_callback,
                                      set_login_success_callback)

    def get_registration_success_callback(self):
        """Return the registration success cb."""
        return self.controller.registration_success_callback

    def set_registration_success_callback(self, cb):
        """Set registration success cb."""
        self.controller.registration_success_callback = cb

    registration_success_callback = property(get_registration_success_callback,
                                             set_registration_success_callback)

    def get_user_cancellation_callback(self):
        """Return the user cancellation callback."""
        return self.controller.user_cancellation_callback

    def set_user_cancellation_callback(self, cb):
        """Set the user cancellation callback."""
        self.controller.user_cancellation_callback = cb

    user_cancellation_callback = property(get_user_cancellation_callback,
                                          set_user_cancellation_callback)