QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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, [] { QgsHelp::openHelp( u"auth_system/auth_workflows.html#authentication-authorities"_s ); } );
59 connect( teCertText, &QPlainTextEdit::textChanged, this, &QgsAuthImportCertDialog::validateCertificates );
60
61 connect( radioImportFile, &QAbstractButton::toggled, this, &QgsAuthImportCertDialog::updateGui );
62 connect( radioImportText, &QAbstractButton::toggled, this, &QgsAuthImportCertDialog::updateGui );
63
64 // hide unused widgets
65 if ( mInput == FileInput )
66 {
67 radioImportText->setHidden( true );
68 teCertText->setHidden( true );
69 }
70 else if ( mInput == TextInput )
71 {
72 radioImportFile->setHidden( true );
73 frameImportFile->setHidden( true );
74 }
75
76 radioImportFile->setChecked( true );
77 updateGui();
78
79 if ( mFilter == CaFilter )
80 {
81 grpbxImportCert->setTitle( tr( "Import Certificate Authorities" ) );
82 }
83
84 okButton()->setText( tr( "Import" ) );
85 okButton()->setEnabled( false );
86 teValidation->setFocus();
87 }
88}
89
91{
92 if ( mDisabled )
93 {
94 return QList<QSslCertificate>();
95 }
96 return mCerts;
97}
98
100{
101 if ( mDisabled )
102 {
103 return QString();
104 }
105 if ( !radioImportFile->isChecked() )
106 return QString();
107
108 return leImportFile->text();
109}
110
112{
113 if ( mDisabled )
114 {
115 return QString();
116 }
117 if ( !radioImportText->isChecked() )
118 return QString();
119
120 return teCertText->toPlainText().trimmed();
121}
122
124{
125 if ( mDisabled )
126 {
127 return false;
128 }
129 return chkAllowInvalid->isChecked();
130}
131
133{
134 if ( mDisabled )
135 {
137 }
138 return cmbbxTrust->trustPolicy();
139}
140
141void QgsAuthImportCertDialog::updateGui()
142{
143 frameImportFile->setEnabled( radioImportFile->isChecked() );
144 teCertText->setEnabled( radioImportText->isChecked() );
145 validateCertificates();
146}
147
148void QgsAuthImportCertDialog::validateCertificates()
149{
150 mCerts.clear();
151 teValidation->clear();
152 teValidation->setStyleSheet( QString() );
153
154 bool valid = false;
155 QList<QSslCertificate> certs;
156 QList<QSslCertificate> nixcerts;
157 int validcerts = 0;
158 const bool allowinvalid = chkAllowInvalid->isChecked();
159 const bool filterCAs = ( mFilter == CaFilter );
160 int cas = 0;
161
162 if ( radioImportFile->isChecked() && !leImportFile->text().isEmpty() )
163 {
164 certs = QgsAuthCertUtils::certsFromFile( leImportFile->text() );
165 }
166 else if ( radioImportText->isChecked() && !teCertText->toPlainText().trimmed().isEmpty() )
167 {
168 certs = QgsAuthCertUtils::certsFromString( teCertText->toPlainText().trimmed() );
169 }
170
171 const int certssize = certs.size();
172
173 const auto constCerts = certs;
174 for ( const QSslCertificate &cert : constCerts )
175 {
176 if ( QgsAuthCertUtils::certIsViable( cert ) )
177 ++validcerts;
178
179 if ( filterCAs )
180 {
181 if ( QgsAuthCertUtils::certificateIsAuthorityOrIssuer( cert ) )
182 {
183 ++cas;
184 }
185 else
186 {
187 nixcerts << cert;
188 }
189 }
190 }
191
192 valid = ( certssize > 0 && ( allowinvalid || certssize == validcerts ) && ( !filterCAs || nixcerts.size() < certssize ) );
193
194 if ( !nixcerts.isEmpty() )
195 {
196 const auto constNixcerts = nixcerts;
197 for ( const QSslCertificate &nixcert : constNixcerts )
198 {
199 certs.removeOne( nixcert );
200 }
201 }
202
203 if ( valid )
204 mCerts = certs;
205
206 if ( certssize > 0 )
207 {
208 teValidation->setStyleSheet( valid ? QgsAuthGuiUtils::greenTextStyleSheet( u"QTextEdit"_s ) : QgsAuthGuiUtils::redTextStyleSheet( u"QTextEdit"_s ) );
209 }
210
211 QString msg = tr(
212 "Certificates found: %1\n"
213 "Certificates valid: %2"
214 )
215 .arg( certssize )
216 .arg( validcerts );
217
218 if ( filterCAs )
219 {
220 msg += tr( "\nAuthorities/Issuers: %1%2" ).arg( cas ).arg( !nixcerts.isEmpty() && nixcerts.size() < certssize ? " (others not imported)" : "" );
221 }
222
223 teValidation->setText( msg );
224
225 okButton()->setEnabled( valid );
226}
227
228void QgsAuthImportCertDialog::btnImportFile_clicked()
229{
230 const QString &fn = getOpenFileName( tr( "Open Certificate File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
231 if ( !fn.isEmpty() )
232 {
233 leImportFile->setText( fn );
234 }
235 validateCertificates();
236}
237
238void QgsAuthImportCertDialog::chkAllowInvalid_toggled( bool checked )
239{
240 Q_UNUSED( checked )
241 validateCertificates();
242}
243
244QString QgsAuthImportCertDialog::getOpenFileName( const QString &title, const QString &extfilter )
245{
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 );
249
250 // return dialog focus on Mac
251 this->raise();
252 this->activateWindow();
253
254 if ( !f.isEmpty() )
255 {
256 settings.setValue( u"UI/lastAuthImportCertOpenFileDir"_s, QFileInfo( f ).absoluteDir().path() );
257 }
258 return f;
259}
260
261QPushButton *QgsAuthImportCertDialog::okButton()
262{
263 return buttonBox->button( QDialogButtonBox::Ok );
264}
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.