QGIS API Documentation  2.17.0-Master (973e4b0)
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::smInstance = nullptr;
22 
24 {
25  if ( smInstance )
26  {
27  QgsDebugMsg( "already registered an instance of QgsCredentials" );
28  }
29 
30  smInstance = theInstance;
31 }
32 
34 {
35  if ( smInstance )
36  return smInstance;
37 
38  return new QgsCredentialsNone();
39 }
40 
42 {
43 }
44 
46 {
47 }
48 
49 bool QgsCredentials::get( const QString& realm, QString &username, QString &password, const QString& message )
50 {
51  if ( mCredentialCache.contains( realm ) )
52  {
53  QPair<QString, QString> credentials = mCredentialCache.take( realm );
54  username = credentials.first;
55  password = credentials.second;
56  QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
57 
58  if ( !password.isNull() )
59  return true;
60  }
61 
62  if ( request( realm, username, password, message ) )
63  {
64  QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
65  return true;
66  }
67  else
68  {
69  QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
70  return false;
71  }
72 }
73 
74 void QgsCredentials::put( const QString& realm, const QString& username, const QString& password )
75 {
76  QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
77  mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
78 }
79 
80 bool QgsCredentials::getMasterPassword( QString &password , bool stored )
81 {
82  if ( requestMasterPassword( password, stored ) )
83  {
84  QgsDebugMsg( "requested master password" );
85  return true;
86  }
87  return false;
88 }
89 
91 {
92  mMutex.lock();
93 }
94 
96 {
97  mMutex.unlock();
98 }
99 
100 
102 // QgsCredentialsNone
103 
105 {
106  setInstance( this );
107 }
108 
109 bool QgsCredentialsNone::request( const QString& realm, QString &username, QString &password, const QString& message )
110 {
111  Q_UNUSED( realm );
112  Q_UNUSED( username );
113  Q_UNUSED( password );
114  Q_UNUSED( message );
115  return false;
116 }
117 
119 {
120  Q_UNUSED( password );
121  Q_UNUSED( stored );
122  return false;
123 }
124 
126 // QgsCredentialsConsole
127 
129 {
130  setInstance( this );
131 }
132 
133 bool QgsCredentialsConsole::request( const QString& realm, QString &username, QString &password, const QString& message )
134 {
135  QTextStream in( stdin, QIODevice::ReadOnly );
136  QTextStream out( stdout, QIODevice::WriteOnly );
137 
138  out << "credentials for " << realm << endl;
139  if ( !message.isEmpty() )
140  out << "message: " << message << endl;
141  out << "username: ";
142  in >> username;
143  out << "password: ";
144  in >> password;
145 
146  return true;
147 }
148 
150 {
151  Q_UNUSED( stored );
152 
153  QTextStream in( stdin, QIODevice::ReadOnly );
154  QTextStream out( stdout, QIODevice::WriteOnly );
155 
156  QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
157 
158  out << msg;
159  in >> password;
160 
161  return true;
162 }
bool getMasterPassword(QString &password, bool stored=false)
bool contains(const Key &key) const
Interface for requesting credentials in QGIS in GUI independent way.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual bool requestMasterPassword(QString &password, bool stored=false) override
request a master password
virtual bool requestMasterPassword(QString &password, bool stored=false)=0
request a master password
static QgsCredentials * instance()
retrieves instance
void unlock()
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.
bool isNull() const
bool isEmpty() const
virtual bool request(const QString &realm, QString &username, QString &password, const QString &message=QString::null) override
request a password
void lock()
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
virtual ~QgsCredentials()
virtual destructor
iterator insert(const Key &key, const T &value)
void unlock()
Unlock the instance after being locked.
void put(const QString &realm, const QString &username, const QString &password)
T take(const Key &key)
void setInstance(QgsCredentials *theInstance)
register instance
virtual bool requestMasterPassword(QString &password, bool stored=false) override
request a master password