QGIS API Documentation  2.99.0-Master (a18066b)
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( "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 
42 {
43 }
44 
45 bool QgsCredentials::get( const QString& realm, QString &username, QString &password, const QString& message )
46 {
47  if ( mCredentialCache.contains( realm ) )
48  {
49  QPair<QString, QString> credentials = mCredentialCache.take( realm );
50  username = credentials.first;
51  password = credentials.second;
52  QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
53 
54  if ( !password.isNull() )
55  return true;
56  }
57 
58  if ( request( realm, username, password, message ) )
59  {
60  QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
61  return true;
62  }
63  else
64  {
65  QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
66  return false;
67  }
68 }
69 
70 void QgsCredentials::put( const QString& realm, const QString& username, const QString& password )
71 {
72  QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
73  mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
74 }
75 
76 bool QgsCredentials::getMasterPassword( QString &password , bool stored )
77 {
78  if ( requestMasterPassword( password, stored ) )
79  {
80  QgsDebugMsg( "requested master password" );
81  return true;
82  }
83  return false;
84 }
85 
87 {
88  mMutex.lock();
89 }
90 
92 {
93  mMutex.unlock();
94 }
95 
96 
98 // QgsCredentialsNone
99 
101 {
102  setInstance( this );
103 }
104 
105 bool QgsCredentialsNone::request( const QString& realm, QString &username, QString &password, const QString& message )
106 {
107  Q_UNUSED( realm );
108  Q_UNUSED( username );
109  Q_UNUSED( password );
110  Q_UNUSED( message );
111  return false;
112 }
113 
114 bool QgsCredentialsNone::requestMasterPassword( QString &password, bool stored )
115 {
116  Q_UNUSED( password );
117  Q_UNUSED( stored );
118  return false;
119 }
120 
122 // QgsCredentialsConsole
123 
125 {
126  setInstance( this );
127 }
128 
129 bool QgsCredentialsConsole::request( const QString& realm, QString &username, QString &password, const QString& message )
130 {
131  QTextStream in( stdin, QIODevice::ReadOnly );
132  QTextStream out( stdout, QIODevice::WriteOnly );
133 
134  out << "credentials for " << realm << endl;
135  if ( !message.isEmpty() )
136  out << "message: " << message << endl;
137  out << "username: ";
138  in >> username;
139  out << "password: ";
140  in >> password;
141 
142  return true;
143 }
144 
145 bool QgsCredentialsConsole::requestMasterPassword( QString &password, bool stored )
146 {
147  Q_UNUSED( stored );
148 
149  QTextStream in( stdin, QIODevice::ReadOnly );
150  QTextStream out( stdout, QIODevice::WriteOnly );
151 
152  QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
153 
154  out << msg;
155  in >> password;
156 
157  return true;
158 }
bool getMasterPassword(QString &password, bool stored=false)
Interface for requesting credentials in QGIS in GUI independent way.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
virtual 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::null)=0
request a password
void lock()
Lock the instance against access from multiple threads.
virtual bool request(const QString &realm, QString &username, QString &password, const QString &message=QString::null) override
request a password
bool get(const QString &realm, QString &username, QString &password, const QString &message=QString::null)
virtual bool request(const QString &realm, QString &username, QString &password, const QString &message=QString::null) override
request a password
void unlock()
Unlock the instance after being locked.
void put(const QString &realm, const QString &username, const QString &password)
virtual bool requestMasterPassword(QString &password, bool stored=false) override
request a master password