Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
timlinux committed Apr 21, 2009
1 parent 4ac3e65 commit 2cb76e8
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/app/qgsnewhttpconnection.cpp
Expand Up @@ -35,8 +35,12 @@ QgsNewHttpConnection::QgsNewHttpConnection(
QSettings settings;

QString key = mBaseKey + connName;
QString credentialsKey = "/Qgis/WMS/" + connName;
txtName->setText( connName );
txtUrl->setText( settings.value( key + "/url" ).toString() );
txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );

}
connect( buttonBox, SIGNAL( helpRequested() ), this, SLOT( helpRequested() ) );
}
Expand All @@ -49,13 +53,17 @@ void QgsNewHttpConnection::accept()
{
QSettings settings;
QString key = mBaseKey + txtName->text();
QString credentialsKey = "/Qgis/WMS/" + txtName->text();

//delete original entry first
if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
{
settings.remove( mBaseKey + mOriginalConnName );
settings.remove ( "/Qgis/WMS/" + mOriginalConnName );
}
settings.setValue( key + "/url", txtUrl->text().trimmed() );
settings.setValue( credentialsKey + "/username", txtUserName->text() );
settings.setValue( credentialsKey + "/password", txtPassword->text() );

QDialog::accept();
}
Expand Down
18 changes: 18 additions & 0 deletions src/app/qgsserversourceselect.cpp
Expand Up @@ -39,6 +39,7 @@
#include <QDomDocument>
#include <QHeaderView>
#include <QImageReader>
#include <QInputDialog>
#include <QMap>
#include <QMessageBox>
#include <QPicture>
Expand Down Expand Up @@ -363,6 +364,7 @@ void QgsServerSourceSelect::on_btnConnect_clicked()
QSettings settings;

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

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

// Check for credentials and prepend to the connection info
QString username = settings.value( credentialsKey + "/username" ).toString();
QString password = settings.value( credentialsKey + "/password" ).toString();
if ( !username.isEmpty() )
{
// check for a password, if none prompt to get it
if ( password.isEmpty() )
{
password = QInputDialog::getText( this, tr( "WMS Password for " ) + m_connName, "Password", QLineEdit::Password );

}
m_connectionInfo = "username=" + username + ",password=" + password + ",url=" + m_connectionInfo;
}


QgsDebugMsg( QString( "Connection info: '%1'." ).arg( m_connectionInfo ) );


Expand Down Expand Up @@ -774,6 +791,7 @@ bool QgsServerSourceSelect::retrieveSearchResults( const QString& searchTerm, QB
}
#endif
}
// Get username/password from settings for protected WMS

QUrl url( QString( "http://geopole.org/wms/search?search=%1&type=rss" ).arg( searchTerm ) );
QgsHttpTransaction http( url.toEncoded(),
Expand Down
20 changes: 19 additions & 1 deletion src/core/qgshttptransaction.cpp
Expand Up @@ -34,12 +34,18 @@
static int NETWORK_TIMEOUT_MSEC = ( 120 * 1000 ); // 120 seconds
static int HTTP_PORT_DEFAULT = 80;

//XXX Set the connection name when creating the provider instance
//XXX in qgswmsprovider. When creating a QgsHttpTransaction, pass
//XXX the user/pass combination to the constructor. Then set the
//XXX username and password using QHttp::setUser.
QgsHttpTransaction::QgsHttpTransaction( QString uri,
QString proxyHost,
int proxyPort,
QString proxyUser,
QString proxyPass,
QNetworkProxy::ProxyType proxyType )
QNetworkProxy::ProxyType proxyType,
QString userName,
QString password )
: httpresponsecontenttype( 0 ),
httpurl( uri ),
httphost( proxyHost ),
Expand All @@ -53,6 +59,11 @@ QgsHttpTransaction::~QgsHttpTransaction()
}


void QgsHttpTransaction::setCredentials( const QString& username, const QString& password )
{
mUserName = username;
mPassword = password;
}
void QgsHttpTransaction::getAsynchronously()
{

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

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

int httpport;

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

if ( !QgsHttpTransaction::applyProxySettings( *http, httpurl ) )
{
Expand Down
21 changes: 20 additions & 1 deletion src/core/qgshttptransaction.h
Expand Up @@ -42,13 +42,16 @@ class CORE_EXPORT QgsHttpTransaction : public QObject
public:
/**
* Constructor.
* \note userName and password added in 1.1
*/
QgsHttpTransaction( QString uri,
QString proxyHost = QString(),
int proxyPort = 80,
QString proxyUser = QString(),
QString proxyPass = QString(),
QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy );
QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy,
QString userName = QString(),
QString password = QString() );

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

/**
* Set the credentials (username and password)
* \note added in 1.1
*/

void setCredentials( const QString& username, const QString &password );


public slots:

Expand Down Expand Up @@ -188,6 +198,15 @@ class CORE_EXPORT QgsHttpTransaction : public QObject
*/
QString mError;

/**
* User name
*/
QString mUserName;

/**
* Password
*/
QString mPassword;
};

#endif
Expand Down
51 changes: 49 additions & 2 deletions src/providers/wms/qgswmsprovider.cpp
Expand Up @@ -62,8 +62,16 @@ QgsWmsProvider::QgsWmsProvider( QString const & uri )
extentDirty( TRUE ),
mGetFeatureInfoUrlBase( 0 ),
mLayerCount( -1 )

