QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsauthconfig.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsauthconfig.h
3  ---------------------
4  begin : October 5, 2014
5  copyright : (C) 2014 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 
17 #ifndef QGSAUTHCONFIG_H
18 #define QGSAUTHCONFIG_H
19 
20 #include "qgis_core.h"
21 
22 #include <QHash>
23 #include <QString>
24 #include <QDomElement>
25 #include <QDomDocument>
26 
27 #ifndef QT_NO_SSL
28 #include <QSslCertificate>
29 #include <QSslKey>
30 #include <QSslError>
31 #include <QSslSocket>
32 #endif
33 
34 #include "qgis.h"
35 
36 
41 class CORE_EXPORT QgsAuthMethodConfig
42 {
43  public:
44 
50  QgsAuthMethodConfig( const QString &method = QString(), int version = 0 );
51 
53  bool operator==( const QgsAuthMethodConfig &other ) const;
54 
56  bool operator!=( const QgsAuthMethodConfig &other ) const;
57 
62  const QString id() const { return mId; }
64  void setId( const QString &id ) { mId = id; }
65 
67  const QString name() const { return mName; }
69  void setName( const QString &name ) { mName = name; }
70 
72  const QString uri() const { return mUri; }
73  void setUri( const QString &uri ) { mUri = uri; }
74 
76  QString method() const { return mMethod; }
77  void setMethod( const QString &method ) { mMethod = method; }
78 
80  int version() const { return mVersion; }
82  void setVersion( int version ) { mVersion = version; }
83 
88  bool isValid( bool validateid = false ) const;
89 
94  const QString configString() const;
95 
100  void loadConfigString( const QString &configstr );
101 
103  QgsStringMap configMap() const { return mConfigMap; }
104 
109  void setConfigMap( const QgsStringMap &map ) { mConfigMap = map; }
110 
117  void setConfig( const QString &key, const QString &value );
118 
125  void setConfigList( const QString &key, const QStringList &value );
126 
132  int removeConfig( const QString &key );
133 
139  QString config( const QString &key, const QString &defaultvalue = QString() ) const;
140 
145  QStringList configList( const QString &key ) const;
146 
151  bool hasConfig( const QString &key ) const;
152 
154  void clearConfigMap() { mConfigMap.clear(); }
155 
164  static bool uriToResource( const QString &accessurl, QString *resource, bool withpath = false );
165 
173  bool writeXml( QDomElement &parentElement, QDomDocument &document );
174 
180  bool readXml( const QDomElement &element );
181 
182  private:
183  QString mId;
184  QString mName;
185  QString mUri;
186  QString mMethod;
187  int mVersion;
188 
189  QgsStringMap mConfigMap;
190 
191  static const QString CONFIG_SEP;
192  static const QString CONFIG_KEY_SEP;
193  static const QString CONFIG_LIST_SEP;
194 
195  static const int CONFIG_VERSION;
196 };
197 
198 typedef QHash<QString, QgsAuthMethodConfig> QgsAuthMethodConfigsMap;
199 
200 
201 #ifndef QT_NO_SSL
202 
208 class CORE_EXPORT QgsPkiBundle
209 {
210  public:
211 
218  QgsPkiBundle( const QSslCertificate &clientCert = QSslCertificate(),
219  const QSslKey &clientKey = QSslKey(),
220  const QList<QSslCertificate> &caChain = QList<QSslCertificate>() );
221 
229  static const QgsPkiBundle fromPemPaths( const QString &certPath,
230  const QString &keyPath,
231  const QString &keyPass = QString(),
232  const QList<QSslCertificate> &caChain = QList<QSslCertificate>() );
233 
239  static const QgsPkiBundle fromPkcs12Paths( const QString &bundlepath,
240  const QString &bundlepass = QString() );
241 
243  bool isNull() const;
244 
246  bool isValid() const;
247 
249  const QString certId() const;
250 
252  const QSslCertificate clientCert() const { return mCert; }
254  void setClientCert( const QSslCertificate &cert );
255 
257  const QSslKey clientKey() const { return mCertKey; }
259  void setClientKey( const QSslKey &certkey );
260 
262  const QList<QSslCertificate> caChain() const { return mCaChain; }
264  void setCaChain( const QList<QSslCertificate> &cachain ) { mCaChain = cachain; }
265 
266  private:
267  QSslCertificate mCert;
268  QSslKey mCertKey;
269  QList<QSslCertificate> mCaChain;
270 };
271 
272 
277 class CORE_EXPORT QgsPkiConfigBundle
278 {
279  public:
280 
289  const QSslCertificate &cert,
290  const QSslKey &certkey,
291  const QList<QSslCertificate> &cachain = QList<QSslCertificate>( ) );
292 
294  bool isValid();
295 
297  const QgsAuthMethodConfig config() const { return mConfig; }
298 
300  void setConfig( const QgsAuthMethodConfig &config ) { mConfig = config; }
301 
303  const QSslCertificate clientCert() const { return mCert; }
304 
306  void setClientCert( const QSslCertificate &cert ) { mCert = cert; }
307 
309  const QSslKey clientCertKey() const { return mCertKey; }
310 
312  void setClientCertKey( const QSslKey &certkey ) { mCertKey = certkey; }
313 
318  QList<QSslCertificate> caChain() const { return mCaChain; }
319 
324  void setCaChain( const QList<QSslCertificate> &caChain ) { mCaChain = caChain; }
325 
326  private:
327  QgsAuthMethodConfig mConfig;
328  QSslCertificate mCert;
329  QSslKey mCertKey;
330  QList<QSslCertificate> mCaChain;
331 };
332 
333 
334 
335 #ifdef SIP_RUN
336 % MappedType QList<QSslError::SslError>
337 {
338  % TypeHeaderCode
339 #include <QList>
340  % End
341 
342  % ConvertFromTypeCode
343  // Create the list.
344  PyObject *l;
345 
346  if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
347  return NULL;
348 
349  // Set the list elements.
350  QList<QSslError::SslError>::iterator it = sipCpp->begin();
351  for ( int i = 0; it != sipCpp->end(); ++it, ++i )
352  {
353  PyObject *tobj;
354 
355  if ( ( tobj = sipConvertFromEnum( *it, sipType_QSslError_SslError ) ) == NULL )
356  {
357  Py_DECREF( l );
358  return NULL;
359  }
360  PyList_SET_ITEM( l, i, tobj );
361  }
362 
363  return l;
364  % End
365 
366  % ConvertToTypeCode
367  // Check the type if that is all that is required.
368  if ( sipIsErr == NULL )
369  return PyList_Check( sipPy );
370 
371  QList<QSslError::SslError> *qlist = new QList<QSslError::SslError>;
372 
373  for ( int i = 0; i < PyList_GET_SIZE( sipPy ); ++i )
374  {
375  *qlist << ( QSslError::SslError )SIPLong_AsLong( PyList_GET_ITEM( sipPy, i ) );
376  }
377 
378  *sipCppPtr = qlist;
379  return sipGetState( sipTransferObj );
380  % End
381 };
382 #endif
383 
384 
385 
390 class CORE_EXPORT QgsAuthConfigSslServer
391 {
392  public:
395 
397  const QSslCertificate sslCertificate() const { return mSslCert; }
399  void setSslCertificate( const QSslCertificate &cert ) { mSslCert = cert; }
400 
402  const QString sslHostPort() const { return mSslHostPort; }
404  void setSslHostPort( const QString &hostport ) { mSslHostPort = hostport; }
405 
407  QSsl::SslProtocol sslProtocol() const { return mSslProtocol; }
409  void setSslProtocol( QSsl::SslProtocol protocol ) { mSslProtocol = protocol; }
410 
412  const QList<QSslError> sslIgnoredErrors() const;
414  const QList<QSslError::SslError> sslIgnoredErrorEnums() const { return mSslIgnoredErrors; }
416  void setSslIgnoredErrorEnums( const QList<QSslError::SslError> &errors ) { mSslIgnoredErrors = errors; }
417 
419  QSslSocket::PeerVerifyMode sslPeerVerifyMode() const { return mSslPeerVerifyMode; }
421  void setSslPeerVerifyMode( QSslSocket::PeerVerifyMode mode ) { mSslPeerVerifyMode = mode; }
422 
427  int sslPeerVerifyDepth() const { return mSslPeerVerifyDepth; }
428 
433  void setSslPeerVerifyDepth( int depth ) { mSslPeerVerifyDepth = depth; }
434 
436  int version() const { return mVersion; }
438  void setVersion( int version ) { mVersion = version; }
439 
441  int qtVersion() const { return mQtVersion; }
443  void setQtVersion( int version ) { mQtVersion = version; }
444 
446  const QString configString() const;
448  void loadConfigString( const QString &config = QString() );
449 
451  bool isNull() const;
452 
453  private:
454 
455  QString mSslHostPort;
456  QSslCertificate mSslCert;
457 
458  QSsl::SslProtocol mSslProtocol;
459  int mQtVersion;
460  QList<QSslError::SslError> mSslIgnoredErrors;
461  QSslSocket::PeerVerifyMode mSslPeerVerifyMode = QSslSocket::VerifyPeer;
462  int mSslPeerVerifyDepth = 0;
463  int mVersion = 1;
464 
465  static const QString CONF_SEP;
466 };
467 #endif
468 
469 #endif // QGSAUTHCONFIG_H
Configuration container for SSL server connection exceptions or overrides.
void setSslProtocol(QSsl::SslProtocol protocol)
Sets SSL server protocol to use in connections.
void setSslCertificate(const QSslCertificate &cert)
Sets server certificate object.
void setSslHostPort(const QString &hostport)
Sets server host:port string.
int version() const
Version of the configuration (used for future upgrading)
QSsl::SslProtocol sslProtocol() const
SSL server protocol to use in connections.
void setSslPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
Sets SSL client's peer verify mode to use in connections.
void setVersion(int version)
Sets version of the configuration (used for future upgrading)
void setSslPeerVerifyDepth(int depth)
Set number or SSL client's peer to verify in connections.
const QList< QSslError::SslError > sslIgnoredErrorEnums() const
SSL server errors (as enum list) to ignore in connections.
int sslPeerVerifyDepth() const
Number or SSL client's peer to verify in connections.
void setSslIgnoredErrorEnums(const QList< QSslError::SslError > &errors)
Sets SSL server errors (as enum list) to ignore in connections.
int qtVersion() const
Qt version when the configuration was made (SSL protocols may differ)
QSslSocket::PeerVerifyMode sslPeerVerifyMode() const
SSL client's peer verify mode to use in connections.
const QSslCertificate sslCertificate() const
Server certificate object.
const QString sslHostPort() const
Server host:port string.
void setQtVersion(int version)
Sets Qt version when the configuration was made (SSL protocols may differ)
Configuration storage class for authentication method configurations.
Definition: qgsauthconfig.h:42
QString method() const
Textual key of the associated authentication method.
Definition: qgsauthconfig.h:76
const QString uri() const
A URI to auto-select a config when connecting to a resource.
Definition: qgsauthconfig.h:72
void setName(const QString &name)
Sets name of configuration.
Definition: qgsauthconfig.h:69
void setConfigMap(const QgsStringMap &map)
Set extended configuration map.
void setVersion(int version)
Sets version of the configuration.
Definition: qgsauthconfig.h:82
const QString name() const
Gets name of configuration.
Definition: qgsauthconfig.h:67
const QString id() const
Gets 'authcfg' 7-character alphanumeric ID of the config.
Definition: qgsauthconfig.h:62
void clearConfigMap()
Clear all configs.
int version() const
Gets version of the configuration.
Definition: qgsauthconfig.h:80
QgsStringMap configMap() const
Gets extended configuration, mapped to key/value pairs of QStrings.
void setMethod(const QString &method)
Definition: qgsauthconfig.h:77
void setUri(const QString &uri)
Definition: qgsauthconfig.h:73
void setId(const QString &id)
Sets auth config ID.
Definition: qgsauthconfig.h:64
Storage set for PKI bundle: SSL certificate, key, optional CA cert chain.
const QSslKey clientKey() const
Private key object.
void setCaChain(const QList< QSslCertificate > &cachain)
Sets chain of Certificate Authorities for client certificate.
const QList< QSslCertificate > caChain() const
Chain of Certificate Authorities for client certificate.
const QSslCertificate clientCert() const
Client certificate object.
Storage set for constructed SSL certificate, key, associated with an authentication config.
void setClientCertKey(const QSslKey &certkey)
Sets private key object.
QList< QSslCertificate > caChain() const
caChain return the CA chain
const QSslCertificate clientCert() const
Client certificate object.
const QgsAuthMethodConfig config() const
Authentication method configuration.
void setConfig(const QgsAuthMethodConfig &config)
Sets authentication method configuration.
const QSslKey clientCertKey() const
Private key object.
void setClientCert(const QSslCertificate &cert)
Sets client certificate object.
void setCaChain(const QList< QSslCertificate > &caChain)
setCaChain set the CA chain
QMap< QString, QString > QgsStringMap
Definition: qgis.h:1041
QHash< QString, QgsAuthMethodConfig > QgsAuthMethodConfigsMap
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)