QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgscredentials.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscredentials.h - interface for requesting credentials
3  ----------------------
4  begin : February 2010
5  copyright : (C) 2010 by Juergen E. Fischer
6  email : jef at norbit dot de
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 
17 #ifndef QGSCREDENTIALS_H
18 #define QGSCREDENTIALS_H
19 
20 #include <QMap>
21 #include <QMutex>
22 #include <QObject>
23 #include <QPair>
24 #include <QString>
25 
26 #include "qgis_core.h"
27 #include "qgis_sip.h"
28 
44 class CORE_EXPORT QgsCredentials
45 {
46  public:
47 
51  virtual ~QgsCredentials() = default;
52 
68  bool get( const QString &realm, QString &username SIP_INOUT, QString &password SIP_INOUT, const QString &message = QString() );
69 
79  void put( const QString &realm, const QString &username, const QString &password );
80 
81  bool getMasterPassword( QString &password SIP_INOUT, bool stored = false );
82 
84  static QgsCredentials *instance();
85 
92  void lock();
93 
98  void unlock();
99 
104  QMutex *mutex() { return &mAuthMutex; }
105 
106  protected:
107 
111  QgsCredentials() = default;
112 
114  virtual bool request( const QString &realm, QString &username SIP_INOUT, QString &password SIP_INOUT, const QString &message = QString() ) = 0;
115 
117  virtual bool requestMasterPassword( QString &password SIP_INOUT, bool stored = false ) = 0;
118 
120  void setInstance( QgsCredentials *instance );
121 
122  private:
123  Q_DISABLE_COPY( QgsCredentials )
124 
125 #ifdef SIP_RUN
126  QgsCredentials( const QgsCredentials & );
127 #endif
128 
130  QMap< QString, QPair<QString, QString> > mCredentialCache;
131 
133  static QgsCredentials *sInstance;
134 
136  QMutex mAuthMutex;
137 
139  QMutex mCacheMutex;
140 };
141 
142 
149 class CORE_EXPORT QgsCredentialsNone : public QObject, public QgsCredentials
150 {
151  Q_OBJECT
152 
153  public:
155 
156  signals:
158  void destroyed();
159 
160  protected:
161  bool request( const QString &realm, QString &username SIP_INOUT, QString &password SIP_INOUT, const QString &message = QString() ) override;
162  bool requestMasterPassword( QString &password SIP_INOUT, bool stored = false ) override;
163 };
164 
165 
174 class CORE_EXPORT QgsCredentialsConsole : public QObject, public QgsCredentials
175 {
176  Q_OBJECT
177 
178  public:
180 
181  signals:
183  void destroyed();
184 
185  protected:
186  bool request( const QString &realm, QString &username SIP_INOUT, QString &password SIP_INOUT, const QString &message = QString() ) override;
187  bool requestMasterPassword( QString &password SIP_INOUT, bool stored = false ) override;
188 };
189 
190 #endif
Interface for requesting credentials in QGIS in GUI independent way.
virtual bool requestMasterPassword(QString &password, bool stored=false)=0
request a master password
#define SIP_INOUT
Definition: qgis_sip.h:71
Default implementation of credentials interface.
virtual bool request(const QString &realm, QString &username, QString &password, const QString &message=QString())=0
request a password
Implementation of credentials interface for the console.
QMutex * mutex()
Returns pointer to mutex.