{
QgsDebugMsg( "QgsWmsProvider: constructing with uri '" + uri + "'." );
// URL may contain username/password information for a WMS
// requiring authentication. In this case the URL is prefixed
// with username=user,password=pass,url=http://xxx.xxx.xx/yyy...
mUserName= "";
mPassword = "";
setAuthentication( httpuri );

QgsDebugMsg( "QgsWmsProvider: constructing with uri '" + httpuri + "'." );

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

void QgsWmsProvider::setAuthentication( QString uri )
{
// Strip off and store the user name and password (if they exist)
if ( ! uri.startsWith(" http:" ) )
{
// uri potentially contains username and password
QStringList parts = uri.split( "," );
QStringListIterator iter( parts );
while ( iter.hasNext() )
{
QString item = iter.next();
QgsDebugMsg( "QgsWmsProvider: Testing for creds: " + item );
if ( item.startsWith( "username=" ) )
{
mUserName = item.mid( 9 );
QgsDebugMsg( "QgsWmsProvider: Set username to " + mUserName );
}
else if ( item.startsWith( "password=" ) )
{
mPassword = item.mid( 9 );
QgsDebugMsg( "QgsWmsProvider: Set password to " + mPassword );
}
else if ( item.startsWith( "url=" ) )
{
// strip the authentication information from the front of the uri
httpuri = item.mid( 4 );
QgsDebugMsg( "QgsWmsProvider: Set httpuri to " + httpuri );
}
}

}

}
QString QgsWmsProvider::prepareUri( QString uri )
{
if ( !( uri.contains( "?" ) ) )
Expand Down Expand Up @@ -221,6 +262,10 @@ void QgsWmsProvider::addLayers( QStringList const &layers,
QgsDebugMsg( "Exiting." );
}

void QgsWmsProvider::setConnectionName( QString const &connName )
{
connectionName = connName;
}

void QgsWmsProvider::setLayerOrder( QStringList const &layers )
{
Expand Down Expand Up @@ -640,7 +685,9 @@ QByteArray QgsWmsProvider::retrieveUrl( QString url )
{
QgsDebugMsg( "WMS request Url: " + url );
QgsHttpTransaction http( url );

QgsDebugMsg( "Setting creds: " + mUserName + "/" + mPassword );
http.setCredentials( mUserName, mPassword );

// Do a passthrough for the status bar text
connect(
&http, SIGNAL( statusChanged( QString ) ),
Expand Down
26 changes: 26 additions & 0 deletions src/providers/wms/qgswmsprovider.h
Expand Up @@ -422,6 +422,12 @@ class QgsWmsProvider : public QgsRasterDataProvider
*/
void setImageCrs( QString const & crs );

/**
* Set the name of the connection for use in authentication where required
* \note added in 1.1
*/
void setConnectionName( QString const & connName);

// TODO: Document this better.
/** \brief Renders the layer as an image
*
Expand Down Expand Up @@ -686,6 +692,17 @@ class QgsWmsProvider : public QgsRasterDataProvider
*/
bool calculateExtent();

/**
* \brief Check for authentication information contained in the uri,
* stripping and saving the username and password if present.
*
* \param uri uri to check
*
* \note added in 1.1
*/

void setAuthentication( QString uri );

/**
* \brief Prepare the URI so that we can later simply append param=value
* \param uri uri to prepare
Expand All @@ -696,6 +713,9 @@ class QgsWmsProvider : public QgsRasterDataProvider
//! Data source URI of the WMS for this layer
QString httpuri;

//! Name of the stored connection
QString connectionName;

//! URL part of URI (httpuri)
QString baseUrl;

Expand Down Expand Up @@ -823,6 +843,12 @@ class QgsWmsProvider : public QgsRasterDataProvider
QMap<int, int> mLayerParents;
QMap<int, QStringList> mLayerParentNames;

//! Username for basic http authentication
QString mUserName;

//! Password for basic http authentication
QString mPassword;

};

#endif
Expand Down
58 changes: 54 additions & 4 deletions src/ui/qgsnewhttpconnectionbase.ui
Expand Up @@ -5,8 +5,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>431</width>
<height>159</height>
<width>512</width>
<height>254</height>
</rect>
</property>
<property name="windowTitle" >
Expand Down Expand Up @@ -38,7 +38,7 @@
</property>
</widget>
</item>
<item row="0" column="1" >
<item row="0" column="1" colspan="4" >
<widget class="QLineEdit" name="txtName" >
<property name="minimumSize" >
<size>
Expand Down Expand Up @@ -67,13 +67,63 @@
</property>
</widget>
</item>
<item row="1" column="1" >
<item row="1" column="1" colspan="4" >
<widget class="QLineEdit" name="txtUrl" >
<property name="toolTip" >
<string>HTTP address of the Web Map Server</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="5" >
<widget class="QLabel" name="label" >
<property name="text" >
<string>If the WMS requires basic authentication, enter a user name and optional password</string>
</property>
<property name="textFormat" >
<enum>Qt::PlainText</enum>
</property>
<property name="wordWrap" >
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2" >
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>User name</string>
</property>
</widget>
</item>
<item row="3" column="2" >
<widget class="QLineEdit" name="txtUserName" >
<property name="maximumSize" >
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="3" column="3" >
<widget class="QLabel" name="label_3" >
<property name="text" >
<string>Password</string>
</property>
</widget>
</item>
<item row="3" column="4" >
<widget class="QLineEdit" name="txtPassword" >
<property name="maximumSize" >
<size>
<width>120</width>
<height>120</height>
</size>
</property>
<property name="echoMode" >
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 2cb76e8

Please sign in to comment.