20 #include <QDialogButtonBox>
21 #include <QPushButton>
31 static void setItemBold_( QTreeWidgetItem *item )
33 item->setFirstColumnSpanned(
true );
34 QFont secf( item->font( 0 ) );
36 item->setFont( 0, secf );
39 static const QString configFoundText_() {
return QObject::tr(
"Configuration loaded from database" ); }
40 static const QString configNotFoundText_() {
return QObject::tr(
"Configuration not found in database" ); }
43 const QSslCertificate &cert,
44 const QString &hostport,
45 const QList<QSslCertificate> &connectionCAs )
48 , mConnectionCAs( connectionCAs )
53 mAuthNotifyLayout =
new QVBoxLayout;
54 this->setLayout( mAuthNotifyLayout );
56 mAuthNotifyLayout->addWidget( mAuthNotify );
61 connect( btnCertInfo, &QToolButton::clicked,
this, &QgsAuthSslConfigWidget::btnCertInfo_clicked );
69 lblLoadedConfig->setVisible(
false );
70 lblLoadedConfig->clear();
72 connect( leHost, &QLineEdit::textChanged,
97 return grpbxSslConfig;
101 QTreeWidgetItem *QgsAuthSslConfigWidget::addRootItem(
const QString &label )
103 QTreeWidgetItem *item =
new QTreeWidgetItem(
104 QStringList() << label,
105 static_cast<int>( ConfigParent ) );
106 setItemBold_( item );
107 item->setTextAlignment( 0, Qt::AlignVCenter );
108 item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
109 treeSslConfig->insertTopLevelItem( treeSslConfig->topLevelItemCount(), item );
114 void QgsAuthSslConfigWidget::setUpSslConfigTree()
116 treeSslConfig->setColumnCount( 1 );
119 mProtocolItem = addRootItem( tr(
"Protocol" ) );
120 mProtocolCmbBx =
new QComboBox( treeSslConfig );
122 static_cast<int>( QSsl::SecureProtocols ) );
124 static_cast<int>( QSsl::TlsV1SslV3 ) );
126 static_cast<int>( QSsl::TlsV1_0 ) );
128 static_cast<int>( QSsl::SslV3 ) );
130 static_cast<int>( QSsl::SslV2 ) );
131 mProtocolCmbBx->setMaximumWidth( 300 );
132 mProtocolCmbBx->setCurrentIndex( 0 );
133 QTreeWidgetItem *protocolitem =
new QTreeWidgetItem(
135 QStringList() << QString(),
136 static_cast<int>( ConfigItem ) );
137 protocolitem->setFlags( protocolitem->flags() & ~Qt::ItemIsSelectable );
138 treeSslConfig->setItemWidget( protocolitem, 0, mProtocolCmbBx );
139 mProtocolItem->setExpanded(
true );
141 mVerifyModeItem = addRootItem( tr(
"Peer verification" ) );
142 mVerifyPeerCmbBx =
new QComboBox( treeSslConfig );
143 mVerifyPeerCmbBx->addItem( tr(
"Verify Peer Certs" ),
144 static_cast<int>( QSslSocket::VerifyPeer ) );
145 mVerifyPeerCmbBx->addItem( tr(
"Do Not Verify Peer Certs" ),
146 static_cast<int>( QSslSocket::VerifyNone ) );
147 mVerifyPeerCmbBx->setMaximumWidth( 300 );
148 mVerifyPeerCmbBx->setCurrentIndex( 0 );
149 QTreeWidgetItem *peerverifycmbxitem =
new QTreeWidgetItem(
151 QStringList() << QString(),
152 static_cast<int>( ConfigItem ) );
153 peerverifycmbxitem->setFlags( peerverifycmbxitem->flags() & ~Qt::ItemIsSelectable );
154 treeSslConfig->setItemWidget( peerverifycmbxitem, 0, mVerifyPeerCmbBx );
155 mVerifyModeItem->setExpanded(
true );
157 mVerifyDepthItem = addRootItem( tr(
"Peer verification depth (0 = complete cert chain)" ) );
158 mVerifyDepthSpnBx =
new QSpinBox( treeSslConfig );
159 mVerifyDepthSpnBx->setMinimum( 0 );
160 mVerifyDepthSpnBx->setMaximum( 10 );
161 mVerifyDepthSpnBx->setMaximumWidth( 200 );
162 mVerifyDepthSpnBx->setAlignment( Qt::AlignHCenter );
163 QTreeWidgetItem *peerverifyspnbxitem =
new QTreeWidgetItem(
165 QStringList() << QString(),
166 static_cast<int>( ConfigItem ) );
167 peerverifyspnbxitem->setFlags( peerverifyspnbxitem->flags() & ~Qt::ItemIsSelectable );
168 treeSslConfig->setItemWidget( peerverifyspnbxitem, 0, mVerifyDepthSpnBx );
169 mVerifyDepthItem->setExpanded(
true );
171 mIgnoreErrorsItem = addRootItem( tr(
"Ignore errors" ) );
174 for (
int i = 0; i < errenums.size(); i++ )
176 QTreeWidgetItem *item =
new QTreeWidgetItem(
178 QStringList() << errenums.at( i ).second,
179 static_cast<int>( ConfigItem ) );
180 item->setCheckState( 0, Qt::Unchecked );
181 item->setTextAlignment( 0, Qt::AlignVCenter );
182 item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
183 item->setData( 0, Qt::UserRole, errenums.at( i ).first );
185 mIgnoreErrorsItem->setExpanded(
true );
208 return QSslCertificate();
219 return leHost->text();
228 if ( grpbxSslConfig->isCheckable() )
230 grpbxSslConfig->setChecked( enable );
246 if ( !hostport.isEmpty() )
257 lblLoadedConfig->setVisible(
true );
265 lblLoadedConfig->setText( configNotFoundText_() );
281 QgsDebugMsg( QStringLiteral(
"Passed-in SSL custom config is null" ) );
288 QgsDebugMsg( QStringLiteral(
"SSL custom config's cert is null" ) );
300 lblLoadedConfig->setVisible(
true );
301 lblLoadedConfig->setText( configFoundText_() );
312 QgsDebugMsg( QStringLiteral(
"SSL custom config FAILED to store in authentication database" ) );
323 mConnectionCAs.clear();
324 leCommonName->clear();
325 leCommonName->setStyleSheet( QString() );
328 lblLoadedConfig->setVisible(
false );
329 lblLoadedConfig->clear();
340 return QSsl::UnknownProtocol;
342 return ( QSsl::SslProtocol )mProtocolCmbBx->currentData().toInt();
351 int indx( mProtocolCmbBx->findData(
static_cast<int>( protocol ) ) );
352 mProtocolCmbBx->setCurrentIndex( indx );
361 mProtocolCmbBx->setCurrentIndex( 0 );
372 QList<QSslError::SslError> errenums;
373 const auto constErrors = errors;
374 for (
const QSslError &err : constErrors )
376 errenums << err.error();
379 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
381 QTreeWidgetItem *item( mIgnoreErrorsItem->child( i ) );
382 if ( errenums.contains( ( QSslError::SslError )item->data( 0, Qt::UserRole ).toInt() ) )
384 item->setCheckState( 0, Qt::Checked );
395 QList<QSslError> errors;
396 const auto constErrorenums = errorenums;
397 for ( QSslError::SslError errorenum : constErrorenums )
399 errors << QSslError( errorenum );
410 if ( errors.isEmpty() )
417 QList<QSslError::SslError> errenums;
418 const auto constErrors = errors;
419 for (
const QSslError &err : constErrors )
421 errenums << err.error();
424 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
426 QTreeWidgetItem *item( mIgnoreErrorsItem->child( i ) );
427 bool enable( errenums.contains( ( QSslError::SslError )item->data( 0, Qt::UserRole ).toInt() ) );
428 item->setCheckState( 0, enable ? Qt::Checked : Qt::Unchecked );
438 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
440 mIgnoreErrorsItem->child( i )->setCheckState( 0, Qt::Unchecked );
446 QList<QSslError::SslError> errs;
451 for (
int i = 0; i < mIgnoreErrorsItem->childCount(); i++ )
453 QTreeWidgetItem *item( mIgnoreErrorsItem->child( i ) );
454 if ( item->checkState( 0 ) == Qt::Checked )
456 errs.append( ( QSslError::SslError )item->data( 0, Qt::UserRole ).toInt() );
466 return QSslSocket::AutoVerifyPeer;
468 return ( QSslSocket::PeerVerifyMode )mVerifyPeerCmbBx->currentData().toInt();
477 return mVerifyDepthSpnBx->value();
488 int indx( mVerifyPeerCmbBx->findData(
static_cast<int>( mode ) ) );
489 mVerifyPeerCmbBx->setCurrentIndex( indx );
491 mVerifyDepthSpnBx->setValue( modedepth );
500 mVerifyPeerCmbBx->setCurrentIndex( 0 );
501 mVerifyDepthSpnBx->setValue( 0 );
510 bool cansave = ( isEnabled()
511 && ( grpbxSslConfig->isCheckable() ? grpbxSslConfig->isChecked() : true )
512 && validateHostPort( leHost->text() ) );
513 if ( mCanSave != cansave )
527 leHost->setText( host );
530 bool QgsAuthSslConfigWidget::validateHostPort(
const QString &txt )
532 QString hostport( txt );
533 if ( hostport.isEmpty() )
540 QString urlbase( QStringLiteral(
"https://%1" ).arg( hostport ) );
542 return ( !url.host().isEmpty() && QString::number( url.port() ).size() > 0
543 && QStringLiteral(
"https://%1:%2" ).arg( url.host() ).arg( url.port() ) == urlbase );
552 bool valid = validateHostPort( txt );
564 grpbxSslConfig->setCheckable( checkable );
567 grpbxSslConfig->setEnabled(
true );
571 void QgsAuthSslConfigWidget::btnCertInfo_clicked()
573 if ( mCert.isNull() )
579 dlg->setWindowModality( Qt::WindowModal );
580 dlg->resize( 675, 500 );
592 setWindowTitle( tr(
"Custom Certificate Configuration" ) );
593 QVBoxLayout *layout =
new QVBoxLayout(
this );
594 layout->setContentsMargins( 6, 6, 6, 6 );
598 this, &QgsAuthSslConfigDialog::checkCanSave );
599 layout->addWidget( mSslConfigWdgt );
601 QDialogButtonBox *buttonBox =
new QDialogButtonBox(
602 QDialogButtonBox::Close | QDialogButtonBox::Save, Qt::Horizontal,
this );
604 buttonBox->button( QDialogButtonBox::Close )->setDefault(
true );
605 mSaveButton = buttonBox->button( QDialogButtonBox::Save );
606 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QWidget::close );
608 layout->addWidget( buttonBox );
611 mSaveButton->setEnabled( mSslConfigWdgt->
readyToSave() );
620 void QgsAuthSslConfigDialog::checkCanSave(
bool cansave )
622 mSaveButton->setEnabled( cansave );