33#include "moc_qgsauthimportcertdialog.cpp"
35using namespace Qt::StringLiterals;
45 mAuthNotifyLayout =
new QVBoxLayout;
46 this->setLayout( mAuthNotifyLayout );
48 mAuthNotifyLayout->addWidget( mAuthNotify );
53 connect( btnImportFile, &QToolButton::clicked,
this, &QgsAuthImportCertDialog::btnImportFile_clicked );
54 connect( chkAllowInvalid, &QCheckBox::toggled,
this, &QgsAuthImportCertDialog::chkAllowInvalid_toggled );
56 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
57 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
58 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, [] {
QgsHelp::openHelp( u
"auth_system/auth_workflows.html#authentication-authorities"_s ); } );
59 connect( teCertText, &QPlainTextEdit::textChanged,
this, &QgsAuthImportCertDialog::validateCertificates );
61 connect( radioImportFile, &QAbstractButton::toggled,
this, &QgsAuthImportCertDialog::updateGui );
62 connect( radioImportText, &QAbstractButton::toggled,
this, &QgsAuthImportCertDialog::updateGui );
67 radioImportText->setHidden(
true );
68 teCertText->setHidden(
true );
72 radioImportFile->setHidden(
true );
73 frameImportFile->setHidden(
true );
76 radioImportFile->setChecked(
true );
81 grpbxImportCert->setTitle( tr(
"Import Certificate Authorities" ) );
84 okButton()->setText( tr(
"Import" ) );
85 okButton()->setEnabled(
false );
86 teValidation->setFocus();
94 return QList<QSslCertificate>();
105 if ( !radioImportFile->isChecked() )
108 return leImportFile->text();
117 if ( !radioImportText->isChecked() )
120 return teCertText->toPlainText().trimmed();
129 return chkAllowInvalid->isChecked();
138 return cmbbxTrust->trustPolicy();
141void QgsAuthImportCertDialog::updateGui()
143 frameImportFile->setEnabled( radioImportFile->isChecked() );
144 teCertText->setEnabled( radioImportText->isChecked() );
145 validateCertificates();
148void QgsAuthImportCertDialog::validateCertificates()
151 teValidation->clear();
152 teValidation->setStyleSheet( QString() );
155 QList<QSslCertificate> certs;
156 QList<QSslCertificate> nixcerts;
158 const bool allowinvalid = chkAllowInvalid->isChecked();
159 const bool filterCAs = ( mFilter ==
CaFilter );
162 if ( radioImportFile->isChecked() && !leImportFile->text().isEmpty() )
164 certs = QgsAuthCertUtils::certsFromFile( leImportFile->text() );
166 else if ( radioImportText->isChecked() && !teCertText->toPlainText().trimmed().isEmpty() )
168 certs = QgsAuthCertUtils::certsFromString( teCertText->toPlainText().trimmed() );
171 const int certssize = certs.size();
173 const auto constCerts = certs;
174 for (
const QSslCertificate &cert : constCerts )
176 if ( QgsAuthCertUtils::certIsViable( cert ) )
181 if ( QgsAuthCertUtils::certificateIsAuthorityOrIssuer( cert ) )
192 valid = ( certssize > 0 && ( allowinvalid || certssize == validcerts ) && ( !filterCAs || nixcerts.size() < certssize ) );
194 if ( !nixcerts.isEmpty() )
196 const auto constNixcerts = nixcerts;
197 for (
const QSslCertificate &nixcert : constNixcerts )
199 certs.removeOne( nixcert );
212 "Certificates found: %1\n"
213 "Certificates valid: %2"
220 msg += tr(
"\nAuthorities/Issuers: %1%2" ).arg( cas ).arg( !nixcerts.isEmpty() && nixcerts.size() < certssize ?
" (others not imported)" :
"" );
223 teValidation->setText( msg );
225 okButton()->setEnabled( valid );
228void QgsAuthImportCertDialog::btnImportFile_clicked()
230 const QString &fn = getOpenFileName( tr(
"Open Certificate File" ), tr(
"All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
233 leImportFile->setText( fn );
235 validateCertificates();
238void QgsAuthImportCertDialog::chkAllowInvalid_toggled(
bool checked )
241 validateCertificates();
244QString QgsAuthImportCertDialog::getOpenFileName(
const QString &title,
const QString &extfilter )
246 QgsSettings settings;
247 const QString recentdir = settings.
value( u
"UI/lastAuthImportCertOpenFileDir"_s, QDir::homePath() ).toString();
248 QString f = QFileDialog::getOpenFileName(
this, title, recentdir, extfilter );
252 this->activateWindow();
256 settings.
setValue( u
"UI/lastAuthImportCertOpenFileDir"_s, QFileInfo( f ).absoluteDir().path() );
261QPushButton *QgsAuthImportCertDialog::okButton()
263 return buttonBox->button( QDialogButtonBox::Ok );
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
CertTrustPolicy
Type of certificate trust policy.
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).
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
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.