Skip to content

Commit 2cb76e8

Browse files
author
timlinux
committed
Applied last minute patch for http auth support in wms (see ticket #1603).
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10608 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 4ac3e65 commit 2cb76e8

7 files changed

+194
-8
lines changed

src/app/qgsnewhttpconnection.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ QgsNewHttpConnection::QgsNewHttpConnection(
3535
QSettings settings;
3636

3737
QString key = mBaseKey + connName;
38+
QString credentialsKey = "/Qgis/WMS/" + connName;
3839
txtName->setText( connName );
3940
txtUrl->setText( settings.value( key + "/url" ).toString() );
41+
txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
42+
txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
43+
4044
}
4145
connect( buttonBox, SIGNAL( helpRequested() ), this, SLOT( helpRequested() ) );
4246
}
@@ -49,13 +53,17 @@ void QgsNewHttpConnection::accept()
4953
{
5054
QSettings settings;
5155
QString key = mBaseKey + txtName->text();
56+
QString credentialsKey = "/Qgis/WMS/" + txtName->text();
5257

5358
//delete original entry first
5459
if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
5560
{
5661
settings.remove( mBaseKey + mOriginalConnName );
62+
settings.remove ( "/Qgis/WMS/" + mOriginalConnName );
5763
}
5864
settings.setValue( key + "/url", txtUrl->text().trimmed() );
65+
settings.setValue( credentialsKey + "/username", txtUserName->text() );
66+
settings.setValue( credentialsKey + "/password", txtPassword->text() );
5967

6068
QDialog::accept();
6169
}

src/app/qgsserversourceselect.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <QDomDocument>
4040
#include <QHeaderView>
4141
#include <QImageReader>
42+
#include <QInputDialog>
4243
#include <QMap>
4344
#include <QMessageBox>
4445
#include <QPicture>
@@ -363,6 +364,7 @@ void QgsServerSourceSelect::on_btnConnect_clicked()
363364
QSettings settings;
364365

365366
QString key = "/Qgis/connections-wms/" + cmbConnections->currentText();
367+
QString credentialsKey = "/Qgis/WMS/" + cmbConnections->currentText();
366368

367369
QStringList connStringParts;
368370
QString part;
@@ -372,6 +374,21 @@ void QgsServerSourceSelect::on_btnConnect_clicked()
372374
m_connName = cmbConnections->currentText();
373375
m_connectionInfo = connStringParts.join( " " );
374376

377+
// Check for credentials and prepend to the connection info
378+
QString username = settings.value( credentialsKey + "/username" ).toString();
379+
QString password = settings.value( credentialsKey + "/password" ).toString();
380+
if ( !username.isEmpty() )
381+
{
382+
// check for a password, if none prompt to get it
383+
if ( password.isEmpty() )
384+
{
385+
password = QInputDialog::getText( this, tr( "WMS Password for " ) + m_connName, "Password", QLineEdit::Password );
386+
387+
}
388+
m_connectionInfo = "username=" + username + ",password=" + password + ",url=" + m_connectionInfo;
389+
}
390+
391+
375392
QgsDebugMsg( QString( "Connection info: '%1'." ).arg( m_connectionInfo ) );
376393

377394

@@ -774,6 +791,7 @@ bool QgsServerSourceSelect::retrieveSearchResults( const QString& searchTerm, QB
774791
}
775792
#endif
776793
}
794+
// Get username/password from settings for protected WMS
777795

778796
QUrl url( QString( "http://geopole.org/wms/search?search=%1&type=rss" ).arg( searchTerm ) );
779797
QgsHttpTransaction http( url.toEncoded(),

src/core/qgshttptransaction.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@
3434
static int NETWORK_TIMEOUT_MSEC = ( 120 * 1000 ); // 120 seconds
3535
static int HTTP_PORT_DEFAULT = 80;
3636

37+
//XXX Set the connection name when creating the provider instance
38+
//XXX in qgswmsprovider. When creating a QgsHttpTransaction, pass
39+
//XXX the user/pass combination to the constructor. Then set the
40+
//XXX username and password using QHttp::setUser.
3741
QgsHttpTransaction::QgsHttpTransaction( QString uri,
3842
QString proxyHost,
3943
int proxyPort,
4044
QString proxyUser,
4145
QString proxyPass,
42-
QNetworkProxy::ProxyType proxyType )
46+
QNetworkProxy::ProxyType proxyType,
47+
QString userName,
48+
QString password )
4349
: httpresponsecontenttype( 0 ),
4450
httpurl( uri ),
4551
httphost( proxyHost ),
@@ -53,6 +59,11 @@ QgsHttpTransaction::~QgsHttpTransaction()
5359
}
5460

5561

