QGIS API Documentation  3.0.2-Girona (307d082)
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 
41 bool QgsCredentials::get( const QString &realm, QString &username, QString &password, const QString &message )
42 {
43  if ( mCredentialCache.contains( realm ) )
44  {
45  QPair<QString, QString> credentials = mCredentialCache.take( realm );
46  username = credentials.first;
47  password = credentials.second;
48  QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
49 
50  if ( !password.isNull() )
51  return true;
52  }
53 
54  if ( request( realm, username, password, message ) )
55  {
56  QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
57  return true;
58  }
59  else
60  {
61  QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
62  return false;
63  }
64 }
65 
66 void QgsCredentials::put( const QString &realm, const QString &username, const QString &password )
67 {
68  QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm, username, password ) );
69  mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
70 }
71 
72 bool QgsCredentials::getMasterPassword( QString &password, bool stored )
73 {
74  if ( requestMasterPassword( password, stored ) )
75  {
76  QgsDebugMsg( "requested master password" );
77  return true;
78  }
79  return false;
80 }
81 
83 {
84  mMutex.lock();
85 }
86 
88 {
89  mMutex.unlock();
90 }
91 
92 
94 // QgsCredentialsNone
95 
97 {
98  setInstance( this );
99 }
100 
101 bool QgsCredentialsNone::request( const QString &realm, QString &username, QString &password, const QString &message )
102 {
103  Q_UNUSED( realm );
104  Q_UNUSED( username );
105  Q_UNUSED( password );
106  Q_UNUSED( message );
107  return false;
108 }
109 
110 bool QgsCredentialsNone::requestMasterPassword( QString &password, bool stored )
111 {
112  Q_UNUSED( password );
113  Q_UNUSED( stored );
114  return false;
115 }
116 
118 // QgsCredentialsConsole
119 
121 {
122  setInstance( this );
123 }
124 
125 bool QgsCredentialsConsole::request( const QString &realm, QString &username, QString &password, const QString &message )
126 {
127  QTextStream in( stdin, QIODevice::ReadOnly );
128  QTextStream out( stdout, QIODevice::WriteOnly );
129 
130  out << "credentials for " << realm << endl;
131  if ( !message.isEmpty() )
132  out << "message: " << message << endl;
133  out << "username: ";
134  in >> username;
135  out << "password: ";
136  in >> password;
137 
138  return true;
139 }
140 
141 bool QgsCredentialsConsole::requestMasterPassword( QString &password, bool stored )
142 {
143  Q_UNUSED( stored );
144 
145  QTextStream in( stdin, QIODevice::ReadOnly );
146  QTextStream out( stdout, QIODevice::WriteOnly );
147 
148  QString msg( stored ? "Master password for authentication configs: " : "Set master password for authentication configs: " );
149 
150  out << msg;
151  in >> password;
152 
153  return true;
154 }
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
bool get(const QString &realm, QString &username, QString &password, const QString &message=QString())
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)
bool requestMasterPassword(QString &password, bool stored=false) override
request a master password