QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
18#include "qgslogger.h"
19
20#include <QIODevice>
21#include <QString>
22#include <QTextStream>
23
24#include "moc_qgscredentials.cpp"
25
26using namespace Qt::StringLiterals;
27
28QgsCredentials *QgsCredentials::sInstance = nullptr;
29
31{
32 if ( sInstance )
33 {
34 QgsDebugError( u"already registered an instance of QgsCredentials"_s );
35 }
36
37 sInstance = instance;
38}
39
41{
42 if ( sInstance )
43 return sInstance;
44
45 return new QgsCredentialsNone();
46}
47
48bool QgsCredentials::get( const QString &realm, QString &username, QString &password, const QString &message, bool requestCredentials )
49{
50 {
51 const QMutexLocker locker( &mCacheMutex );
52 if ( mCredentialCache.contains( realm ) )
53 {
54 const QPair<QString, QString> credentials = mCredentialCache.take( realm );
55 username = credentials.first;
56 password = credentials.second;
57 QgsDebugMsgLevel( u"retrieved realm:%1 username:%2"_s.arg( realm, username ), 2 );
58
59 if ( !password.isNull() )
60 return true;
61 }
62 }
63
64 if ( requestCredentials && request( realm, username, password, message ) )
65 {
66 QgsDebugMsgLevel( u"requested realm:%1 username:%2"_s.arg( realm, username ), 2 );
67 return true;
68 }
69 else
70 {
71 QgsDebugMsgLevel( u"unset realm:%1"_s.arg( realm ), 4 );
72 return false;
73 }
74}
75
76void QgsCredentials::put( const QString &realm, const QString &username, const QString &password )
77{
78 const QMutexLocker locker( &mCacheMutex );
79 QgsDebugMsgLevel( u"inserting realm:%1 username:%2"_s.arg( realm, username ), 2 );
80 mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
81}
82
83bool QgsCredentials::getMasterPassword( QString &password, bool stored )
84{
85 if ( requestMasterPassword( password, stored ) )
86 {
87 QgsDebugMsgLevel( u"requested master password"_s, 2 );
88 return true;
89 }
90 return false;
91}
92
94{
95 mAuthMutex.lock();
96}
97
99{
100 mAuthMutex.unlock();
101}
102
103
105// QgsCredentialsNone
106
111
112bool QgsCredentialsNone::request( const QString &realm, QString &username, QString &password, const QString &message )
113{
114 Q_UNUSED( realm );
115 Q_UNUSED( username );
116 Q_UNUSED( password );
117 Q_UNUSED( message );
118 return false;
119}
120
121bool QgsCredentialsNone::requestMasterPassword( QString &password, bool stored )
122{
123 Q_UNUSED( password );
124 Q_UNUSED( stored );
125 return false;
126}
127
129// QgsCredentialsConsole
130
135
136bool QgsCredentialsConsole::request( const QString &realm, QString &username, QString &password, const QString &message )
137{
138 QTextStream in( stdin, QIODevice::ReadOnly );
139 QTextStream out( stdout, QIODevice::WriteOnly );
140
141 out << "credentials for " << realm << Qt::endl;
142 if ( !message.isEmpty() )
143 out << "message: " << message << Qt::endl;
144 out << "username: ";
145 in >> username;
146 out << "password: ";
147 in >> password;
148
149 return true;
150}
151
152bool QgsCredentialsConsole::requestMasterPassword( QString &password, bool stored )
153{
154 Q_UNUSED( stored );
155
156 QTextStream in( stdin, QIODevice::ReadOnly );
157 QTextStream out( stdout, QIODevice::WriteOnly );
158
159 const QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
160
161 out << msg;
162 in >> password;
163
164 return true;
165}
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 get(const QString &realm, QString &username, QString &password, const QString &message=QString(), bool requestCredentials=true)
Requests credentials for the specified realm.
QgsCredentials()=default
bool getMasterPassword(QString &password, bool stored=false)
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:63
#define QgsDebugError(str)
Definition qgslogger.h:59