QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgscredentials.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscredentials.cpp - 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 #include "qgscredentials.h"
17 #include "qgslogger.h"
18 
19 #include <QTextStream>
20 #include <QIODevice>
21 
22 QgsCredentials *QgsCredentials::sInstance = nullptr;
23 
25 {
26  if ( sInstance )
27  {
28  QgsDebugMsg( QStringLiteral( "already registered an instance of QgsCredentials" ) );
29  }
30 
31  sInstance = instance;
32 }
33 
35 {
36  if ( sInstance )
37  return sInstance;
38 
39  return new QgsCredentialsNone();
40 }
41 
42 bool QgsCredentials::get( const QString &realm, QString &username, QString &password, const QString &message )
43 {
44  {
45  QMutexLocker locker( &mCacheMutex );
46  if ( mCredentialCache.contains( realm ) )
47  {
48  QPair<QString, QString> credentials = mCredentialCache.take( realm );
49  username = credentials.first;
50  password = credentials.second;
51  QgsDebugMsgLevel( QStringLiteral( "retrieved realm:%1 username:%2" ).arg( realm, username ), 2 );
52 
53  if ( !password.isNull() )
54  return true;
55  }
56  }
57 
58  if ( request( realm, username, password, message ) )
59  {
60  QgsDebugMsgLevel( QStringLiteral( "requested realm:%1 username:%2" ).arg( realm, username ), 2 );
61  return true;
62  }
63  else
64  {
65  QgsDebugMsgLevel( QStringLiteral( "unset realm:%1" ).arg( realm ), 4 );
66  return false;
67  }
68 }
69 
70 void QgsCredentials::put( const QString &realm, const QString &username, const QString &password )
71 {
72  QMutexLocker locker( &mCacheMutex );
73  QgsDebugMsgLevel( QStringLiteral( "inserting realm:%1 username:%2" ).arg( realm, username ), 2 );
74  mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
75 }
76 
77 bool QgsCredentials::getMasterPassword( QString &password, bool stored )
78 {
79  if ( requestMasterPassword( password, stored ) )
80  {
81  QgsDebugMsgLevel( QStringLiteral( "requested master password" ), 2 );
82  return true;
83  }
84  return false;
85 }
86 
88 {
89  mAuthMutex.lock();
90 }
91 
93 {
94  mAuthMutex.unlock();
95 }
96 
97 
99 // QgsCredentialsNone
100 
102 {
103  setInstance( this );
104 }
105 
106 bool QgsCredentialsNone::request( const QString &realm, QString &username, QString &password, const QString &message )
107 {
108  Q_UNUSED( realm );
109  Q_UNUSED( username );
110  Q_UNUSED( password );
111  Q_UNUSED( message );
112  return false;
113 }
114 
115 bool QgsCredentialsNone::requestMasterPassword( QString &password, bool stored )
116 {
117  Q_UNUSED( password );
118  Q_UNUSED( stored );
119  return false;
120 }
121 
123 // QgsCredentialsConsole
124 
126 {
127  setInstance( this );
128 }
129 
130 bool QgsCredentialsConsole::request( const QString &realm, QString &username, QString &password, const QString &message )
131 {
132  QTextStream in( stdin, QIODevice::ReadOnly );
133  QTextStream out( stdout, QIODevice::WriteOnly );
134 
135 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
136  out << "credentials for " << realm << endl;
137 #else
138  out << "credentials for " << realm << Qt::endl;
139 #endif
140  if ( !message.isEmpty() )
141 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
142  out << "message: " << message << endl;
143 #else
144  out << "message: " << message << Qt::endl;
145 #endif
146  out << "username: ";
147  in >> username;
148  out << "password: ";
149  in >> password;
150 
151  return true;
152 }
153 
154 bool QgsCredentialsConsole::requestMasterPassword( QString &password, bool stored )
155 {
156  Q_UNUSED( stored );
157 
158  QTextStream in( stdin, QIODevice::ReadOnly );
159  QTextStream out( stdout, QIODevice::WriteOnly );
160 
161  QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
162 
163  out << msg;
164  in >> password;
165 
166  return true;
167 }
bool requestMasterPassword(QString &password, bool stored=false) override
request a master password
bool request(const QString &realm, QString &username, QString &password, const QString &message=QString()) override
request a password
Default implementation of credentials interface.
bool requestMasterPassword(QString &password, bool stored=false) override
request a master password
bool request(const QString &realm, QString &username, QString &password, const QString &message=QString()) override
request a password
Interface for requesting credentials in QGIS in GUI independent way.
virtual bool requestMasterPassword(QString &password, bool stored=false)=0
request a master password
void setInstance(QgsCredentials *instance)
register instance
static QgsCredentials * instance()
retrieves instance
void lock()
Lock the instance against access from multiple threads.
bool getMasterPassword(QString &password, bool stored=false)
bool get(const QString &realm, QString &username, QString &password, const QString &message=QString())
Requests credentials for the specified realm.
void unlock()
Unlock the instance after being locked.
void put(const QString &realm, const QString &username, const QString &password)
Stores the correct username and password for the specified realm.
virtual bool request(const QString &realm, QString &username, QString &password, const QString &message=QString())=0
request a password
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugMsg(str)
Definition: qgslogger.h:38