62+
void QgsHttpTransaction::setCredentials( const QString& username, const QString& password )
63+
{
64+
mUserName = username;
65+
mPassword = password;
66+
}
5667
void QgsHttpTransaction::getAsynchronously()
5768
{
5869

@@ -67,6 +78,7 @@ bool QgsHttpTransaction::getSynchronously( QByteArray &respondedContent, int red
6778

6879
QgsDebugMsg( "Entered." );
6980
QgsDebugMsg( "Using '" + httpurl + "'." );
81+
QgsDebugMsg( "Creds: " + mUserName + "/" + mPassword );
7082

7183
int httpport;
7284

@@ -88,6 +100,12 @@ bool QgsHttpTransaction::getSynchronously( QByteArray &respondedContent, int red
88100
header.setValue( "User-agent", QString( "Quantum GIS - " ) + VERSION );
89101
// Set the host in the QHttp object
90102
http->setHost( qurl.host(), qurl.port( HTTP_PORT_DEFAULT ) );
103+
// Set the username and password if supplied for this connection
104+
// If we have username and password set in header
105+
if ( !mUserName.isEmpty() && !mPassword.isEmpty() )
106+
{
107+
http->setUser( mUserName, mPassword );
108+
}
91109

92110
if ( !QgsHttpTransaction::applyProxySettings( *http, httpurl ) )
93111
{

src/core/qgshttptransaction.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ class CORE_EXPORT QgsHttpTransaction : public QObject
4242
public:
4343
/**
4444
* Constructor.
45+
* \note userName and password added in 1.1
4546
*/
4647
QgsHttpTransaction( QString uri,
4748
QString proxyHost = QString(),
4849
int proxyPort = 80,
4950
QString proxyUser = QString(),
5051
QString proxyPass = QString(),
51-
QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy );
52+
QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy,
53+
QString userName = QString(),
54+
QString password = QString() );
5255

5356
//! Destructor
5457
virtual ~QgsHttpTransaction();
@@ -86,6 +89,13 @@ class CORE_EXPORT QgsHttpTransaction : public QObject
8689
@param return true if proxy settings was applied, false else*/
8790
static bool applyProxySettings( QHttp& http, const QString& url );
8891

92+
/**
93+
* Set the credentials (username and password)
94+
* \note added in 1.1
95+
*/
96+
97+
void setCredentials( const QString& username, const QString &password );
98+
8999

90100
public slots:
91101

@@ -188,6 +198,15 @@ class CORE_EXPORT QgsHttpTransaction : public QObject
188198
*/
189199
QString mError;
190200

201+
/**
202+
* User name
203+
*/
204+
QString mUserName;
205+
206+
/**
207+
* Password
208+
*/
209+
QString mPassword;
191210
};
192211

193212
#endif

src/providers/wms/qgswmsprovider.cpp

+49-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ QgsWmsProvider::QgsWmsProvider( QString const & uri )
6262
extentDirty( TRUE ),
6363
mGetFeatureInfoUrlBase( 0 ),
6464
mLayerCount( -1 )
65+
6566
{
66-
QgsDebugMsg( "QgsWmsProvider: constructing with uri '" + uri + "'." );
67+
// URL may contain username/password information for a WMS
68+
// requiring authentication. In this case the URL is prefixed
69+
// with username=user,password=pass,url=http://xxx.xxx.xx/yyy...
70+
mUserName= "";
71+
mPassword = "";
72+
setAuthentication( httpuri );
73+
74+
QgsDebugMsg( "QgsWmsProvider: constructing with uri '" + httpuri + "'." );
6775

6876
// assume this is a valid layer until we determine otherwise
6977
valid = true;
@@ -100,6 +108,39 @@ QgsWmsProvider::QgsWmsProvider( QString const & uri )
100108
QgsDebugMsg( "QgsWmsProvider: exiting constructor." );
101109
}
102110

111+
void QgsWmsProvider::setAuthentication( QString uri )
112+
{
113+
// Strip off and store the user name and password (if they exist)
114+
if ( ! uri.startsWith(" http:" ) )
115+
{
116+
// uri potentially contains username and password
117+
QStringList parts = uri.split( "," );
118+
QStringListIterator iter( parts );
119+
while ( iter.hasNext() )
120+
{
121+
QString item = iter.next();
122+
QgsDebugMsg( "QgsWmsProvider: Testing for creds: " + item );
123+
if ( item.startsWith( "username=" ) )
124+
{
125+
mUserName = item.mid( 9 );
126+
QgsDebugMsg( "QgsWmsProvider: Set username to " + mUserName );
127+
}
128+
else if ( item.startsWith( "password=" ) )
129+
{
130+
mPassword = item.mid( 9 );
131+
QgsDebugMsg( "QgsWmsProvider: Set password to " + mPassword );
132+
}
133+
else if ( item.startsWith( "url=" ) )
134+
{
135+
// strip the authentication information from the front of the uri
136+
httpuri = item.mid( 4 );
137+
QgsDebugMsg( "QgsWmsProvider: Set httpuri to " + httpuri );
138+
}
139+
}
140+
141+
}
142+
143+
}
103144
QString QgsWmsProvider::prepareUri( QString uri )
104145
{
105146
if ( !( uri.contains( "?" ) ) )
@@ -221,6 +262,10 @@ void QgsWmsProvider::addLayers( QStringList const &layers,
221262
QgsDebugMsg( "Exiting." );
222263
}
223264

265+
void QgsWmsProvider::setConnectionName( QString const &connName )
266+
{
267+
connectionName = connName;
268+
}
224269

225270
void QgsWmsProvider::setLayerOrder( QStringList const &layers )
226271
{
@@ -640,7 +685,9 @@ QByteArray QgsWmsProvider::retrieveUrl( QString url )
640685
{
641686
QgsDebugMsg( "WMS request Url: " + url );
642687
QgsHttpTransaction http( url );
643-
688+
QgsDebugMsg( "Setting creds: " + mUserName + "/" + mPassword );
689+
http.setCredentials( mUserName, mPassword );
690+
644691
// Do a passthrough for the status bar text
645692
connect(
646693
&http, SIGNAL( statusChanged( QString ) ),

src/providers/wms/qgswmsprovider.h

+26
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ class QgsWmsProvider : public QgsRasterDataProvider
422422
*/
423423
void setImageCrs( QString const & crs );
424424

425+
/**
426+
* Set the name of the connection for use in authentication where required
427+
* \note added in 1.1
428+
*/
429+
void setConnectionName( QString const & connName);
430+
425431
// TODO: Document this better.
426432
/** \brief Renders the layer as an image
427433
*
@@ -686,6 +692,17 @@ class QgsWmsProvider : public QgsRasterDataProvider
686692
*/
687693
bool calculateExtent();
688694

695+
/**
696+
* \brief Check for authentication information contained in the uri,
697+
* stripping and saving the username and password if present.
698+
*
699+
* \param uri uri to check
700+
*
701+
* \note added in 1.1
702+
*/
703+
704+
void setAuthentication( QString uri );
705+
689706
/**
690707
* \brief Prepare the URI so that we can later simply append param=value
691708
* \param uri uri to prepare
@@ -696,6 +713,9 @@ class QgsWmsProvider : public QgsRasterDataProvider
696713
//! Data source URI of the WMS for this layer
697714
QString httpuri;
698715

716+
//! Name of the stored connection
717+
QString connectionName;
718+
699719
//! URL part of URI (httpuri)
700720
QString baseUrl;
701721

@@ -823,6 +843,12 @@ class QgsWmsProvider : public QgsRasterDataProvider
823843
QMap<int, int> mLayerParents;
824844
QMap<int, QStringList> mLayerParentNames;
825845

846+
//! Username for basic http authentication
847+
QString mUserName;
848+
849+
//! Password for basic http authentication
850+
QString mPassword;
851+
826852
};
827853

828854
#endif

src/ui/qgsnewhttpconnectionbase.ui

+54-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<rect>
66
<x>0</x>
77
<y>0</y>
8-
<width>431</width>
9-
<height>159</height>
8+
<width>512</width>
9+
<height>254</height>
1010
</rect>
1111
</property>
1212
<property name="windowTitle" >
@@ -38,7 +38,7 @@
3838
</property>
3939
</widget>
4040
</item>
41-
<item row="0" column="1" >
41+
<item row="0" column="1" colspan="4" >
4242
<widget class="QLineEdit" name="txtName" >
4343
<property name="minimumSize" >
4444
<size>
@@ -67,13 +67,63 @@
6767
</property>
6868
</widget>
6969
</item>
70-
<item row="1" column="1" >
70+
<item row="1" column="1" colspan="4" >
7171
<widget class="QLineEdit" name="txtUrl" >
7272
<property name="toolTip" >
7373
<string>HTTP address of the Web Map Server</string>
7474
</property>
7575
</widget>
7676
</item>
77+
<item row="2" column="0" colspan="5" >
78+
<widget class="QLabel" name="label" >
79+
<property name="text" >
80+
<string>If the WMS requires basic authentication, enter a user name and optional password</string>
81+
</property>
82+
<property name="textFormat" >
83+
<enum>Qt::PlainText</enum>
84+
</property>
85+
<property name="wordWrap" >
86+
<bool>true</bool>
87+
</property>
88+
</widget>
89+
</item>
90+
<item row="3" column="0" colspan="2" >
91+
<widget class="QLabel" name="label_2" >
92+
<property name="text" >
93+
<string>User name</string>
94+
</property>
95+
</widget>
96+
</item>
97+
<item row="3" column="2" >
98+
<widget class="QLineEdit" name="txtUserName" >
99+
<property name="maximumSize" >
100+
<size>
101+
<width>120</width>
102+
<height>16777215</height>
103+
</size>
104+
</property>
105+
</widget>
106+
</item>
107+
<item row="3" column="3" >
108+
<widget class="QLabel" name="label_3" >
109+
<property name="text" >
110+
<string>Password</string>
111+
</property>
112+
</widget>
113+
</item>
114+
<item row="3" column="4" >
115+
<widget class="QLineEdit" name="txtPassword" >
116+
<property name="maximumSize" >
117+
<size>
118+
<width>120</width>
119+
<height>120</height>
120+
</size>
121+
</property>
122+
<property name="echoMode" >
123+
<enum>QLineEdit::Password</enum>
124+
</property>
125+
</widget>
126+
</item>
77127
</layout>
78128
</widget>
79129
</item>

0 commit comments

Comments
 (0)