18#include "moc_qgsauthsslconfigwidget.cpp"
21#include <QDialogButtonBox>
32static const QString configFoundText_() {
return QObject::tr(
"Configuration loaded from database" ); }
33static const QString configNotFoundText_() {
return QObject::tr(
"Configuration not found in database" ); }
38 , mConnectionCAs( connectionCAs )
43 mAuthNotifyLayout =
new QVBoxLayout;
44 this->setLayout( mAuthNotifyLayout );
46 mAuthNotifyLayout->addWidget( mAuthNotify );
51 connect( btnCertInfo, &QToolButton::clicked,
this, &QgsAuthSslConfigWidget::btnCertInfo_clicked );
59 lblLoadedConfig->setVisible(
false );
60 lblLoadedConfig->clear();
86 return grpbxSslConfig;
90QTreeWidgetItem *QgsAuthSslConfigWidget::addRootItem(
const QString &label )
92 QTreeWidgetItem *item =
new QTreeWidgetItem(
93 QStringList() << label,
94 static_cast<int>( ConfigParent )
97 item->setTextAlignment( 0, Qt::AlignVCenter );
98 item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
99 treeSslConfig->insertTopLevelItem( treeSslConfig->topLevelItemCount(), item );
104void QgsAuthSslConfigWidget::setUpSslConfigTree()
106 treeSslConfig->setColumnCount( 1 );
109 mProtocolItem = addRootItem( tr(
"Protocol" ) );
110 mProtocolCmbBx =
new QComboBox( treeSslConfig );
113 mProtocolCmbBx->setMaximumWidth( 300 );
114 mProtocolCmbBx->setCurrentIndex( 0 );
115 QTreeWidgetItem *protocolitem =
new QTreeWidgetItem(
117 QStringList() << QString(),
118 static_cast<int>( ConfigItem )
120 protocolitem->setFlags( protocolitem->flags() & ~Qt::ItemIsSelectable );
121 treeSslConfig->setItemWidget( protocolitem, 0, mProtocolCmbBx );
122 mProtocolItem->setExpanded(
true );
124 mVerifyModeItem = addRootItem( tr(
"Peer verification" ) );
125 mVerifyPeerCmbBx =
new QComboBox( treeSslConfig );
126 mVerifyPeerCmbBx->addItem( tr(
"Verify Peer Certs" ),
static_cast<int>( QSslSocket::VerifyPeer ) );
127 mVerifyPeerCmbBx->addItem( tr(
"Do Not Verify Peer Certs" ),
static_cast<int>( QSslSocket::VerifyNone ) );
128 mVerifyPeerCmbBx->setMaximumWidth( 300 );
129 mVerifyPeerCmbBx->setCurrentIndex( 0 );
130 QTreeWidgetItem *peerverifycmbxitem =
new QTreeWidgetItem(
132 QStringList() << QString(),
133 static_cast<int>( ConfigItem )
135 peerverifycmbxitem->setFlags( peerverifycmbxitem->flags() & ~Qt::ItemIsSelectable );
136 treeSslConfig->setItemWidget( peerverifycmbxitem, 0, mVerifyPeerCmbBx );
137 mVerifyModeItem->setExpanded(
true );
139 mVerifyDepthItem = addRootItem( tr(
"Peer verification depth (0 = complete cert chain)" ) );
140 mVerifyDepthSpnBx =
new QSpinBox( treeSslConfig );
141 mVerifyDepthSpnBx->setMinimum( 0 );
142 mVerifyDepthSpnBx->setMaximum( 10 );
143 mVerifyDepthSpnBx->setMaximumWidth( 200 );
144 mVerifyDepthSpnBx->setAlignment( Qt::AlignHCenter );
145 QTreeWidgetItem *peerverifyspnbxitem =
new QTreeWidgetItem(
147 QStringList() << QString(),
148 static_cast<int>( ConfigItem )
150 peerverifyspnbxitem->setFlags( peerverifyspnbxitem->flags() & ~Qt::ItemIsSelectable );
151 treeSslConfig->setItemWidget( peerverifyspnbxitem, 0, mVerifyDepthSpnBx );
152 mVerifyDepthItem->setExpanded(
true );
154 mIgnoreErrorsItem = addRootItem( tr(
"Ignore errors" ) );
157 for (
int i = 0; i < errenums.size(); i++ )
159 QTreeWidgetItem *item =
new QTreeWidgetItem(
161 QStringList() << errenums.at( i ).second,
162 static_cast<int>( ConfigItem )
164 item->setCheckState( 0, Qt::Unchecked );
165 item->setTextAlignment( 0, Qt::AlignVCenter );
166 item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
167 item->setData( 0, Qt::UserRole, errenums.at( i ).first );
169 mIgnoreErrorsItem->setExpanded(
true );
192 return QSslCertificate();
203 return leHost->text();
212 if ( grpbxSslConfig->isCheckable() )
214 grpbxSslConfig->setChecked( enable );
230 if ( !hostport.isEmpty() )
242 lblLoadedConfig->setVisible(
true );
250 lblLoadedConfig->setText( configNotFoundText_() );
266 QgsDebugError( QStringLiteral(
"Passed-in SSL custom config is null" ) );
273 QgsDebugError( QStringLiteral(
"SSL custom config's cert is null" ) );
285 lblLoadedConfig->setVisible(
true );
286 lblLoadedConfig->setText( configFoundText_() );
297 QgsDebugError( QStringLiteral(
"SSL custom config FAILED to store in authentication database" ) );
308 mConnectionCAs.clear();
309 leCommonName->clear();
310 leCommonName->setStyleSheet( QString() );
313 lblLoadedConfig->setVisible(
false );
314 lblLoadedConfig->clear();
325 return QSsl::UnknownProtocol;
327 return ( QSsl::SslProtocol ) mProtocolCmbBx->currentData().toInt();
336 const int indx( mProtocolCmbBx->findData(
static_cast<int>( protocol ) ) );
337 mProtocolCmbBx->setCurrentIndex( indx );
346 mProtocolCmbBx->setCurrentIndex( 0 );
357 QList<QSslError::SslError> errenums;
358 const auto constErrors = errors;
359 for (
const QSslError &err : constErrors )
361 errenums << err.error();
364 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
366 QTreeWidgetItem *item( mIgnoreErrorsItem->child( i ) );
367 if ( errenums.contains( ( QSslError::SslError ) item->data( 0, Qt::UserRole ).toInt() ) )
369 item->setCheckState( 0, Qt::Checked );
380 QList<QSslError> errors;
381 const auto constErrorenums = errorenums;
382 for (
const QSslError::SslError errorenum : constErrorenums )
384 errors << QSslError( errorenum );
395 if ( errors.isEmpty() )
402 QList<QSslError::SslError> errenums;
403 const auto constErrors = errors;
404 for (
const QSslError &err : constErrors )
406 errenums << err.error();
409 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
411 QTreeWidgetItem *item( mIgnoreErrorsItem->child( i ) );
412 const bool enable( errenums.contains( ( QSslError::SslError ) item->data( 0, Qt::UserRole ).toInt() ) );
413 item->setCheckState( 0, enable ? Qt::Checked : Qt::Unchecked );
423 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
425 mIgnoreErrorsItem->child( i )->setCheckState( 0, Qt::Unchecked );
431 QList<QSslError::SslError> errs;
436 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
438 QTreeWidgetItem *item( mIgnoreErrorsItem->child( i ) );
439 if ( item->checkState( 0 ) == Qt::Checked )
441 errs.append( ( QSslError::SslError ) item->data( 0, Qt::UserRole ).toInt() );
451 return QSslSocket::AutoVerifyPeer;
453 return ( QSslSocket::PeerVerifyMode ) mVerifyPeerCmbBx->currentData().toInt();
462 return mVerifyDepthSpnBx->value();
473 const int indx( mVerifyPeerCmbBx->findData(
static_cast<int>( mode ) ) );
474 mVerifyPeerCmbBx->setCurrentIndex( indx );
476 mVerifyDepthSpnBx->setValue( modedepth );
485 mVerifyPeerCmbBx->setCurrentIndex( 0 );
486 mVerifyDepthSpnBx->setValue( 0 );
495 const bool cansave = ( isEnabled() && ( grpbxSslConfig->isCheckable() ? grpbxSslConfig->isChecked() : true ) && validateHostPort( leHost->text() ) );
496 if ( mCanSave != cansave )
510 leHost->setText( host );
513bool QgsAuthSslConfigWidget::validateHostPort(
const QString &txt )
515 const QString hostport( txt );
516 if ( hostport.isEmpty() )
523 const QString urlbase( QStringLiteral(
"https://%1" ).arg( hostport ) );
524 const QUrl url( urlbase );
525 return ( !url.host().isEmpty() && QString::number( url.port() ).size() > 0 && QStringLiteral(
"https://%1:%2" ).arg( url.host() ).arg( url.port() ) == urlbase );
534 const bool valid = validateHostPort( txt );
545 grpbxSslConfig->setCheckable( checkable );
548 grpbxSslConfig->setEnabled(
true );
552void QgsAuthSslConfigWidget::btnCertInfo_clicked()
554 if ( mCert.isNull() )
560 dlg->setWindowModality( Qt::WindowModal );
561 dlg->resize( 675, 500 );
573 setWindowTitle( tr(
"Custom Certificate Configuration" ) );
574 QVBoxLayout *layout =
new QVBoxLayout(
this );
575 layout->setContentsMargins( 6, 6, 6, 6 );
579 layout->addWidget( mSslConfigWdgt );
581 QDialogButtonBox *buttonBox =
new QDialogButtonBox(
582 QDialogButtonBox::Close | QDialogButtonBox::Save, Qt::Horizontal,
this
585 buttonBox->button( QDialogButtonBox::Close )->setDefault(
true );
586 mSaveButton = buttonBox->button( QDialogButtonBox::Save );
587 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QWidget::close );
589 layout->addWidget( buttonBox );
592 mSaveButton->setEnabled( mSslConfigWdgt->
readyToSave() );
601void QgsAuthSslConfigDialog::checkCanSave(
bool cansave )
603 mSaveButton->setEnabled( cansave );
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 resolvedCertName(const QSslCertificate &cert, bool issuer=false)
Gets the general name via RFC 5280 resolution.
static QString shaHexForCert(const QSslCertificate &cert, bool formatted=false)
Gets the sha1 hash for certificate.
static QString getSslProtocolName(QSsl::SslProtocol protocol)
SSL Protocol name strings per enum.
static QList< QPair< QSslError::SslError, QString > > sslErrorEnumStrings()
Gets short strings describing SSL errors.
Configuration container for SSL server connection exceptions or overrides.
void setSslProtocol(QSsl::SslProtocol protocol)
Sets SSL server protocol to use in connections.
void setSslCertificate(const QSslCertificate &cert)
Sets server certificate object.
void setSslHostPort(const QString &hostport)
Sets server host:port string.
QSsl::SslProtocol sslProtocol() const
SSL server protocol to use in connections.
void setSslPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
Sets SSL client's peer verify mode to use in connections.
void setSslPeerVerifyDepth(int depth)
Set number or SSL client's peer to verify in connections.
int sslPeerVerifyDepth() const
Number or SSL client's peer to verify in connections.
bool isNull() const
Whether configuration is null (missing components)
void setSslIgnoredErrorEnums(const QList< QSslError::SslError > &errors)
Sets SSL server errors (as enum list) to ignore in connections.
const QList< QSslError::SslError > sslIgnoredErrorEnums() const
SSL server errors (as enum list) to ignore in connections.
QSslSocket::PeerVerifyMode sslPeerVerifyMode() const
SSL client's peer verify mode to use in connections.
const QSslCertificate sslCertificate() const
Server certificate object.
const QString sslHostPort() const
Server host:port string.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, etc. certificate.
static QString redTextStyleSheet(const QString &selector="*")
Red text stylesheet representing invalid, untrusted, etc. certificate.
static QString orangeTextStyleSheet(const QString &selector="*")
Orange text stylesheet representing loaded component, but not stored in database.
static void setItemBold(QTreeWidgetItem *item)
Call setFirstColumnSpanned(true) on the item and make its font bold.
QgsAuthSslConfigDialog(QWidget *parent=nullptr, const QSslCertificate &cert=QSslCertificate(), const QString &hostport=QString())
Construct wrapper dialog for the SSL config widget.
#define QgsDebugError(str)