17#include "ui_qgsauthimportidentitydialog.h"
33#include "moc_qgsauthimportidentitydialog.cpp"
42 mAuthNotifyLayout =
new QVBoxLayout;
43 this->setLayout( mAuthNotifyLayout );
45 mAuthNotifyLayout->addWidget( mAuthNotify );
50 connect( lePkiPathsKeyPass, &QLineEdit::textChanged,
this, &QgsAuthImportIdentityDialog::lePkiPathsKeyPass_textChanged );
51 connect( chkPkiPathsPassShow, &QCheckBox::stateChanged,
this, &QgsAuthImportIdentityDialog::chkPkiPathsPassShow_stateChanged );
52 connect( btnPkiPathsCert, &QToolButton::clicked,
this, &QgsAuthImportIdentityDialog::btnPkiPathsCert_clicked );
53 connect( btnPkiPathsKey, &QToolButton::clicked,
this, &QgsAuthImportIdentityDialog::btnPkiPathsKey_clicked );
54 connect( lePkiPkcs12KeyPass, &QLineEdit::textChanged,
this, &QgsAuthImportIdentityDialog::lePkiPkcs12KeyPass_textChanged );
55 connect( chkPkiPkcs12PassShow, &QCheckBox::stateChanged,
this, &QgsAuthImportIdentityDialog::chkPkiPkcs12PassShow_stateChanged );
56 connect( btnPkiPkcs12Bundle, &QToolButton::clicked,
this, &QgsAuthImportIdentityDialog::btnPkiPkcs12Bundle_clicked );
57 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QWidget::close );
58 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
59 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, [] {
60 QgsHelp::openHelp( QStringLiteral(
"auth_system/auth_workflows.html#authentication-identities" ) );
62 mIdentityType = identitytype;
64 populateIdentityType();
81 return qMakePair( QSslCertificate(), QSslKey() );
86void QgsAuthImportIdentityDialog::populateIdentityType()
90 stkwBundleType->setVisible(
true );
95 connect( cmbIdentityTypes,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ), stkwBundleType, &QStackedWidget::setCurrentIndex );
96 connect( stkwBundleType, &QStackedWidget::currentChanged, cmbIdentityTypes, &QComboBox::setCurrentIndex );
98 connect( cmbIdentityTypes,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, [
this] { validateIdentity(); } );
99 connect( stkwBundleType, &QStackedWidget::currentChanged,
this, &QgsAuthImportIdentityDialog::validateIdentity );
101 cmbIdentityTypes->setCurrentIndex( 0 );
102 stkwBundleType->setCurrentIndex( 0 );
108void QgsAuthImportIdentityDialog::validateIdentity()
113 ok = validateBundle();
115 okButton()->setEnabled( ok );
118bool QgsAuthImportIdentityDialog::validateBundle()
121 const QSslCertificate emptycert;
122 const QSslKey emptykey;
123 mCertBundle = qMakePair( emptycert, emptykey );
124 mPkiBundle = QgsPkiBundle();
126 QWidget *curpage = stkwBundleType->currentWidget();
127 if ( curpage == pagePkiPaths )
129 return validatePkiPaths();
131 else if ( curpage == pagePkiPkcs12 )
133 return validatePkiPkcs12();
139void QgsAuthImportIdentityDialog::clearValidation()
141 teValidation->clear();
142 teValidation->setStyleSheet( QString() );
153 txt = tr(
"Valid: %1" ).arg( msg );
157 txt = tr(
"Invalid: %1" ).arg( msg );
162 teValidation->setStyleSheet( ss );
165 teValidation->append( txt );
169 teValidation->setText( txt );
171 teValidation->moveCursor( QTextCursor::Start );
174void QgsAuthImportIdentityDialog::lePkiPathsKeyPass_textChanged(
const QString &pass )
180void QgsAuthImportIdentityDialog::chkPkiPathsPassShow_stateChanged(
int state )
182 lePkiPathsKeyPass->setEchoMode( ( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
185void QgsAuthImportIdentityDialog::btnPkiPathsCert_clicked()
187 const QString &fn = getOpenFileName( tr(
"Open Client Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
190 lePkiPathsCert->setText( fn );
195void QgsAuthImportIdentityDialog::btnPkiPathsKey_clicked()
197 const QString &fn = getOpenFileName( tr(
"Open Private Key File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
200 lePkiPathsKey->setText( fn );
205void QgsAuthImportIdentityDialog::lePkiPkcs12KeyPass_textChanged(
const QString &pass )
211void QgsAuthImportIdentityDialog::chkPkiPkcs12PassShow_stateChanged(
int state )
213 lePkiPkcs12KeyPass->setEchoMode( ( state > 0 ) ? QLineEdit::Normal : QLineEdit::Password );
216void QgsAuthImportIdentityDialog::btnPkiPkcs12Bundle_clicked()
218 const QString &fn = getOpenFileName( tr(
"Open PKCS#12 Certificate Bundle" ), tr(
"PKCS#12 (*.p12 *.pfx)" ) );
221 lePkiPkcs12Bundle->setText( fn );
226bool QgsAuthImportIdentityDialog::validatePkiPaths()
228 bool isvalid =
false;
231 const QString certpath( lePkiPathsCert->text() );
232 const QString keypath( lePkiPathsKey->text() );
234 const bool certfound = QFile::exists( certpath );
235 const bool keyfound = QFile::exists( keypath );
237 fileFound( certpath.isEmpty() || certfound, lePkiPathsCert );
238 fileFound( keypath.isEmpty() || keyfound, lePkiPathsKey );
240 if ( !certfound || !keyfound )
242 writeValidation( tr(
"Missing components" ),
Invalid );
247 QSslCertificate clientcert;
249 QList<QSslCertificate> ca_certs;
250 if ( !certs.isEmpty() )
252 clientcert = certs.takeFirst();
256 writeValidation( tr(
"Failed to read client certificate from file" ),
Invalid );
260 if ( clientcert.isNull() )
262 writeValidation( tr(
"Failed to load client certificate from file" ),
Invalid );
266 if ( !certs.isEmpty() )
268 teValidation->append( tr(
"Extra certificates found with identity" ) );
274 const QDateTime startdate( clientcert.effectiveDate() );
275 const QDateTime enddate( clientcert.expiryDate() );
281 const QString keypass( lePkiPathsKeyPass->text() );
283 if ( clientkey.isNull() )
285 writeValidation( tr(
"Failed to load client private key from file" ),
Invalid,
true );
286 if ( !keypass.isEmpty() )
288 writeValidation( tr(
"Private key password may not match" ),
Invalid,
true );
295 mCertBundle = qMakePair( clientcert, clientkey );
296 mPkiBundle = QgsPkiBundle( clientcert, clientkey, ca_certs );
302bool QgsAuthImportIdentityDialog::validatePkiPkcs12()
305 const QString bundlepath( lePkiPkcs12Bundle->text() );
306 const bool bundlefound = QFile::exists( bundlepath );
307 fileFound( bundlepath.isEmpty() || bundlefound, lePkiPkcs12Bundle );
311 writeValidation( tr(
"Missing components" ),
Invalid );
315 if ( !QCA::isSupported(
"pkcs12" ) )
317 writeValidation( tr(
"QCA library has no PKCS#12 support" ),
Invalid );
322 QCA::SecureArray passarray;
323 QString keypass = QString();
324 if ( !lePkiPkcs12KeyPass->text().isEmpty() )
326 passarray = QCA::SecureArray( lePkiPkcs12KeyPass->text().toUtf8() );
327 keypass = lePkiPkcs12KeyPass->text();
330 QCA::ConvertResult res;
331 const QCA::KeyBundle bundle( QCA::KeyBundle::fromFile( bundlepath, passarray, &res, QStringLiteral(
"qca-ossl" ) ) );
333 if ( res == QCA::ErrorFile )
335 writeValidation( tr(
"Failed to read bundle file" ),
Invalid );
338 else if ( res == QCA::ErrorPassphrase )
340 writeValidation( tr(
"Incorrect bundle password" ),
Invalid );
341 lePkiPkcs12KeyPass->setPlaceholderText( QStringLiteral(
"Required passphrase" ) );
344 else if ( res == QCA::ErrorDecode )
346 writeValidation( tr(
"Failed to decode (try entering password)" ),
Invalid );
350 if ( bundle.isNull() )
352 writeValidation( tr(
"Bundle empty or can not be loaded" ),
Invalid );
357 const QCA::Certificate cert( bundle.certificateChain().primary() );
360 writeValidation( tr(
"Bundle client cert can not be loaded" ),
Invalid );
365 const QDateTime startdate( cert.notValidBefore() );
366 const QDateTime enddate( cert.notValidAfter() );
367 const QDateTime now( QDateTime::currentDateTime() );
368 const bool bundlevalid = ( now >= startdate && now <= enddate );
370 writeValidation( tr(
"%1 thru %2" ).arg( startdate.toString(), enddate.toString() ), ( bundlevalid ?
Valid :
Invalid ) );
374 QSslCertificate clientcert;
376 if ( !certs.isEmpty() )
378 clientcert = certs.first();
380 if ( clientcert.isNull() )
382 writeValidation( tr(
"Qt cert could not be created from QCA cert" ),
Invalid,
true );
386 clientkey = QSslKey( bundle.privateKey().toRSA().toPEM().toLatin1(), QSsl::Rsa );
387 if ( clientkey.isNull() )
389 writeValidation( tr(
"Qt private key could not be created from QCA key" ),
Invalid,
true );
393 const QCA::CertificateChain cert_chain( bundle.certificateChain() );
394 QList<QSslCertificate> ca_certs;
395 if ( cert_chain.size() > 1 )
397 const auto constCert_chain = cert_chain;
398 for (
const QCA::Certificate &ca_cert : constCert_chain )
400 if ( ca_cert != cert_chain.primary() )
402 ca_certs << QSslCertificate( ca_cert.toPEM().toLatin1() );
407 mCertBundle = qMakePair( clientcert, clientkey );
408 mPkiBundle = QgsPkiBundle( clientcert, clientkey, ca_certs );
414void QgsAuthImportIdentityDialog::fileFound(
bool found, QWidget *widget )
419 widget->setToolTip( tr(
"File not found" ) );
423 widget->setStyleSheet( QString() );
424 widget->setToolTip( QString() );
428QString QgsAuthImportIdentityDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
430 QgsSettings settings;
431 const QString recentdir = settings.
value( QStringLiteral(
"UI/lastAuthImportBundleOpenFileDir" ), QDir::homePath() ).toString();
432 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
436 this->activateWindow();
440 settings.
setValue( QStringLiteral(
"UI/lastAuthImportBundleOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
445QPushButton *QgsAuthImportIdentityDialog::okButton()
447 return buttonBox->button( QDialogButtonBox::Ok );
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static bool certIsViable(const QSslCertificate &cert)
certIsViable checks for viability errors of cert and whether it is NULL
static QList< QSslCertificate > certsFromFile(const QString &certspath)
Returns a list of concatenated certs from a PEM or DER formatted file.
static QSslKey keyFromFile(const QString &keypath, const QString &keypass=QString(), QString *algtype=nullptr)
Returns non-encrypted key from a PEM or DER formatted file.
static QList< QSslCertificate > certsFromString(const QString &pemtext)
Returns a list of concatenated certs from a PEM Base64 text block.
static bool certIsCurrent(const QSslCertificate &cert)
certIsCurrent checks if cert is viable for its not before and not after dates
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.
const QPair< QSslCertificate, QSslKey > certBundleToImport()
Gets certificate/key bundle to be imported.
Validity
Type of certificate/bundle validity output.
IdentityType
Type of identity being imported.
QgsAuthImportIdentityDialog::IdentityType identityType() const
Gets identity type.
QgsAuthImportIdentityDialog(QgsAuthImportIdentityDialog::IdentityType identitytype, QWidget *parent=nullptr)
Construct a dialog for importing identities.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Storage set for PKI bundle: SSL certificate, key, optional CA cert chain.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.