QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsauthsslerrorsdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthsslerrorsdialog.cpp
3 ---------------------
4 begin : May 22, 2015
5 copyright : (C) 2015 by Boundless Spatial, Inc. USA
6 author : Larry Shaffer
7 email : lshaffer at boundlessgeo dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgsapplication.h"
21#include "qgsauthcertutils.h"
22#include "qgsauthmanager.h"
26#include "qgslogger.h"
28
29#include <QDialogButtonBox>
30#include <QFont>
31#include <QPushButton>
32#include <QString>
33#include <QStyle>
34#include <QToolButton>
35
36#include "moc_qgsauthsslerrorsdialog.cpp"
37
38using namespace Qt::StringLiterals;
39
40QgsAuthSslErrorsDialog::QgsAuthSslErrorsDialog( QNetworkReply *reply, const QList<QSslError> &sslErrors, QWidget *parent, const QString &digest, const QString &hostport )
41 : QDialog( parent )
42 , mSslConfiguration( reply->sslConfiguration() )
43 , mSslErrors( sslErrors )
44 , mDigest( digest )
45 , mHostPort( hostport )
46{
47 if ( mDigest.isEmpty() )
48 {
49 mDigest = QgsAuthCertUtils::shaHexForCert( mSslConfiguration.peerCertificate() );
50 }
51 if ( mHostPort.isEmpty() )
52 {
53 mHostPort = u"%1:%2"_s.arg( reply->url().host() ).arg( reply->url().port() != -1 ? reply->url().port() : 443 ).trimmed();
54 }
55
56 setupUi( this );
57 connect( buttonBox, &QDialogButtonBox::clicked, this, &QgsAuthSslErrorsDialog::buttonBox_clicked );
58 connect( btnChainInfo, &QToolButton::clicked, this, &QgsAuthSslErrorsDialog::btnChainInfo_clicked );
59 connect( btnChainCAs, &QToolButton::clicked, this, &QgsAuthSslErrorsDialog::btnChainCAs_clicked );
60 connect( grpbxSslErrors, &QgsCollapsibleGroupBoxBasic::collapsedStateChanged, this, &QgsAuthSslErrorsDialog::grpbxSslErrors_collapsedStateChanged );
61 QStyle *style = QApplication::style();
62 lblWarningIcon->setPixmap( style->standardIcon( QStyle::SP_MessageBoxWarning ).pixmap( 48, 48 ) );
63 lblWarningIcon->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
64
65 lblErrorsText->setStyleSheet( u"QLabel{ font-weight: bold; }"_s );
66 leUrl->setText( reply->request().url().toString() );
67
68 ignoreButton()->setDefault( false );
69 abortButton()->setDefault( true );
70
71 if ( !QgsApplication::authManager()->isDisabled() )
72 {
73 saveButton()->setEnabled( false );
74
75 saveButton()->setText( u"%1 && %2"_s.arg( saveButton()->text(), ignoreButton()->text() ) );
76
77 grpbxSslConfig->setChecked( false );
78 grpbxSslConfig->setCollapsed( true );
79 connect( grpbxSslConfig, &QGroupBox::toggled, this, &QgsAuthSslErrorsDialog::loadUnloadCertificate );
80
81 connect( wdgtSslConfig, &QgsAuthSslConfigWidget::readyToSaveChanged, this, &QgsAuthSslErrorsDialog::widgetReadyToSaveChanged );
82 wdgtSslConfig->setConfigCheckable( false );
83 wdgtSslConfig->certificateGroupBox()->setFlat( true );
84 }
85 else
86 {
87 btnChainInfo->setVisible( false );
88 btnChainCAs->setVisible( false );
89 grpbxSslConfig->setVisible( false );
90 saveButton()->setVisible( false );
91 }
92
93 populateErrorsList();
94}
95
96void QgsAuthSslErrorsDialog::loadUnloadCertificate( bool load )
97{
98 grpbxSslErrors->setCollapsed( load );
99 if ( !load )
100 {
101 QgsDebugMsgLevel( u"Unloading certificate and host:port"_s, 2 );
102 clearCertificateConfig();
103 return;
104 }
105 wdgtSslConfig->setEnabled( true );
106 QgsDebugMsgLevel( u"Loading certificate for host:port = %1"_s.arg( mHostPort ), 2 );
107 wdgtSslConfig->setSslCertificate( mSslConfiguration.peerCertificate(), mHostPort );
108 if ( !mSslErrors.isEmpty() )
109 {
110 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
111 }
112}
113
114void QgsAuthSslErrorsDialog::showCertificateChainInfo()
115{
116 QList<QSslCertificate> peerchain( mSslConfiguration.peerCertificateChain() );
117
118 if ( !peerchain.isEmpty() )
119 {
120 const QSslCertificate cert = peerchain.takeFirst();
121 if ( !cert.isNull() )
122 {
123 QgsAuthCertInfoDialog *dlg = new QgsAuthCertInfoDialog( cert, false, this, peerchain );
124 dlg->setWindowModality( Qt::WindowModal );
125 dlg->resize( 675, 500 );
126 dlg->exec();
127 dlg->deleteLater();
128 }
129 }
130}
131
132void QgsAuthSslErrorsDialog::showCertificateChainCAsInfo()
133{
134 const QList<QSslCertificate> certificates = mSslConfiguration.caCertificates();
135 for ( const auto &cert : certificates )
136 {
137 qDebug() << cert.subjectInfo( QSslCertificate::SubjectInfo::CommonName );
138 }
139
140 QgsAuthTrustedCAsDialog *dlg = new QgsAuthTrustedCAsDialog( this, mSslConfiguration.caCertificates() );
141 dlg->setWindowModality( Qt::WindowModal );
142 dlg->resize( 675, 500 );
143 dlg->exec();
144 dlg->deleteLater();
145}
146
147void QgsAuthSslErrorsDialog::widgetReadyToSaveChanged( bool cansave )
148{
149 ignoreButton()->setDefault( false );
150 abortButton()->setDefault( !cansave );
151 saveButton()->setEnabled( cansave );
152 saveButton()->setDefault( cansave );
153}
154
155void QgsAuthSslErrorsDialog::checkCanSave()
156{
157 widgetReadyToSaveChanged( wdgtSslConfig->readyToSave() );
158}
159
160void QgsAuthSslErrorsDialog::clearCertificateConfig()
161{
162 wdgtSslConfig->resetSslCertConfig();
163 wdgtSslConfig->setEnabled( false );
164 checkCanSave();
165}
166
167void QgsAuthSslErrorsDialog::buttonBox_clicked( QAbstractButton *button )
168{
169 const QDialogButtonBox::StandardButton btnenum( buttonBox->standardButton( button ) );
170 switch ( btnenum )
171 {
172 case QDialogButtonBox::Ignore:
173 QgsApplication::authManager()->updateIgnoredSslErrorsCache( u"%1:%2"_s.arg( mDigest, mHostPort ), mSslErrors );
174 accept();
175 break;
176 case QDialogButtonBox::Save:
177 // save config and ignore errors
178 wdgtSslConfig->saveSslCertConfig();
179 accept();
180 break;
181 case QDialogButtonBox::Abort:
182 default:
183 reject();
184 break;
185 }
186 // Clear access cache if the user choose abort and the
187 // setting allows it
188 if ( btnenum == QDialogButtonBox::Abort && QgsSettings().value( u"clear_auth_cache_on_errors"_s, true, QgsSettings::Section::Auth ).toBool() )
189 {
190 QgsNetworkAccessManager::instance()->clearAccessCache();
191 }
192}
193
194void QgsAuthSslErrorsDialog::populateErrorsList()
195{
196 QStringList errs;
197 errs.reserve( mSslErrors.size() );
198 const auto constMSslErrors = mSslErrors;
199 for ( const QSslError &err : constMSslErrors )
200 {
201 errs << u"* %1: %2"_s.arg( QgsAuthCertUtils::sslErrorEnumString( err.error() ), err.errorString() );
202 }
203 teSslErrors->setPlainText( errs.join( QLatin1Char( '\n' ) ) );
204}
205
206QPushButton *QgsAuthSslErrorsDialog::ignoreButton()
207{
208 return buttonBox->button( QDialogButtonBox::Ignore );
209}
210
211QPushButton *QgsAuthSslErrorsDialog::saveButton()
212{
213 return buttonBox->button( QDialogButtonBox::Save );
214}
215
216QPushButton *QgsAuthSslErrorsDialog::abortButton()
217{
218 return buttonBox->button( QDialogButtonBox::Abort );
219}
220
221void QgsAuthSslErrorsDialog::btnChainInfo_clicked()
222{
223 showCertificateChainInfo();
224}
225
226void QgsAuthSslErrorsDialog::btnChainCAs_clicked()
227{
228 showCertificateChainCAsInfo();
229}
230
231void QgsAuthSslErrorsDialog::grpbxSslErrors_collapsedStateChanged( bool collapsed )
232{
233 if ( !collapsed && QgsApplication::authManager()->isDisabled() )
234 {
235 btnChainInfo->setVisible( false );
236 btnChainCAs->setVisible( false );
237 }
238}
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
bool updateIgnoredSslErrorsCache(const QString &shahostport, const QList< QSslError > &errors)
Update ignored SSL error cache with possible ignored SSL errors, using sha:host:port key.
void readyToSaveChanged(bool cansave)
Emitted when the configuration can be saved changes.
QgsAuthSslErrorsDialog(QNetworkReply *reply, const QList< QSslError > &sslErrors, QWidget *parent=nullptr, const QString &digest=QString(), const QString &hostport=QString())
Construct a dialog to handle SSL errors and saving SSL server certificate exceptions.
void collapsedStateChanged(bool collapsed)
Signal emitted when groupbox collapsed/expanded state is changed, and when first shown.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63