QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 "qgslogger.h"
18
19#include <QTextStream>
20#include <QIODevice>
21
22QgsCredentials *QgsCredentials::sInstance = nullptr;
23
25{
26 if ( sInstance )
27 {
28 QgsDebugError( 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
42bool QgsCredentials::get( const QString &realm, QString &username, QString &password, const QString &message, bool requestCredentials )
43{
44 {
45 const QMutexLocker locker( &mCacheMutex );
46 if ( mCredentialCache.contains( realm ) )
47 {
48 const 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 ( requestCredentials && 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
70void QgsCredentials::put( const QString &realm, const QString &username, const QString &password )
71{
72 const 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
77bool 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
105
106bool 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
115bool QgsCredentialsNone::requestMasterPassword( QString &password, bool stored )
116{
117 Q_UNUSED( password );
118 Q_UNUSED( stored );
119 return false;
120}
121
123// QgsCredentialsConsole
124
129
130bool 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 out << "credentials for " << realm << Qt::endl;
136 if ( !message.isEmpty() )
137 out << "message: " << message << Qt::endl;
138 out << "username: ";
139 in >> username;
140 out << "password: ";
141 in >> password;
142
143 return true;
144}
145
146bool QgsCredentialsConsole::requestMasterPassword( QString &password, bool stored )
147{
148 Q_UNUSED( stored );
149
150 QTextStream in( stdin, QIODevice::ReadOnly );
151 QTextStream out( stdout, QIODevice::WriteOnly );
152
153 const QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
154
155 out << msg;
156 in >> password;
157
158 return true;
159}
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