21 #include <QDialogButtonBox>
23 #include <QPushButton>
25 #include <QToolButton>
37 const QList<QSslError> &sslErrors,
39 const QString &digest,
40 const QString &hostport )
42 , mSslConfiguration( reply->sslConfiguration() )
43 , mSslErrors( sslErrors )
45 , mHostPort( hostport )
47 if ( mDigest.isEmpty() )
51 if ( mHostPort.isEmpty() )
53 mHostPort = QStringLiteral(
"%1:%2" )
54 .arg( reply->url().host() )
55 .arg( reply->url().port() != -1 ? reply->url().port() : 443 )
60 connect( buttonBox, &QDialogButtonBox::clicked,
this, &QgsAuthSslErrorsDialog::buttonBox_clicked );
61 connect( btnChainInfo, &QToolButton::clicked,
this, &QgsAuthSslErrorsDialog::btnChainInfo_clicked );
62 connect( btnChainCAs, &QToolButton::clicked,
this, &QgsAuthSslErrorsDialog::btnChainCAs_clicked );
64 QStyle *style = QApplication::style();
65 lblWarningIcon->setPixmap( style->standardIcon( QStyle::SP_MessageBoxWarning ).pixmap( 48, 48 ) );
66 lblWarningIcon->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
68 lblErrorsText->setStyleSheet( QStringLiteral(
"QLabel{ font-weight: bold; }" ) );
69 leUrl->setText( reply->request().url().toString() );
71 ignoreButton()->setDefault(
false );
72 abortButton()->setDefault(
true );
76 saveButton()->setEnabled(
false );
78 saveButton()->setText( QStringLiteral(
"%1 && %2" ).arg( saveButton()->text(),
79 ignoreButton()->text() ) );
81 grpbxSslConfig->setChecked(
false );
82 grpbxSslConfig->setCollapsed(
true );
83 connect( grpbxSslConfig, &QGroupBox::toggled,
84 this, &QgsAuthSslErrorsDialog::loadUnloadCertificate );
87 this, &QgsAuthSslErrorsDialog::widgetReadyToSaveChanged );
88 wdgtSslConfig->setConfigCheckable(
false );
89 wdgtSslConfig->certificateGroupBox()->setFlat(
true );
93 btnChainInfo->setVisible(
false );
94 btnChainCAs->setVisible(
false );
95 grpbxSslConfig->setVisible(
false );
96 saveButton()->setVisible(
false );
102 void QgsAuthSslErrorsDialog::loadUnloadCertificate(
bool load )
104 grpbxSslErrors->setCollapsed( load );
107 QgsDebugMsg( QStringLiteral(
"Unloading certificate and host:port" ) );
108 clearCertificateConfig();
111 wdgtSslConfig->setEnabled(
true );
112 QgsDebugMsg( QStringLiteral(
"Loading certificate for host:port = %1" ).arg( mHostPort ) );
113 wdgtSslConfig->setSslCertificate( mSslConfiguration.peerCertificate(), mHostPort );
114 if ( !mSslErrors.isEmpty() )
116 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
120 void QgsAuthSslErrorsDialog::showCertificateChainInfo()
122 QList<QSslCertificate> peerchain( mSslConfiguration.peerCertificateChain() );
124 if ( !peerchain.isEmpty() )
126 const QSslCertificate cert = peerchain.takeFirst();
127 if ( !cert.isNull() )
130 dlg->setWindowModality( Qt::WindowModal );
131 dlg->resize( 675, 500 );
138 void QgsAuthSslErrorsDialog::showCertificateChainCAsInfo()
140 const QList< QSslCertificate > certificates = mSslConfiguration.caCertificates();
141 for (
const auto &cert : certificates )
143 qDebug() << cert.subjectInfo( QSslCertificate::SubjectInfo::CommonName );
147 dlg->setWindowModality( Qt::WindowModal );
148 dlg->resize( 675, 500 );
153 void QgsAuthSslErrorsDialog::widgetReadyToSaveChanged(
bool cansave )
155 ignoreButton()->setDefault(
false );
156 abortButton()->setDefault( !cansave );
157 saveButton()->setEnabled( cansave );
158 saveButton()->setDefault( cansave );
161 void QgsAuthSslErrorsDialog::checkCanSave()
163 widgetReadyToSaveChanged( wdgtSslConfig->readyToSave() );
166 void QgsAuthSslErrorsDialog::clearCertificateConfig()
168 wdgtSslConfig->resetSslCertConfig();
169 wdgtSslConfig->setEnabled(
false );
173 void QgsAuthSslErrorsDialog::buttonBox_clicked( QAbstractButton *button )
175 const QDialogButtonBox::StandardButton btnenum( buttonBox->standardButton( button ) );
178 case QDialogButtonBox::Ignore:
180 QStringLiteral(
"%1:%2" ).arg( mDigest, mHostPort ),
184 case QDialogButtonBox::Save:
186 wdgtSslConfig->saveSslCertConfig();
189 case QDialogButtonBox::Abort:
196 if ( btnenum == QDialogButtonBox::Abort &&
197 QgsSettings().value( QStringLiteral(
"clear_auth_cache_on_errors" ),
199 QgsSettings::Section::Auth ).toBool( ) )
205 void QgsAuthSslErrorsDialog::populateErrorsList()
208 errs.reserve( mSslErrors.size() );
209 const auto constMSslErrors = mSslErrors;
210 for (
const QSslError &err : constMSslErrors )
212 errs << QStringLiteral(
"* %1: %2" )
216 teSslErrors->setPlainText( errs.join( QLatin1Char(
'\n' ) ) );
219 QPushButton *QgsAuthSslErrorsDialog::ignoreButton()
221 return buttonBox->button( QDialogButtonBox::Ignore );
224 QPushButton *QgsAuthSslErrorsDialog::saveButton()
226 return buttonBox->button( QDialogButtonBox::Save );
229 QPushButton *QgsAuthSslErrorsDialog::abortButton()
231 return buttonBox->button( QDialogButtonBox::Abort );
234 void QgsAuthSslErrorsDialog::btnChainInfo_clicked()
236 showCertificateChainInfo();
239 void QgsAuthSslErrorsDialog::btnChainCAs_clicked()
241 showCertificateChainCAsInfo();
244 void QgsAuthSslErrorsDialog::grpbxSslErrors_collapsedStateChanged(
bool collapsed )
248 btnChainInfo->setVisible(
false );
249 btnChainCAs->setVisible(
false );
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
Dialog wrapper for widget displaying detailed info on a certificate and its hierarchical trust chain.
static QString sslErrorEnumString(QSslError::SslError errenum)
Gets short strings describing an SSL error.
static QString shaHexForCert(const QSslCertificate &cert, bool formatted=false)
Gets the sha1 hash for certificate.
bool updateIgnoredSslErrorsCache(const QString &shahostport, const QList< QSslError > &errors)
Update ignored SSL error cache with possible ignored SSL errors, using sha:host:port key.
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.
Widget for listing trusted Certificate (Intermediate) Authorities used in secure connections.
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.
This class is a composition of two QSettings instances: