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