|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to edit the SSL error exceptions. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, Qt, QPoint |
|
13 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QMenu |
|
14 from PyQt5.QtWebEngineWidgets import QWebEngineCertificateError |
|
15 |
|
16 from .Ui_SslErrorExceptionsDialog import Ui_SslErrorExceptionsDialog |
|
17 |
|
18 |
|
19 class SslErrorExceptionsDialog(QDialog, Ui_SslErrorExceptionsDialog): |
|
20 """ |
|
21 Class implementing a dialog to edit the SSL error exceptions. |
|
22 """ |
|
23 def __init__(self, errorsDict, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param errorsDict error exceptions |
|
28 @type dict of list of int |
|
29 @param parent reference to the parent widget |
|
30 @type QWidget |
|
31 """ |
|
32 super(SslErrorExceptionsDialog, self).__init__(parent) |
|
33 self.setupUi(self) |
|
34 |
|
35 self.__errorDescriptions = { |
|
36 QWebEngineCertificateError.SslPinnedKeyNotInCertificateChain: |
|
37 self.tr("The certificate did not match the built-in public" |
|
38 " keys pinned for the host name."), |
|
39 QWebEngineCertificateError.CertificateCommonNameInvalid: |
|
40 self.tr("The certificate's common name did not match the" |
|
41 " host name."), |
|
42 QWebEngineCertificateError.CertificateDateInvalid: |
|
43 self.tr("The certificate is not valid at the current date" |
|
44 " and time."), |
|
45 QWebEngineCertificateError.CertificateAuthorityInvalid: |
|
46 self.tr("The certificate is not signed by a trusted" |
|
47 " authority."), |
|
48 QWebEngineCertificateError.CertificateContainsErrors: |
|
49 self.tr("The certificate contains errors."), |
|
50 QWebEngineCertificateError.CertificateNoRevocationMechanism: |
|
51 self.tr("The certificate has no mechanism for determining if" |
|
52 " it has been revoked."), |
|
53 QWebEngineCertificateError.CertificateUnableToCheckRevocation: |
|
54 self.tr("Revocation information for the certificate is" |
|
55 " not available."), |
|
56 QWebEngineCertificateError.CertificateRevoked: |
|
57 self.tr("The certificate has been revoked."), |
|
58 QWebEngineCertificateError.CertificateInvalid: |
|
59 self.tr("The certificate is invalid."), |
|
60 QWebEngineCertificateError.CertificateWeakSignatureAlgorithm: |
|
61 self.tr("The certificate is signed using a weak signature" |
|
62 " algorithm."), |
|
63 QWebEngineCertificateError.CertificateNonUniqueName: |
|
64 self.tr("The host name specified in the certificate is" |
|
65 " not unique."), |
|
66 QWebEngineCertificateError.CertificateWeakKey: |
|
67 self.tr("The certificate contains a weak key."), |
|
68 QWebEngineCertificateError.CertificateNameConstraintViolation: |
|
69 self.tr("The certificate claimed DNS names that are in" |
|
70 " violation of name constraints."), |
|
71 } |
|
72 try: |
|
73 self.__errorDescriptions[ |
|
74 QWebEngineCertificateError.CertificateValidityTooLong] = \ |
|
75 self.tr("The certificate has a validity period that is" |
|
76 " too long.") |
|
77 except AttributeError: |
|
78 # the value was added in Qt 5.7 |
|
79 pass |
|
80 try: |
|
81 self.__errorDescriptions[ |
|
82 QWebEngineCertificateError.CertificateTransparencyRequired] = \ |
|
83 self.tr("Certificate Transparency was required for this" |
|
84 " connection, but the server did not provide" |
|
85 " information that complied with the policy.") |
|
86 except AttributeError: |
|
87 # the value was added in Qt 5.8 |
|
88 pass |
|
89 |
|
90 for host, errors in errorsDict.items(): |
|
91 itm = QTreeWidgetItem(self.errorsTree, [host]) |
|
92 self.errorsTree.setFirstItemColumnSpanned(itm, True) |
|
93 for error in errors: |
|
94 try: |
|
95 errorDesc = self.__errorDescriptions[error] |
|
96 except KeyError: |
|
97 errorDesc = self.tr("No error description available.") |
|
98 QTreeWidgetItem(itm, [str(error), errorDesc]) |
|
99 |
|
100 self.errorsTree.expandAll() |
|
101 for i in range(self.errorsTree.columnCount()): |
|
102 self.errorsTree.resizeColumnToContents(i) |
|
103 self.errorsTree.sortItems(0, Qt.AscendingOrder) |
|
104 |
|
105 self.__setRemoveButtons() |
|
106 |
|
107 def __setRemoveButtons(self): |
|
108 """ |
|
109 Private method to set the state of the 'remove' buttons. |
|
110 """ |
|
111 if self.errorsTree.topLevelItemCount() == 0: |
|
112 self.removeButton.setEnabled(False) |
|
113 self.removeAllButton.setEnabled(False) |
|
114 else: |
|
115 self.removeAllButton.setEnabled(True) |
|
116 self.removeButton.setEnabled( |
|
117 len(self.errorsTree.selectedItems()) > 0) |
|
118 |
|
119 @pyqtSlot(QPoint) |
|
120 def on_errorsTree_customContextMenuRequested(self, pos): |
|
121 """ |
|
122 Private slot to show the context menu. |
|
123 |
|
124 @param pos cursor position |
|
125 @type QPoint |
|
126 """ |
|
127 menu = QMenu() |
|
128 menu.addAction( |
|
129 self.tr("Remove Selected"), |
|
130 self.on_removeButton_clicked).setEnabled( |
|
131 self.errorsTree.topLevelItemCount() > 0 and |
|
132 len(self.errorsTree.selectedItems()) > 0) |
|
133 menu.addAction( |
|
134 self.tr("Remove All"), |
|
135 self.on_removeAllButton_clicked).setEnabled( |
|
136 self.errorsTree.topLevelItemCount() > 0) |
|
137 |
|
138 menu.exec_(self.errorsTree.mapToGlobal(pos)) |
|
139 |
|
140 @pyqtSlot() |
|
141 def on_errorsTree_itemSelectionChanged(self): |
|
142 """ |
|
143 Private slot handling the selection of entries. |
|
144 """ |
|
145 self.__setRemoveButtons() |
|
146 |
|
147 @pyqtSlot() |
|
148 def on_removeButton_clicked(self): |
|
149 """ |
|
150 Private slot to remove the selected items. |
|
151 """ |
|
152 for itm in self.errorsTree.selectedItems(): |
|
153 pitm = itm.parent() |
|
154 if pitm: |
|
155 pitm.removeChild(itm) |
|
156 else: |
|
157 index = self.errorsTree.indexOfTopLevelItem(itm) |
|
158 self.errorsTree.takeTopLevelItem(index) |
|
159 del itm |
|
160 |
|
161 # remove all hosts without an exception |
|
162 for index in range(self.errorsTree.topLevelItemCount() - 1, -1, -1): |
|
163 itm = self.errorsTree.topLevelItem(index) |
|
164 if itm.childCount() == 0: |
|
165 self.errorsTree.takeTopLevelItem(index) |
|
166 del itm |
|
167 |
|
168 @pyqtSlot() |
|
169 def on_removeAllButton_clicked(self): |
|
170 """ |
|
171 Private slot to remove all entries. |
|
172 """ |
|
173 self.errorsTree.clear() |
|
174 |
|
175 def getSslErrorExceptions(self): |
|
176 """ |
|
177 Public method to retrieve the list of SSL error exceptions. |
|
178 |
|
179 @return error exceptions |
|
180 @rtype dict of list of int |
|
181 """ |
|
182 errors = {} |
|
183 |
|
184 for index in range(self.errorsTree.topLevelItemCount()): |
|
185 itm = self.errorsTree.topLevelItem(index) |
|
186 host = itm.text(0) |
|
187 errors[host] = [] |
|
188 for cindex in range(itm.childCount()): |
|
189 citm = itm.child(cindex) |
|
190 errors[host].append(int(citm.text(0))) |
|
191 |
|
192 return errors |