QGIS API Documentation  2.12.0-Lyon
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 = 0;
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 QgsCredentialsConsole();
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 // QgsCredentialsConsole
103 
105 {
106  setInstance( this );
107 }
108 
109 bool QgsCredentialsConsole::request( const QString& realm, QString &username, QString &password, const QString& message )
110 {
111  QTextStream in( stdin, QIODevice::ReadOnly );
112  QTextStream out( stdout, QIODevice::WriteOnly );
113 
114  out << "credentials for " << realm << endl;
115  if ( !message.isEmpty() )
116  out << "message: " << message << endl;
117  out << "username: ";
118  in >> username;
119  out << "password: ";
120  in >> password;
121 
122  return true;
123 }
124 
126 {
127  Q_UNUSED( stored );
128 
129  QTextStream in( stdin, QIODevice::ReadOnly );
130  QTextStream out( stdout, QIODevice::WriteOnly );
131 
132  QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
133 
134  out << msg;
135  in >> password;
136 
137  return true;
138 }
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()
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
void lock()
Default implementation of credentials interface.
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