QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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#include "moc_qgsauthimportcertdialog.cpp"
19
20#include <QDir>
21#include <QFileDialog>
22#include <QFileInfo>
23#include <QPushButton>
24
25#include <QtCrypto>
26
27#include "qgssettings.h"
28#include "qgsauthcertutils.h"
29#include "qgsauthguiutils.h"
30#include "qgsauthmanager.h"
31#include "qgsapplication.h"
32
33
35 : QDialog( parent )
36 , mFilter( filter )
37 , mInput( input )
38{
39 if ( QgsApplication::authManager()->isDisabled() )
40 {
41 mDisabled = true;
42 mAuthNotifyLayout = new QVBoxLayout;
43 this->setLayout( mAuthNotifyLayout );
44 mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
45 mAuthNotifyLayout->addWidget( mAuthNotify );
46 }
47 else
48 {
49 setupUi( this );
50 connect( btnImportFile, &QToolButton::clicked, this, &QgsAuthImportCertDialog::btnImportFile_clicked );
51 connect( chkAllowInvalid, &QCheckBox::toggled, this, &QgsAuthImportCertDialog::chkAllowInvalid_toggled );
52
53 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
54 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
55
56 connect( teCertText, &QPlainTextEdit::textChanged, this, &QgsAuthImportCertDialog::validateCertificates );
57
58 connect( radioImportFile, &QAbstractButton::toggled, this, &QgsAuthImportCertDialog::updateGui );
59 connect( radioImportText, &QAbstractButton::toggled, this, &QgsAuthImportCertDialog::updateGui );
60
61 // hide unused widgets
62 if ( mInput == FileInput )
63 {
64 radioImportText->setHidden( true );
65 teCertText->setHidden( true );
66 }
67 else if ( mInput == TextInput )
68 {
69 radioImportFile->setHidden( true );
70 frameImportFile->setHidden( true );
71 }
72
73 radioImportFile->setChecked( true );
74 updateGui();
75
76 if ( mFilter == CaFilter )
77 {
78 grpbxImportCert->setTitle( tr( "Import Certificate Authorities" ) );
79 }
80
81 okButton()->setText( tr( "Import" ) );
82 okButton()->setEnabled( false );
83 teValidation->setFocus();
84 }
85}
86
88{
89 if ( mDisabled )
90 {
91 return QList<QSslCertificate>();
92 }
93 return mCerts;
94}
95
97{
98 if ( mDisabled )
99 {
100 return QString();
101 }
102 if ( !radioImportFile->isChecked() )
103 return QString();
104
105 return leImportFile->text();
106}
107
109{
110 if ( mDisabled )
111 {
112 return QString();
113 }
114 if ( !radioImportText->isChecked() )
115 return QString();
116
117 return teCertText->toPlainText().trimmed();
118}
119
121{
122 if ( mDisabled )
123 {
124 return false;
125 }
126 return chkAllowInvalid->isChecked();
127}
128
130{
131 if ( mDisabled )
132 {
134 }
135 return cmbbxTrust->trustPolicy();
136}
137
138void QgsAuthImportCertDialog::updateGui()
139{
140 frameImportFile->setEnabled( radioImportFile->isChecked() );
141 teCertText->setEnabled( radioImportText->isChecked() );
142 validateCertificates();
143}
144
145void QgsAuthImportCertDialog::validateCertificates()
146{
147 mCerts.clear();
148 teValidation->clear();
149 teValidation->setStyleSheet( QString() );
150
151 bool valid = false;
152 QList<QSslCertificate> certs;
153 QList<QSslCertificate> nixcerts;
154 int validcerts = 0;
155 const bool allowinvalid = chkAllowInvalid->isChecked();
156 const bool filterCAs = ( mFilter == CaFilter );
157 int cas = 0;
158
159 if ( radioImportFile->isChecked() && !leImportFile->text().isEmpty() )
160 {
161 certs = QgsAuthCertUtils::certsFromFile( leImportFile->text() );
162 }
163 else if ( radioImportText->isChecked() && !teCertText->toPlainText().trimmed().isEmpty() )
164 {
165 certs = QgsAuthCertUtils::certsFromString( teCertText->toPlainText().trimmed() );
166 }
167
168 const int certssize = certs.size();
169
170 const auto constCerts = certs;
171 for ( const QSslCertificate &cert : constCerts )
172 {
173 if ( QgsAuthCertUtils::certIsViable( cert ) )
174 ++validcerts;
175
176 if ( filterCAs )
177 {
179 {
180 ++cas;
181 }
182 else
183 {
184 nixcerts << cert;
185 }
186 }
187 }
188
189 valid = ( certssize > 0 && ( allowinvalid || certssize == validcerts ) && ( !filterCAs || nixcerts.size() < certssize ) );
190
191 if ( !nixcerts.isEmpty() )
192 {
193 const auto constNixcerts = nixcerts;
194 for ( const QSslCertificate &nixcert : constNixcerts )
195 {
196 certs.removeOne( nixcert );
197 }
198 }
199
200 if ( valid )
201 mCerts = certs;
202
203 if ( certssize > 0 )
204 {
205 teValidation->setStyleSheet(
206 valid ? QgsAuthGuiUtils::greenTextStyleSheet( QStringLiteral( "QTextEdit" ) )
207 : QgsAuthGuiUtils::redTextStyleSheet( QStringLiteral( "QTextEdit" ) )
208 );
209 }
210
211 QString msg = tr( "Certificates found: %1\n"
212 "Certificates valid: %2" )
213 .arg( certssize )
214 .arg( validcerts );
215
216 if ( filterCAs )
217 {
218 msg += tr( "\nAuthorities/Issuers: %1%2" ).arg( cas ).arg( !nixcerts.isEmpty() && nixcerts.size() < certssize ? " (others not imported)" : "" );
219 }
220
221 teValidation->setText( msg );
222
223 okButton()->setEnabled( valid );
224}
225
226void QgsAuthImportCertDialog::btnImportFile_clicked()
227{
228 const QString &fn = getOpenFileName( tr( "Open Certificate File" ), tr( "All files (*.*);;PEM (*.pem);;DER (*.der)" ) );
229 if ( !fn.isEmpty() )
230 {
231 leImportFile->setText( fn );
232 }
233 validateCertificates();
234}
235
236void QgsAuthImportCertDialog::chkAllowInvalid_toggled( bool checked )
237{
238 Q_UNUSED( checked )
239 validateCertificates();
240}
241
242QString QgsAuthImportCertDialog::getOpenFileName( const QString &title, const QString &extfilter )
243{
244 QgsSettings settings;
245 const QString recentdir = settings.value( QStringLiteral( "UI/lastAuthImportCertOpenFileDir" ), QDir::homePath() ).toString();
246 QString f = QFileDialog::getOpenFileName( this, title, recentdir, extfilter );
247
248 // return dialog focus on Mac
249 this->raise();
250 this->activateWindow();
251
252 if ( !f.isEmpty() )
253 {
254 settings.setValue( QStringLiteral( "UI/lastAuthImportCertOpenFileDir" ), QFileInfo( f ).absoluteDir().path() );
255 }
256 return f;
257}
258
259QPushButton *QgsAuthImportCertDialog::okButton()
260{
261 return buttonBox->button( QDialogButtonBox::Ok );
262}
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.
Utility functions for use by authentication GUI widgets or standalone apps.
static QString greenTextStyleSheet(const QString &selector="*")
Green text stylesheet representing valid, trusted, 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)
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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.