QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  {
44  QMutexLocker locker( &mCacheMutex );
45  if ( mCredentialCache.contains( realm ) )
46  {
47  QPair<QString, QString> credentials = mCredentialCache.take( realm );
48  username = credentials.first;
49  password = credentials.second;
50  QgsDebugMsg( QStringLiteral( "retrieved realm:%1 username:%2" ).arg( realm, username ) );
51 
52  if ( !password.isNull() )
53  return true;
54  }
55  }
56 
57  if ( request( realm, username, password, message ) )
58  {
59  QgsDebugMsg( QStringLiteral( "requested realm:%1 username:%2" ).arg( realm, username ) );
60  return true;
61  }
62  else
63  {
64  QgsDebugMsgLevel( QStringLiteral( "unset realm:%1" ).arg( realm ), 4 );
65  return false;
66  }
67 }
68 
69 void QgsCredentials::put( const QString &realm, const QString &username, const QString &password )
70 {
71  QMutexLocker locker( &mCacheMutex );
72  QgsDebugMsg( QStringLiteral( "inserting realm:%1 username:%2" ).arg( realm, username ) );
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( QStringLiteral( "requested master password" ) );
81  return true;
82  }
83  return false;
84 }
85 
87 {
88  mAuthMutex.lock();
89 }
90 
92 {
93  mAuthMutex.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: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
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
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