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