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