Skip to content
Permalink
Browse files

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
timlinux committed Apr 21, 2009
1 parent 4ac3e65 commit 2cb76e830575b01fd6884aee9136b0747d708696
@@ -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() ) );
}
@@ -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();
}
@@ -39,6 +39,7 @@
#include <QDomDocument>
#include <QHeaderView>
#include <QImageReader>
#include <QInputDialog>
#include <QMap>
#include <QMessageBox>
#include <QPicture>
@@ -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;
@@ -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 ) );


@@ -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(),
@@ -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 ),
@@ -53,6 +59,11 @@ QgsHttpTransaction::~QgsHttpTransaction()
}


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

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

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

int httpport;

@@ -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 ) )
{
@@ -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();
@@ -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:

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

/**
* User name
*/
QString mUserName;

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

#endif
@@ -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;
@@ -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( "?" ) ) )
@@ -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 )
{
@@ -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 ) ),
@@ -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
*
@@ -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
@@ -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;

@@ -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
@@ -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" >
@@ -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>
@@ -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>

0 comments on commit 2cb76e8

Please sign in to comment.
You can’t perform that action at this time.