QGIS API Documentation  3.6.0-Noosa (5873452)
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  QMutexLocker locker( &mMutex );
44  if ( mCredentialCache.contains( realm ) )
45  {
46  QPair<QString, QString> credentials = mCredentialCache.take( realm );
47  locker.unlock();
48  username = credentials.first;
49  password = credentials.second;
50 #if 0 // don't leak credentials on log
51  QgsDebugMsg( QStringLiteral( "retrieved realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
52 #endif
53 
54  if ( !password.isNull() )
55  return true;
56  }
57  locker.unlock();
58 
59  if ( request( realm, username, password, message ) )
60  {
61 #if 0 // don't leak credentials on log
62  QgsDebugMsg( QStringLiteral( "requested realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
63 #endif
64  return true;
65  }
66  else
67  {
68  QgsDebugMsgLevel( QStringLiteral( "unset realm:%1" ).arg( realm ), 4 );
69  return false;
70  }
71 }
72 
73 void QgsCredentials::put( const QString &realm, const QString &username, const QString &password )
74 {
75 #if 0 // don't leak credentials on log
76  QgsDebugMsg( QStringLiteral( "inserting realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
77 #endif
78  QMutexLocker locker( &mMutex );
79  mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
80 }
81 
82 bool QgsCredentials::getMasterPassword( QString &password, bool stored )
83 {
84  if ( requestMasterPassword( password, stored ) )
85  {
86  QgsDebugMsg( QStringLiteral( "requested master password" ) );
87  return true;
88  }
89  return false;
90 }
91 
93 {
94  mMutex.lock();
95 }
96 
98 {
99  mMutex.unlock();
100 }
101 
102 
104 // QgsCredentialsNone
105 
107 {
108  setInstance( this );
109 }
110 
111 bool QgsCredentialsNone::request( const QString &realm, QString &username, QString &password, const QString &message )
112 {
113  Q_UNUSED( realm );
114  Q_UNUSED( username );
115  Q_UNUSED( password );
116  Q_UNUSED( message );
117  return false;
118 }
119 
120 bool QgsCredentialsNone::requestMasterPassword( QString &password, bool stored )
121 {
122  Q_UNUSED( password );
123  Q_UNUSED( stored );
124  return false;
125 }
126 
128 // QgsCredentialsConsole
129 
131 {
132  setInstance( this );
133 }
134 
135 bool QgsCredentialsConsole::request( const QString &realm, QString &username, QString &password, const QString &message )
136 {
137  QTextStream in( stdin, QIODevice::ReadOnly );
138  QTextStream out( stdout, QIODevice::WriteOnly );
139 
140  out << "credentials for " << realm << endl;
141  if ( !message.isEmpty() )
142  out << "message: " << message << endl;
143  out << "username: ";
144  in >> username;
145  out << "password: ";
146  in >> password;
147 
148  return true;
149 }
150 
151 bool QgsCredentialsConsole::requestMasterPassword( QString &password, bool stored )
152 {
153  Q_UNUSED( stored );
154 
155  QTextStream in( stdin, QIODevice::ReadOnly );
156  QTextStream out( stdout, QIODevice::WriteOnly );
157 
158  QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
159 
160  out << msg;
161  in >> password;
162 
163  return true;
164 }
bool getMasterPassword(QString &password, bool stored=false)
Interface for requesting credentials in QGIS in GUI independent way.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
bool requestMasterPassword(QString &password, bool stored=false) override
request a master password
void setInstance(QgsCredentials *instance)
register instance
virtual bool requestMasterPassword(QString &password, bool stored=false)=0
request a master password
static QgsCredentials * instance()
retrieves instance
Default implementation of credentials interface.
virtual bool request(const QString &realm, QString &username, QString &password, const QString &message=QString())=0
request a password
Q_DECL_DEPRECATED void lock()
Lock the instance against access from multiple threads.
bool request(const QString &realm, QString &username, QString &password, const QString &message=QString()) override
request a password
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
bool get(const QString &realm, QString &username, QString &password, const QString &message=QString())
Requests credentials for the specified realm.
bool request(const QString &realm, QString &username, QString &password, const QString &message=QString()) override
request a password
Q_DECL_DEPRECATED 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.
bool requestMasterPassword(QString &password, bool stored=false) override
request a master password