20 #include <QFileDialog>
22 #include <QPushButton>
26 #include "qgssettings.h"
43 mAuthNotifyLayout =
new QVBoxLayout;
44 this->setLayout( mAuthNotifyLayout );
46 mAuthNotifyLayout->addWidget( mAuthNotify );
51 connect( btnImportFile, &QToolButton::clicked,
this, &QgsAuthImportCertDialog::btnImportFile_clicked );
52 connect( chkAllowInvalid, &QCheckBox::toggled,
this, &QgsAuthImportCertDialog::chkAllowInvalid_toggled );
54 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
55 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
57 connect( teCertText, &QPlainTextEdit::textChanged,
this, &QgsAuthImportCertDialog::validateCertificates );
59 connect( radioImportFile, &QAbstractButton::toggled,
this, &QgsAuthImportCertDialog::updateGui );
60 connect( radioImportText, &QAbstractButton::toggled,
this, &QgsAuthImportCertDialog::updateGui );
65 radioImportText->setHidden(
true );
66 teCertText->setHidden(
true );
70 radioImportFile->setHidden(
true );
71 frameImportFile->setHidden(
true );
74 radioImportFile->setChecked(
true );
79 grpbxImportCert->setTitle( tr(
"Import Certificate Authorities" ) );
82 okButton()->setText( tr(
"Import" ) );
83 okButton()->setEnabled(
false );
84 teValidation->setFocus();
92 return QList<QSslCertificate>();
103 if ( !radioImportFile->isChecked() )
106 return leImportFile->text();
115 if ( !radioImportText->isChecked() )
118 return teCertText->toPlainText().trimmed();
127 return chkAllowInvalid->isChecked();
136 return cmbbxTrust->trustPolicy();
139 void QgsAuthImportCertDialog::updateGui()
141 frameImportFile->setEnabled( radioImportFile->isChecked() );
142 teCertText->setEnabled( radioImportText->isChecked() );
143 validateCertificates();
146 void QgsAuthImportCertDialog::validateCertificates()
149 teValidation->clear();
150 teValidation->setStyleSheet( QString() );
153 QList<QSslCertificate> certs;
154 QList<QSslCertificate> nixcerts;
156 bool allowinvalid = chkAllowInvalid->isChecked();
157 bool filterCAs = ( mFilter ==
CaFilter );
160 if ( radioImportFile->isChecked() && !leImportFile->text().isEmpty() )
164 else if ( radioImportText->isChecked() && !teCertText->toPlainText().trimmed().isEmpty() )
169 int certssize = certs.size();
171 const auto constCerts = certs;
172 for (
const QSslCertificate &cert : constCerts )
190 valid = ( certssize > 0
191 && ( allowinvalid || certssize == validcerts )
192 && ( !filterCAs || nixcerts.size() < certssize ) );
194 if ( !nixcerts.isEmpty() )
196 const auto constNixcerts = nixcerts;
197 for (
const QSslCertificate &nixcert : constNixcerts )
199 certs.removeOne( nixcert );
208 teValidation->setStyleSheet(
213 QString msg = tr(
"Certificates found: %1\n"
214 "Certificates valid: %2" ).arg( certssize ).arg( validcerts );
218 msg += tr(
"\nAuthorities/Issuers: %1%2" ).arg( cas )
219 .arg( !nixcerts.isEmpty() && nixcerts.size() < certssize ?
" (others not imported)" :
"" );
222 teValidation->setText( msg );
224 okButton()->setEnabled( valid );
227 void QgsAuthImportCertDialog::btnImportFile_clicked()
229 const QString &fn = getOpenFileName( tr(
"Open Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
232 leImportFile->setText( fn );
234 validateCertificates();
237 void QgsAuthImportCertDialog::chkAllowInvalid_toggled(
bool checked )
240 validateCertificates();
243 QString QgsAuthImportCertDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
245 QgsSettings settings;
246 QString recentdir = settings.value( QStringLiteral(
"UI/lastAuthImportCertOpenFileDir" ), QDir::homePath() ).toString();
247 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
251 this->activateWindow();
255 settings.setValue( QStringLiteral(
"UI/lastAuthImportCertOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
260 QPushButton *QgsAuthImportCertDialog::okButton()
262 return buttonBox->button( QDialogButtonBox::Ok );
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
CertTrustPolicy
Type of certificate trust policy.
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 bool certificateIsAuthorityOrIssuer(const QSslCertificate &cert)
Gets whether a certificate is an Authority or can at least sign other certificates.
static QList< QSslCertificate > certsFromString(const QString &pemtext)
Returns a list of concatenated certs from a PEM Base64 text block.
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.
QgsAuthCertUtils::CertTrustPolicy certTrustPolicy()
Defined trust policy for imported certificates.
const QString certFileToImport()
Gets the file path to a certificate to import.
CertFilter
Type of filter to apply to dialog.
QgsAuthImportCertDialog(QWidget *parent=nullptr, QgsAuthImportCertDialog::CertFilter filter=NoFilter, QgsAuthImportCertDialog::CertInput input=AllInputs)
Construct a dialog for importing certificates.
const QList< QSslCertificate > certificatesToImport()
Gets list of certificate objects to import.
const QString certTextToImport()
Gets certificate text to import.
CertInput
Type of inputs for certificates.
bool allowInvalidCerts()
Whether to allow importation of invalid certificates (so trust policy can be overridden)