summaryrefslogtreecommitdiff
path: root/src/leap/gui/firstrun/providersetup.py
blob: 48a89091048db613681527ff87c0f0991a565e0e (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
"""
Provider Setup Validation Page,
used if First Run Wizard
"""
import logging

from PyQt4 import QtGui

from leap.base import exceptions as baseexceptions
from leap.gui.progress import ValidationPage

from leap.gui.constants import APP_LOGO

logger = logging.getLogger(__name__)


class ProviderSetupValidationPage(ValidationPage):
    def __init__(self, parent=None):
        super(ProviderSetupValidationPage, self).__init__(parent)
        self.current_page = "providersetupvalidation"

        # XXX needed anymore?
        #is_signup = self.field("is_signup")
        #self.is_signup = is_signup

        self.setTitle(self.tr("Provider setup"))
        self.setSubTitle(
            self.tr("Doing autoconfig."))

        self.setPixmap(
            QtGui.QWizard.LogoPixmap,
            QtGui.QPixmap(APP_LOGO))

    def _do_checks(self):
        """
        generator that yields actual checks
        that are executed in a separate thread
        """

        full_domain = self.field('provider_domain')
        wizard = self.wizard()
        pconfig = wizard.providerconfig

        #pCertChecker = wizard.providercertchecker
        #certchecker = pCertChecker(domain=full_domain)
        pCertChecker = wizard.providercertchecker(
            domain=full_domain)

        yield(("head_sentinel", 0), lambda: None)

        ########################
        # 1) fetch ca cert
        ########################

        def fetchcacert():
            if pconfig:
                ca_cert_uri = pconfig.get('ca_cert_uri').geturl()
            else:
                ca_cert_uri = None

            # XXX check scheme == "https"
            # XXX passing verify == False because
            # we have trusted right before.
            # We should check it's the same domain!!!
            # (Check with the trusted fingerprints dict
            # or something smart)
            try:
                pCertChecker.download_ca_cert(
                    uri=ca_cert_uri,
                    verify=False)

            except baseexceptions.LeapException as exc:
                logger.error(exc.message)
                # XXX this should be _ method
                return self.fail(self.tr(exc.usermessage))

            except Exception as exc:
                return self.fail(exc.message)

            else:
                return True

        yield((self.tr('Fetching CA certificate'), 30),
              fetchcacert)

        #########################
        # 2) check CA fingerprint
        #########################

        def checkcafingerprint():
            # XXX get the real thing!!!
            pass
        #ca_cert_fingerprint = pconfig.get('ca_cert_fingerprint', None)

        # XXX get fingerprint dict (types)
        #sha256_fpr = ca_cert_fingerprint.split('=')[1]

        #validate_fpr = pCertChecker.check_ca_cert_fingerprint(
            #fingerprint=sha256_fpr)
        #if not validate_fpr:
            # XXX update validationMsg
            # should catch exception
            #return False

        yield((self.tr("Checking CA fingerprint"), 60),
              checkcafingerprint)

        #########################
        # 2) check CA fingerprint
        #########################

        def validatecacert():
            pass
            #api_uri = pconfig.get('api_uri', None)
            #try:
                #api_cert_verified = pCertChecker.verify_api_https(api_uri)
            #except requests.exceptions.SSLError as exc:
                #logger.error('BUG #638. %s' % exc.message)
                # XXX RAISE! See #638
                # bypassing until the hostname is fixed.
                # We probably should raise yet-another-warning
                # here saying user that the hostname "XX.XX.XX.XX' does not
                # match 'foo.bar.baz'
                #api_cert_verified = True

            #if not api_cert_verified:
                # XXX update validationMsg
                # should catch exception
                #return False

            #???
            #ca_cert_path = checker.ca_cert_path

        yield((self.tr('Validating api certificate'), 90), validatecacert)

        self.set_done()
        yield(('end_sentinel', 100), lambda: None)

    def on_checks_validation_ready(self):
        """
        called after _do_checks has finished
        (connected to checker thread finished signal)
        """
        prevpage = "providerselection" if self.is_signup else "login"
        wizard = self.wizard()

        if self.errors:
            logger.debug('going back with errors')
            name, first_error = self.pop_first_error()
            wizard.set_validation_error(
                prevpage,
                first_error)
            # XXX don't go back, signal error
            #self.go_back()
        else:
            logger.debug('should be going next, wait on user')
            #self.go_next()

    def nextId(self):
        wizard = self.wizard()
        from_login = wizard.from_login
        if from_login:
            # XXX bad name. change to connect again.
            next_ = 'signupvalidation'
        else:
            next_ = 'signup'
        return wizard.get_page_index(next_)

    def initializePage(self):
        super(ProviderSetupValidationPage, self).initializePage()
        self.set_undone()
        self.completeChanged.emit()