19#include "moc_qgsauthsslerrorsdialog.cpp"
22#include <QDialogButtonBox>
38 const QList<QSslError> &sslErrors,
40 const QString &digest,
41 const QString &hostport )
43 , mSslConfiguration( reply->sslConfiguration() )
44 , mSslErrors( sslErrors )
46 , mHostPort( hostport )
48 if ( mDigest.isEmpty() )
52 if ( mHostPort.isEmpty() )
54 mHostPort = QStringLiteral(
"%1:%2" )
55 .arg( reply->url().host() )
56 .arg( reply->url().port() != -1 ? reply->url().port() : 443 )
61 connect( buttonBox, &QDialogButtonBox::clicked,
this, &QgsAuthSslErrorsDialog::buttonBox_clicked );
62 connect( btnChainInfo, &QToolButton::clicked,
this, &QgsAuthSslErrorsDialog::btnChainInfo_clicked );
63 connect( btnChainCAs, &QToolButton::clicked,
this, &QgsAuthSslErrorsDialog::btnChainCAs_clicked );
65 QStyle *style = QApplication::style();
66 lblWarningIcon->setPixmap( style->standardIcon( QStyle::SP_MessageBoxWarning ).pixmap( 48, 48 ) );
67 lblWarningIcon->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
69 lblErrorsText->setStyleSheet( QStringLiteral(
"QLabel{ font-weight: bold; }" ) );
70 leUrl->setText( reply->request().url().toString() );
72 ignoreButton()->setDefault(
false );
73 abortButton()->setDefault(
true );
77 saveButton()->setEnabled(
false );
79 saveButton()->setText( QStringLiteral(
"%1 && %2" ).arg( saveButton()->text(),
80 ignoreButton()->text() ) );
82 grpbxSslConfig->setChecked(
false );
83 grpbxSslConfig->setCollapsed(
true );
84 connect( grpbxSslConfig, &QGroupBox::toggled,
85 this, &QgsAuthSslErrorsDialog::loadUnloadCertificate );
88 this, &QgsAuthSslErrorsDialog::widgetReadyToSaveChanged );
89 wdgtSslConfig->setConfigCheckable(
false );
90 wdgtSslConfig->certificateGroupBox()->setFlat(
true );
94 btnChainInfo->setVisible(
false );
95 btnChainCAs->setVisible(
false );
96 grpbxSslConfig->setVisible(
false );
97 saveButton()->setVisible(
false );
100 populateErrorsList();
103void QgsAuthSslErrorsDialog::loadUnloadCertificate(
bool load )
105 grpbxSslErrors->setCollapsed( load );
108 QgsDebugMsgLevel( QStringLiteral(
"Unloading certificate and host:port" ), 2 );
109 clearCertificateConfig();
112 wdgtSslConfig->setEnabled(
true );
113 QgsDebugMsgLevel( QStringLiteral(
"Loading certificate for host:port = %1" ).arg( mHostPort ), 2 );
114 wdgtSslConfig->setSslCertificate( mSslConfiguration.peerCertificate(), mHostPort );
115 if ( !mSslErrors.isEmpty() )
117 wdgtSslConfig->appendSslIgnoreErrors( mSslErrors );
121void QgsAuthSslErrorsDialog::showCertificateChainInfo()
123 QList<QSslCertificate> peerchain( mSslConfiguration.peerCertificateChain() );
125 if ( !peerchain.isEmpty() )
127 const QSslCertificate cert = peerchain.takeFirst();
128 if ( !cert.isNull() )
131 dlg->setWindowModality( Qt::WindowModal );
132 dlg->resize( 675, 500 );
139void QgsAuthSslErrorsDialog::showCertificateChainCAsInfo()
141 const QList< QSslCertificate > certificates = mSslConfiguration.caCertificates();
142 for (
const auto &cert : certificates )
144 qDebug() << cert.subjectInfo( QSslCertificate::SubjectInfo::CommonName );
148 dlg->setWindowModality( Qt::WindowModal );
149 dlg->resize( 675, 500 );
154void QgsAuthSslErrorsDialog::widgetReadyToSaveChanged(
bool cansave )
156 ignoreButton()->setDefault(
false );
157 abortButton()->setDefault( !cansave );
158 saveButton()->setEnabled( cansave );
159 saveButton()->setDefault( cansave );
162void QgsAuthSslErrorsDialog::checkCanSave()
164 widgetReadyToSaveChanged( wdgtSslConfig->readyToSave() );
167void QgsAuthSslErrorsDialog::clearCertificateConfig()
169 wdgtSslConfig->resetSslCertConfig();
170 wdgtSslConfig->setEnabled(
false );
174void QgsAuthSslErrorsDialog::buttonBox_clicked( QAbstractButton *button )
176 const QDialogButtonBox::StandardButton btnenum( buttonBox->standardButton( button ) );
179 case QDialogButtonBox::Ignore:
181 QStringLiteral(
"%1:%2" ).arg( mDigest, mHostPort ),
185 case QDialogButtonBox::Save:
187 wdgtSslConfig->saveSslCertConfig();
190 case QDialogButtonBox::Abort:
197 if ( btnenum == QDialogButtonBox::Abort &&
198 QgsSettings().value( QStringLiteral(
"clear_auth_cache_on_errors" ),
206void QgsAuthSslErrorsDialog::populateErrorsList()
209 errs.reserve( mSslErrors.size() );
210 const auto constMSslErrors = mSslErrors;
211 for (
const QSslError &err : constMSslErrors )
213 errs << QStringLiteral(
"* %1: %2" )
217 teSslErrors->setPlainText( errs.join( QLatin1Char(
'\n' ) ) );
220QPushButton *QgsAuthSslErrorsDialog::ignoreButton()
222 return buttonBox->button( QDialogButtonBox::Ignore );
225QPushButton *QgsAuthSslErrorsDialog::saveButton()
227 return buttonBox->button( QDialogButtonBox::Save );
230QPushButton *QgsAuthSslErrorsDialog::abortButton()
232 return buttonBox->button( QDialogButtonBox::Abort );
235void QgsAuthSslErrorsDialog::btnChainInfo_clicked()
237 showCertificateChainInfo();
240void QgsAuthSslErrorsDialog::btnChainCAs_clicked()
242 showCertificateChainCAsInfo();
245void QgsAuthSslErrorsDialog::grpbxSslErrors_collapsedStateChanged(
bool collapsed )
249 btnChainInfo->setVisible(
false );
250 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:
#define QgsDebugMsgLevel(str, level)