-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgsauthsaml2method.cpp
409 lines (351 loc) · 15 KB
/
qgsauthsaml2method.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/***************************************************************************
begin : October 15, 2017
copyright : (C) 2017 by Secure Dimensions GmbH, Germany
author : Andreas Matheus, Secure Dimensions GmbH
email : am at secure-dimensions dot de
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsauthsaml2method.h"
#include "qgsauthsaml2edit.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsauthmanager.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include <QDomDocument>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QBuffer>
#include <QNetworkCookie>
#include <QDomNamedNodeMap>
#include <QSettings>
static const QString AUTH_METHOD_KEY = "SAML2";
static const QString AUTH_METHOD_DESCRIPTION = "SAML2 authentication";
QMap<QString, QgsAuthMethodConfig> QgsAuthSAML2Method::mAuthConfigCache = QMap<QString, QgsAuthMethodConfig>();
namespace
{
QDomNode namedItemNS(const QDomNodeList &nodes, const char *nsURI, const char *localName)
{
QDomNode n;
int ix, count = nodes.count();
for (ix=0; ix < count; ix++)
{
n = nodes.at(ix);
//QgsDebugMsg(QString("element name: %1 nsURI: %2").arg(n.localName(),n.namespaceURI()));
if ((n.localName() == localName) && (n.namespaceURI() == nsURI))
return n;
}
return QDomNode();
}
}
QgsAuthSAML2Method::QgsAuthSAML2Method()
: QgsAuthMethod()
{
setVersion( 1 );
setExpansions( QgsAuthMethod::NetworkRequest | QgsAuthMethod::NetworkReply );
setDataProviders( QStringList()
<< "ows"
<< "wfs" // convert to lowercase
<< "wcs"
<< "wms" );
}
QgsAuthSAML2Method::~QgsAuthSAML2Method()
{
}
QString QgsAuthSAML2Method::key() const
{
return AUTH_METHOD_KEY;
}
QString QgsAuthSAML2Method::description() const
{
return AUTH_METHOD_DESCRIPTION;
}
QString QgsAuthSAML2Method::displayDescription() const
{
return tr( "SAML2 authentication" );
}
bool QgsAuthSAML2Method::updateNetworkRequest( QNetworkRequest &request, const QString &authcfg,
const QString &dataprovider )
{
Q_UNUSED( dataprovider )
QString errorMsg;
QEventLoop networkLoop;
QByteArray spECPResponse;
QByteArray idpECPResponse;
QgsNetworkAccessManager* nam = QgsNetworkAccessManager::instance();
if( mCookieCache.contains( request.url().host() ) )
{
request.setHeader( QNetworkRequest::CookieHeader, mCookieCache[request.url().host()] );
return true;
}
QgsAuthMethodConfig mconfig = getMethodConfig( authcfg );
if ( !mconfig.isValid() )
{
errorMsg = QStringLiteral( "Update request config FAILED for authcfg: %1: config invalid" ).arg( authcfg );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
// the ECP URI
const char *nsECPURI = "urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp";
// signal to the SP that we understand SAML2 ECP
request.setRawHeader("PAOS", "ver=\"urn:liberty:paos:2003-08\";\"urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp\"");
request.setRawHeader("Accept", "text/xml; application/vnd.paos+xml");
/* Wait until reply is finished */
/* this now contains the ecp response from the SP and not the capabilities*/
QNetworkReply* spReply = nam->get( request );
connect( spReply, SIGNAL( finished() ), &networkLoop, SLOT( quit() ) );
networkLoop.exec();
if ( spReply->error() == QNetworkReply::NoError )
{
spECPResponse = spReply->readAll();
if ( spECPResponse.isEmpty() )
{
QString errorMsg = QStringLiteral( "Update request FAILED: empty ECP response from SP: %1" ).arg( spReply->errorString() );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
}
else
{
QString errorMsg = QStringLiteral( "Update request FAILED: ECP Response from SP failed: %1" ).arg( spReply->errorString() );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
if( spReply->header(QNetworkRequest::ContentTypeHeader).toString() != "application/vnd.paos+xml" )
{
return true;
}
QgsDebugMsg( QString( "ECP Response from SP: %1" ).arg( spECPResponse.data() ) );
// check if the response contains the PAOS response from the SP
if (spECPResponse.indexOf(nsECPURI) != -1)
{
QDomDocument docFromSP, docFromIdP;
QString acsURL,rsValue;
QString errorMsg;
int errorLine, errorColumn;
if ( docFromSP.setContent( spECPResponse, true, &errorMsg, &errorLine, &errorColumn ) )
{
QDomNode headerNode = namedItemNS(docFromSP.documentElement().childNodes(), "http://schemas.xmlsoap.org/soap/envelope/", "Header");
QDomNode bodyNode = namedItemNS(docFromSP.documentElement().childNodes(), "http://schemas.xmlsoap.org/soap/envelope/", "Body");
QDomNode relayState = namedItemNS(headerNode.childNodes(), nsECPURI, "RelayState");
rsValue = relayState.toElement().text();
QgsDebugMsg( QString( "RelayState: %1" ).arg( rsValue) );
QDomNode authnRequest = namedItemNS(bodyNode.childNodes(), "urn:oasis:names:tc:SAML:2.0:protocol", "AuthnRequest");
acsURL = authnRequest.toElement().attribute("AssertionConsumerServiceURL");
QgsDebugMsg( QString( "acsURL: %1" ).arg( acsURL) );
QDomNode paosRequest = namedItemNS(headerNode.childNodes(), "urn:liberty:paos:2003-08", "Request");
QDomNode ecpRequest = namedItemNS(headerNode.childNodes(), nsECPURI,"Request");
}
else
{
errorMsg = QStringLiteral( "Update request config FAILED for authcfg: %1: could not create DOM from ECP response from SP" ).arg( authcfg );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
// modify the RAW data to preserve the Digital Signature
// we CANNOT do this via DOM processing...
QByteArray dataToIdP = spECPResponse;
int ix1 = dataToIdP.indexOf("Header>") + 7;
int ix2 = dataToIdP.indexOf("Header>", ix1) + 7;
dataToIdP.remove(ix1, ix2-ix1);
dataToIdP.insert(ix1-1,'/');
QNetworkRequest requestToIdP( QUrl( mconfig.config( "providerurl" ) ) );
// in case the user has saved username/password in the configuration, it must
// be applied to the IdP not the SP
QString username = mconfig.config( "username" );
QString password = mconfig.config( "password" );
if ( !username.isEmpty() )
{
requestToIdP.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( username, password ).toAscii().toBase64() );
}
requestToIdP.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
requestToIdP.setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
// signal SAML2 ECP to the IdP
requestToIdP.setHeader(QNetworkRequest::ContentTypeHeader, "text/xml");
// relay the modified ECP message to IdP
QgsDebugMsg( QString( "ECP message to IdP: %1" ).arg( QString(dataToIdP)) );
/* Wait until reply is finished */
QNetworkReply* idpReply = nam->post( requestToIdP , dataToIdP);
connect( idpReply, SIGNAL( finished() ), &networkLoop, SLOT( quit() ) );
networkLoop.exec();
// we have a response from the IdP
if ( idpReply->error() == QNetworkReply::NoError )
{
idpECPResponse = idpReply->readAll();
if ( idpECPResponse.isEmpty() )
{
QString errorMsg = QStringLiteral( "Update request FAILED: empty ECP response from IdP: %1" ).arg( idpReply->errorString() );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
}
else
{
QString errorMsg = QStringLiteral( "Update request FAILED: ECP Response from IdP failed: %1" ).arg( idpReply->errorString() );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
QgsDebugMsg( QString( "ECP Response from IdP: %1" ).arg( idpECPResponse.constData() ) );
if ( docFromIdP.setContent( idpECPResponse, true, &errorMsg, &errorLine, &errorColumn ) )
{
QDomNode headerNode = namedItemNS(docFromIdP.documentElement().childNodes(), "http://schemas.xmlsoap.org/soap/envelope/", "Header");
QString relayState = QString ("<ecp:RelayState xmlns:ecp=\"urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp\" " + headerNode.prefix() + ":actor=\"http://schemas.xmlsoap.org/soap/actor/next\" " + headerNode.prefix() + ":mustUnderstand=\"1\">" + rsValue + "</ecp:RelayState>");
idpECPResponse.replace(QString("<%1:Header>").arg(headerNode.prefix()).toAscii().data(),QString("<%1:Header>%2").arg(headerNode.prefix(),relayState).toAscii().data());
}
else
{
errorMsg = QStringLiteral( "Update request config FAILED for authcfg: %1: could not create DOM from ECP response from SP" ).arg( authcfg );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
QgsDebugMsg( QString( "ECP message to SP: %1" ).arg( QString(idpECPResponse) ) );
// send modified IdP response to the SP - this contains the captured RelayState
QNetworkRequest requestToSP ( acsURL);
requestToSP.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
requestToSP.setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
QgsDebugMsg( QString( "requesting capabilities via ECP with URL: %1" ).arg( acsURL ) );
requestToSP.setHeader(QNetworkRequest::ContentTypeHeader, "text/xml; application/vnd.paos+xml");
/* Send request for cookies */
QNetworkReply* capabilitiesReply = nam->post( requestToSP, idpECPResponse );
connect( capabilitiesReply, SIGNAL( finished() ), &networkLoop, SLOT( quit() ) );
networkLoop.exec();
if ( capabilitiesReply->error() == QNetworkReply::NoError )
{
QVariant cookie = capabilitiesReply->header( QNetworkRequest::SetCookieHeader );
if ( !cookie.isValid() )
{
QString errorMsg = QStringLiteral( "Update request FAILED: no cookies from SP: %1" ).arg( capabilitiesReply->errorString() );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
request.setHeader( QNetworkRequest::CookieHeader, cookie );
mCookieCache.insert( request.url().host(), cookie );
return true;
}
else
{
QString errorMsg = QStringLiteral( "Update request FAILED: ECP Response from SP failed: %1" ).arg( capabilitiesReply->errorString() );
QgsMessageLog::logMessage( errorMsg, AUTH_METHOD_KEY, QgsMessageLog::CRITICAL );
return false;
}
}
return true;
}
bool QgsAuthSAML2Method::updateDataSourceUriItems( QStringList &connectionItems, const QString &authcfg,
const QString &dataprovider )
{
Q_UNUSED( dataprovider )
Q_UNUSED( connectionItems )
Q_UNUSED( authcfg )
return true;
}
bool QgsAuthSAML2Method::updateNetworkReply( QNetworkReply *reply, const QString &authcfg, const QString &dataprovider )
{
Q_UNUSED( dataprovider )
Q_UNUSED( reply )
Q_UNUSED( authcfg )
return true;
}
void QgsAuthSAML2Method::updateMethodConfig( QgsAuthMethodConfig &mconfig )
{
if ( mconfig.hasConfig( "oldconfigstyle" ) )
{
QgsDebugMsg( "Updating old style auth method config" );
QStringList conflist = mconfig.config( "oldconfigstyle" ).split( "|||" );
mconfig.setConfig( "username", conflist.at( 1 ) );
mconfig.setConfig( "password", conflist.at( 2 ) );
mconfig.setConfig( "federationurl", conflist.at( 3 ) );
mconfig.setConfig( "providername", conflist.at( 4 ) );
mconfig.setConfig( "providerurl", conflist.at( 5 ) );
mconfig.removeConfig( "oldconfigstyle" );
}
// TODO: add updates as method version() increases due to config storage changes
}
void QgsAuthSAML2Method::clearCachedConfig( const QString &authcfg )
{
removeMethodConfig( authcfg );
}
QgsAuthMethodConfig QgsAuthSAML2Method::getMethodConfig( const QString &authcfg, bool fullconfig )
{
QgsAuthMethodConfig mconfig;
// check if it is cached
if ( mAuthConfigCache.contains( authcfg ) )
{
mconfig = mAuthConfigCache.value( authcfg );
QgsDebugMsg( QString( "Retrieved config for authcfg: %1" ).arg( authcfg ) );
return mconfig;
}
// else build basic bundle
if ( !QgsAuthManager::instance()->loadAuthenticationConfig( authcfg, mconfig, fullconfig ) )
{
QgsDebugMsg( QString( "Retrieve config FAILED for authcfg: %1" ).arg( authcfg ) );
return QgsAuthMethodConfig();
}
// cache bundle
putMethodConfig( authcfg, mconfig );
return mconfig;
}
void QgsAuthSAML2Method::putMethodConfig( const QString &authcfg, const QgsAuthMethodConfig& mconfig )
{
QgsDebugMsg( QString( "Putting basic config for authcfg: %1" ).arg( authcfg ) );
mAuthConfigCache.insert( authcfg, mconfig );
}
void QgsAuthSAML2Method::removeMethodConfig( const QString &authcfg )
{
if ( mAuthConfigCache.contains( authcfg ) )
{
mAuthConfigCache.remove( authcfg );
QgsDebugMsg( QString( "Removed basic config for authcfg: %1" ).arg( authcfg ) );
}
}
//////////////////////////////////////////////
// Plugin externals
//////////////////////////////////////////////
/**
* Required class factory to return a pointer to a newly created object
*/
QGISEXTERN QgsAuthSAML2Method *classFactory()
{
return new QgsAuthSAML2Method();
}
/** Required key function (used to map the plugin to a data store type)
*/
QGISEXTERN QString authMethodKey()
{
return AUTH_METHOD_KEY;
}
/**
* Required description function
*/
QGISEXTERN QString description()
{
return AUTH_METHOD_DESCRIPTION;
}
/**
* Required isAuthMethod function. Used to determine if this shared library
* is an authentication method plugin
*/
QGISEXTERN bool isAuthMethod()
{
return true;
}
/**
* Optional class factory to return a pointer to a newly created edit widget
*/
QGISEXTERN QgsAuthSAML2Edit *editWidget( QWidget *parent )
{
return new QgsAuthSAML2Edit( parent );
}
/**
* Required cleanup function
*/
QGISEXTERN void cleanupAuthMethod() // pass QgsAuthMethod *method, then delete method ?
{
}