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