[Qgis-user] Using QgsAuthManager

Paul Wittle paul.wittle at dorsetcouncil.gov.uk
Fri Jul 26 02:32:37 PDT 2019


Hi,

I think I might have just spotted the issue with my code.

If you do quir.encodedUri() you get an error in version 3.4 because it is expecting a string but encodedUri returns a ByteArray. If you wrap it in the str() function you get a result because the result in now a string however it is a string which has b’ a the front and a trailing ‘ at the end.

I believe the trailing ‘ is being taken literally and the resulting getCapabilities statement is for https://domain.name/wms’?request=GetCapabilities<https://domain.name/wms'?request=GetCapabilities>

If you use quir.uri() it doesn’t work and neither does quir.uri(False) so what should I be using there or should the QgsRasterLayer function just be accepting a ByteArray or String?

Thanks,
Paul

From: Paul Wittle
Sent: 26 July 2019 08:23
To: DelazJ <delazj at gmail.com>
Cc: qgis-user at lists.osgeo.org
Subject: RE: [Qgis-user] Using QgsAuthManager

Hi,

I couldn’t get it to work with “authMgr = QgsAuthManager.instance()” so following the comment at the top of (https://qgis.org/pyqgis/3.4/core/QgsAuthManager.html) I used “authMgr =  QgsApplication.authManager()” instead.

I changed “authMgr.authenticationDbPath()” to “authMgr.authenticationDatabasePath()”.

I also changed “QgsDataSourceURI()” to “QgsDataSourceUri()”.

p_config.setConfig("certpath", "path/to/alice-cert.pem" ))
p_config.setConfig("keypath", "path/to/alice-key.pem" ))
There are extra brackets on the lines above but clearly those are just typos.

Finally the line “rlayer = QgsRasterLayer(quri.encodedUri(), 'states', 'wms')” only ran if I put in “rlayer = QgsRasterLayer(str(quri.encodedUri()), 'states', 'wms')”.

That is the issue though; whilst the line above runs in QGIS it doesn’t appear to actually be authenticating on the GetCapabilities request. This maybe because our server uses pre-authentication because it is a GeoServer which is set to HIDE (https://docs.geoserver.org/latest/en/user/security/webadmin/data.html#catalog-mode).

As far as I can tell there is no concept of that in QGIS so in effect I believe QGIS would not support HIDE mode GeoServers unless you are using some sort of token based authentication as it will send a number of requests without the correct authentication settings. I’ve been investigating a work around given that this process is aimed at connections via our network only but perhaps I’ve missed something obvious or the use of str() in the statement above is simply preventing the authentication manager from expanding the auth credentials at the last minute?

I’ll go do a quick update for the brackets and I can update the other lines once others have confirmed my interpretations are correct; they compiled but that doesn’t mean I got them right!

Thanks,
Paul


From: DelazJ <delazj at gmail.com<mailto:delazj at gmail.com>>
Sent: 24 July 2019 23:25
To: Paul Wittle <P.Wittle at dorsetcc.gov.uk<mailto:P.Wittle at dorsetcc.gov.uk>>
Cc: qgis-user at lists.osgeo.org<mailto:qgis-user at lists.osgeo.org>
Subject: Re: [Qgis-user] Using QgsAuthManager

Hi,

Le mer. 24 juil. 2019 à 14:20, Paul Wittle <paul.wittle at dorsetcouncil.gov.uk<mailto:paul.wittle at dorsetcouncil.gov.uk>> a écrit :
Hi,

I’m trying to use python to assign an authentication configuration to a WMS layers that I’m adding. I have been using the following guide https://docs.qgis.org/3.4/en/docs/pyqgis_developer_cookbook/authentication.html#leave-authcfg-expansion-to-qgsauthmanager.

I had to update a few bits as I think some of the code maybe a little out of date but I managed to get it all working right down to the section I’ve linked to.

The line  “rlayer = QgsRasterLayer(quri.encodedUri(), 'states', 'wms')” was resulting in an unexpected type error as encodedUri returns a QByteArray rather than a string but I just wrapped it in str(quri.encodedUri()) and that now returns as a string.

Paul, can you share all the changes you had to do, so that we can update the docs? Or is the line above the only one you had to change?
A direct change to the source file (https://github.com/qgis/QGIS-Documentation/edit/master/source/docs/pyqgis_developer_cookbook/authentication.rst) with a pull request for merging would be the most practical way but we'd also welcome changes within a text-like file or in this thread.

Thanks,
Harrissou

The only trouble is that the getCapabilities statement fails and no layer is added.

Does anyone know how I can see whether the AuthManager attempted to expand the authcfg and send credentials as I suspect it didn’t in my case?

Sorry if this email is too much along the developer lines for this mailing list but it is only plugin development rather than actual development of the application itself so I thought I’d try my luck.

Thanks,
Paul
This e-mail and any files transmitted with it are intended solely for the use of the individual or entity to whom they are addressed. It may contain unclassified but sensitive or protectively marked material and should be handled accordingly. Unless you are the named addressee (or authorised to receive it for the addressee) you may not copy or use it, or disclose it to anyone else. If you have received this transmission in error please notify the sender immediately. All traffic may be subject to recording and/or monitoring in accordance with relevant legislation. Any views expressed in this message are those of the individual sender, except where the sender specifies and with authority, states them to be the views of Dorset Council. Dorset Council does not accept service of documents by fax or other electronic means. Virus checking: Whilst all reasonable steps have been taken to ensure that this electronic communication and its attachments whether encoded, encrypted or otherwise supplied are free from computer viruses, Dorset Council accepts no liability in respect of any loss, cost, damage or expense suffered as a result of accessing this message or any of its attachments. For information on how Dorset Council processes your information, please see www.dorsetcouncil.gov.uk/416433<http://www.dorsetcouncil.gov.uk/416433>
_______________________________________________
Qgis-user mailing list
Qgis-user at lists.osgeo.org<mailto:Qgis-user at lists.osgeo.org>
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
This e-mail and any files transmitted with it are intended solely for the use of the individual or entity to whom they are addressed. It may contain unclassified but sensitive or protectively marked material and should be handled accordingly. Unless you are the named addressee (or authorised to receive it for the addressee) you may not copy or use it, or disclose it to anyone else. If you have received this transmission in error please notify the sender immediately. All traffic may be subject to recording and/or monitoring in accordance with relevant legislation. Any views expressed in this message are those of the individual sender, except where the sender specifies and with authority, states them to be the views of Dorset Council. Dorset Council does not accept service of documents by fax or other electronic means. Virus checking: Whilst all reasonable steps have been taken to ensure that this electronic communication and its attachments whether encoded, encrypted or otherwise supplied are free from computer viruses, Dorset Council accepts no liability in respect of any loss, cost, damage or expense suffered as a result of accessing this message or any of its attachments. For information on how Dorset Council processes your information, please see www.dorsetcouncil.gov.uk/416433
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20190726/d4d3e74f/attachment-0001.html>


More information about the Qgis-user mailing list