QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsauthimportcertdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthimportcertdialog.cpp
3 ---------------------
4 begin : April 30, 2015
5 copyright : (C) 2015 by Boundless Spatial, Inc. USA
6 author : Larry Shaffer
7 email : lshaffer at boundlessgeo dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgsapplication.h"
20#include "qgsauthcertutils.h"
21#include "qgsauthguiutils.h"
22#include "qgsauthmanager.h"
23#include "qgshelp.h"
24#include "qgssettings.h"
25
26#include <QDir>
27#include <QFileDialog>
28#include <QFileInfo>
29#include <QPushButton>
30#include <QString>
31#include <QtCrypto>
32
33#include "moc_qgsauthimportcertdialog.cpp"
34
35using namespace Qt::StringLiterals;
36
38 : QDialog( parent )
39 , mFilter( filter )
40 , mInput( input )
41{
42 if ( QgsApplication::authManager()->isDisabled() )
43 {
44 mDisabled = true;
45 mAuthNotifyLayout = new QVBoxLayout;
46 this->setLayout( mAuthNotifyLayout );
47 mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
48 mAuthNotifyLayout->addWidget( mAuthNotify );
49 }
50 else
51 {
52 setupUi( this );
53 connect( btnImportFile, &QToolButton::clicked, this, &QgsAuthImportCertDialog::btnImportFile_clicked );
54 connect( chkAllowInvalid, &QCheckBox::toggled, this, &QgsAuthImportCertDialog::chkAllowInvalid_toggled );
55
56 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
57 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
58 connect( buttonBox, &QDialogButtonBox::helpRequested, this, [] {
59 QgsHelp::openHelp( u"auth_system/auth_workflows.html#authentication-authorities"_s );
60 } );
61 connect( teCertText, &QPlainTextEdit::textChanged, this, &QgsAuthImportCertDialog::validateCertificates );
62
63 connect( radioImportFile, &QAbstractButton::toggled, this, &QgsAuthImportCertDialog::updateGui );
64 connect( radioImportText, &QAbstractButton::toggled, this, &QgsAuthImportCertDialog::updateGui );
65
66 // hide unused widgets
67 if ( mInput == FileInput )
68 {
69 radioImportText->setHidden( true );
70 teCertText->setHidden( true );
71 }
72 else if ( mInput == TextInput )
73 {
74 radioImportFile->setHidden( true );
75 frameImportFile->setHidden( true );
76 }
77
78 radioImportFile->setChecked( true );
79 updateGui();
80
81 if ( mFilter == CaFilter )
82 {
83 grpbxImportCert->setTitle( tr( "Import Certificate Authorities" ) );
84 }
85
86 okButton()->setText( tr( "Import" ) );
87 okButton()->setEnabled( false );
88 teValidation->setFocus();
89 }
90}
91
93{
94 if ( mDisabled )
95 {
96 return QList<QSslCertificate>();
97 }
98 return mCerts;
99}
100
102{
103 if ( mDisabled )
104 {
105 return QString();
106 }
107 if ( !radioImportFile->isChecked() )
108 return QString();
109
110 return leImportFile->text();
111}
112
114{
115 if ( mDisabled )
116 {
117 return QString();
118 }
119 if ( !radioImportText->isChecked() )
120 return QString();
121
122 return teCertText->toPlainText().trimmed();
123}
124
126{
127 if ( mDisabled )
128 {
129 return false;
130 }
131 return chkAllowInvalid->isChecked();
132}
133
135{
136 if ( mDisabled )
137 {
139 }
140 return cmbbxTrust->trustPolicy();
141}
142
143void QgsAuthImportCertDialog::updateGui()
144{
145 frameImportFile->setEnabled( radioImportFile->isChecked() );
146 teCertText->setEnabled( radioImportText->isChecked() );
147 validateCertificates();
148}
149
150void QgsAuthImportCertDialog::validateCertificates()
151{
152 mCerts.clear();
153 teValidation->clear();
154 teValidation->setStyleSheet( QString() );
155
156 bool valid = false;
157 QList<QSslCertificate> certs;
158 QList<QSslCertificate> nixcerts;
159 int validcerts = 0;
160 const bool allowinvalid = chkAllowInvalid->isChecked();
161 const bool filterCAs = ( mFilter == CaFilter );
162 int cas = 0;
163
164 if ( radioImportFile->isChecked() && !leImportFile->text().isEmpty() )
165 {
166 certs = QgsAuthCertUtils::certsFromFile( leImportFile->text() );
167 }
168 else if ( radioImportText->isChecked() && !teCertText->toPlainText().trimmed().isEmpty() )
169 {
170 certs = QgsAuthCertUtils::certsFromString( teCertText->toPlainText().trimmed() );
171 }
172
173 const int certssize = certs.size();
174
175 const auto constCerts = certs;
176 for ( const QSslCertificate &cert : constCerts )
177 {
178 if ( QgsAuthCertUtils::certIsViable( cert ) )
179 ++validcerts;
180
181 if ( filterCAs )
182 {
183 if ( QgsAuthCertUtils::certificateIsAuthorityOrIssuer( cert ) )
184 {
185 ++cas;
186 }
187 else
188 {
189 nixcerts << cert;
190 }
191 }
192 }
193
194 valid = ( certssize > 0 && ( allowinvalid || certssize == validcerts ) && ( !filterCAs || nixcerts.size() < certssize ) );
195
196 if ( !nixcerts.isEmpty() )
197 {
198 const auto constNixcerts = nixcerts;
199 for ( const QSslCertificate &nixcert : constNixcerts )
200 {
201 certs.removeOne( nixcert );
202 }
203 }
204
205 if ( valid )
206 mCerts = certs;
207
208 if ( certssize > 0 )
209 {
210 teValidation->setStyleSheet(
211 valid ? QgsAuthGuiUtils::greenTextStyleSheet( u"QTextEdit"_s )
212 : QgsAuthGuiUtils::redTextStyleSheet( u"QTextEdit"_s )
213 );
214 }
215
216 QString msg = tr( "Certificates found: %1\n"
217 "Certificates valid: %2" )
218 .arg( certssize )
219 .arg( validcerts );
220
221 if ( filterCAs )
222 {
223 msg += tr( "\nAuthorities/Issuers: %1%2" ).arg( cas ).arg( !nixcerts.isEmpty() && nixcerts.size() < certssize ? " (others not imported)" : "" );
224 }
225
226 teValidation->setText( msg );
227
228 okButton()->setEnabled( valid );
229}
230
231void QgsAuthImportCertDialog::btnImportFile_clicked()
232{
233 const QString &fn = getOpenFileName( tr( "Open Certificate File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
234 if ( !fn.isEmpty() )
235 {
236 leImportFile->setText( fn );
237 }
238 validateCertificates();
239}
240
241void QgsAuthImportCertDialog::chkAllowInvalid_toggled( bool checked )
242{
243 Q_UNUSED( checked )
244 validateCertificates();
245}
246
247QString QgsAuthImportCertDialog::getOpenFileName( const QString &title, const QString &extfilter )
248{
249 QgsSettings settings;
250 const QString recentdir = settings.value( u"UI/lastAuthImportCertOpenFileDir"_s, QDir::homePath() ).toString();
251 QString f = QFileDialog::getOpenFileName( this, title, recentdir, extfilter );
252
253 // return dialog focus on Mac
254 this->raise();
255 this->activateWindow();
256
257 if ( !f.isEmpty() )
258 {
259 settings.setValue( u"UI/lastAuthImportCertOpenFileDir"_s, QFileInfo( f ).absoluteDir().path() );
260 }
261 return f;
262}
263
264QPushButton *QgsAuthImportCertDialog::okButton()
265{
266 return buttonBox->button( QDialogButtonBox::Ok );
267}
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.
Definition qgshelp.cpp:41
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.