2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * QueryAttributeResolver.cpp
20 * AttributeResolver based on SAML queries.
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "SessionCache.h"
27 #include "attribute/Attribute.h"
28 #include "attribute/filtering/AttributeFilter.h"
29 #include "attribute/filtering/BasicFilteringContext.h"
30 #include "attribute/resolver/AttributeExtractor.h"
31 #include "attribute/resolver/AttributeResolver.h"
32 #include "attribute/resolver/ResolutionContext.h"
33 #include "binding/SOAPClient.h"
34 #include "util/SPConstants.h"
36 #include <log4cpp/Category.hh>
37 #include <saml/exceptions.h>
38 #include <saml/binding/SecurityPolicy.h>
39 #include <saml/saml1/binding/SAML1SOAPClient.h>
40 #include <saml/saml1/core/Assertions.h>
41 #include <saml/saml1/core/Protocols.h>
42 #include <saml/saml1/profile/AssertionValidator.h>
43 #include <saml/saml2/binding/SAML2SOAPClient.h>
44 #include <saml/saml2/core/Protocols.h>
45 #include <saml/saml2/metadata/Metadata.h>
46 #include <saml/saml2/metadata/MetadataProvider.h>
47 #include <saml/saml2/profile/AssertionValidator.h>
48 #include <xmltooling/util/NDC.h>
49 #include <xmltooling/util/XMLHelper.h>
50 #include <xercesc/util/XMLUniDefs.hpp>
52 using namespace shibsp;
53 using namespace opensaml::saml1;
54 using namespace opensaml::saml1p;
55 using namespace opensaml::saml2;
56 using namespace opensaml::saml2p;
57 using namespace opensaml::saml2md;
58 using namespace opensaml;
59 using namespace xmltooling;
60 using namespace log4cpp;
65 class SHIBSP_DLLLOCAL QueryContext : public ResolutionContext
68 QueryContext(const Application& application, const Session& session)
69 : m_query(true), m_app(application), m_session(&session), m_metadata(NULL), m_entity(NULL), m_nameid(session.getNameID()) {
73 const Application& application,
74 const EntityDescriptor* issuer,
76 const vector<const opensaml::Assertion*>* tokens=NULL,
77 const multimap<string,Attribute*>* attributes=NULL
78 ) : m_query(true), m_app(application), m_session(NULL), m_metadata(NULL), m_entity(issuer), m_nameid(nameid) {
81 for (vector<const opensaml::Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
82 const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(*t);
83 if (token2 && !token2->getAttributeStatements().empty()) {
87 const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(*t);
88 if (token1 && !token1->getAttributeStatements().empty()) {
99 for_each(m_attributes.begin(), m_attributes.end(), cleanup_pair<string,shibsp::Attribute>());
100 for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
103 bool doQuery() const {
107 const Application& getApplication() const {
110 const EntityDescriptor* getEntityDescriptor() const {
113 if (m_session && m_session->getEntityID()) {
114 m_metadata = m_app.getMetadataProvider();
117 return m_entity = m_metadata->getEntityDescriptor(m_session->getEntityID());
122 const NameID* getNameID() const {
125 const Session* getSession() const {
128 multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
131 vector<opensaml::Assertion*>& getResolvedAssertions() {
137 const Application& m_app;
138 const Session* m_session;
139 mutable MetadataProvider* m_metadata;
140 mutable const EntityDescriptor* m_entity;
141 const NameID* m_nameid;
142 multimap<string,shibsp::Attribute*> m_attributes;
143 vector<opensaml::Assertion*> m_assertions;
146 class SHIBSP_DLLLOCAL QueryResolver : public AttributeResolver
149 QueryResolver(const DOMElement* e);
151 for_each(m_SAML1Designators.begin(), m_SAML1Designators.end(), xmltooling::cleanup<AttributeDesignator>());
152 for_each(m_SAML2Designators.begin(), m_SAML2Designators.end(), xmltooling::cleanup<saml2::Attribute>());
155 Lockable* lock() {return this;}
158 ResolutionContext* createResolutionContext(
159 const Application& application,
160 const EntityDescriptor* issuer,
161 const NameID* nameid,
162 const vector<const opensaml::Assertion*>* tokens=NULL,
163 const multimap<string,shibsp::Attribute*>* attributes=NULL
165 return new QueryContext(application,issuer,nameid,tokens,attributes);
168 ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
169 return new QueryContext(application,session);
172 void resolveAttributes(ResolutionContext& ctx) const;
175 bool SAML1Query(QueryContext& ctx) const;
176 bool SAML2Query(QueryContext& ctx) const;
179 vector<AttributeDesignator*> m_SAML1Designators;
180 vector<saml2::Attribute*> m_SAML2Designators;
183 AttributeResolver* SHIBSP_DLLLOCAL QueryResolverFactory(const DOMElement* const & e)
185 return new QueryResolver(e);
190 void SHIBSP_API shibsp::registerAttributeResolvers()
192 SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
195 QueryResolver::QueryResolver(const DOMElement* e) : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver"))
198 xmltooling::NDC ndc("QueryResolver");
201 DOMElement* child = XMLHelper::getFirstChildElement(e);
204 if (XMLHelper::isNodeNamed(e, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
205 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
206 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
208 m_SAML2Designators.push_back(down);
212 else if (XMLHelper::isNodeNamed(e, samlconstants::SAML1P_NS, AttributeDesignator::LOCAL_NAME)) {
213 auto_ptr<XMLObject> obj(AttributeDesignatorBuilder::buildOneFromElement(child));
214 AttributeDesignator* down = dynamic_cast<AttributeDesignator*>(obj.get());
216 m_SAML1Designators.push_back(down);
221 catch (exception& ex) {
222 m_log.error("exception loading attribute designator: %s", ex.what());
224 child = XMLHelper::getNextSiblingElement(child);
228 bool QueryResolver::SAML1Query(QueryContext& ctx) const
231 xmltooling::NDC ndc("query");
235 const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML11_PROTOCOL_ENUM);
237 AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML10_PROTOCOL_ENUM);
241 m_log.info("no SAML 1.x AttributeAuthority role found in metadata");
245 shibsp::SecurityPolicy policy(ctx.getApplication());
246 MetadataCredentialCriteria mcc(*AA);
247 shibsp::SOAPClient soaper(policy);
248 const PropertySet* policySettings =
249 ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
250 pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
252 auto_ptr_XMLCh binding(samlconstants::SAML1_BINDING_SOAP);
253 saml1p::Response* response=NULL;
254 const vector<AttributeService*>& endpoints=AA->getAttributeServices();
255 for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
257 if (!XMLString::equals((*ep)->getBinding(),binding.get()))
259 auto_ptr_char loc((*ep)->getLocation());
260 auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
261 NameIdentifier* nameid = NameIdentifierBuilder::buildNameIdentifier();
262 nameid->setName(ctx.getNameID()->getName());
263 nameid->setFormat(ctx.getNameID()->getFormat());
264 nameid->setNameQualifier(ctx.getNameID()->getNameQualifier());
265 saml1::Subject* subject = saml1::SubjectBuilder::buildSubject();
266 subject->setNameIdentifier(nameid);
267 saml1p::AttributeQuery* query = saml1p::AttributeQueryBuilder::buildAttributeQuery();
268 query->setSubject(subject);
269 query->setResource(issuer.get());
270 for (vector<AttributeDesignator*>::const_iterator ad = m_SAML1Designators.begin(); ad!=m_SAML1Designators.end(); ++ad)
271 query->getAttributeDesignators().push_back((*ad)->cloneAttributeDesignator());
272 Request* request = RequestBuilder::buildRequest();
273 request->setAttributeQuery(query);
274 request->setMinorVersion(version);
276 SAML1SOAPClient client(soaper);
277 client.sendSAML(request, mcc, loc.get());
278 response = client.receiveSAML();
280 catch (exception& ex) {
281 m_log.error("exception making SAML query: %s", ex.what());
287 m_log.error("unable to successfully query for attributes");
291 const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
292 if (assertions.size()>1)
293 m_log.warn("simple resolver only supports one assertion in the query response");
295 auto_ptr<saml1p::Response> wrapper(response);
296 saml1::Assertion* newtoken = assertions.front();
298 if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
299 m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
304 policy.evaluate(*newtoken);
305 if (!policy.isSecure())
306 throw SecurityPolicyException("Security of SAML 1.x query result not established.");
307 saml1::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
308 tokval.validateAssertion(*newtoken);
310 catch (exception& ex) {
311 m_log.error("assertion failed policy/validation: %s", ex.what());
317 ctx.getResolvedAssertions().push_back(newtoken);
319 // Finally, extract and filter the result.
321 AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
323 Locker extlocker(extractor);
324 extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
327 AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
329 BasicFilteringContext fc(ctx.getApplication(), AA);
330 Locker filtlocker(filter);
331 filter->filterAttributes(fc, ctx.getResolvedAttributes());
334 catch (exception& ex) {
335 m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
336 for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
337 ctx.getResolvedAttributes().clear();
343 bool QueryResolver::SAML2Query(QueryContext& ctx) const
346 xmltooling::NDC ndc("query");
349 const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML20P_NS);
351 m_log.info("no SAML 2 AttributeAuthority role found in metadata");
355 shibsp::SecurityPolicy policy(ctx.getApplication());
356 MetadataCredentialCriteria mcc(*AA);
357 shibsp::SOAPClient soaper(policy);
358 const PropertySet* policySettings =
359 ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
360 pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
362 auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
363 saml2p::StatusResponseType* srt=NULL;
364 const vector<AttributeService*>& endpoints=AA->getAttributeServices();
365 for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
367 if (!XMLString::equals((*ep)->getBinding(),binding.get()))
369 auto_ptr_char loc((*ep)->getLocation());
370 auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
371 saml2::Subject* subject = saml2::SubjectBuilder::buildSubject();
372 subject->setNameID(ctx.getNameID()->cloneNameID());
373 saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
374 query->setSubject(subject);
375 Issuer* iss = IssuerBuilder::buildIssuer();
376 iss->setName(issuer.get());
377 query->setIssuer(iss);
378 for (vector<saml2::Attribute*>::const_iterator ad = m_SAML2Designators.begin(); ad!=m_SAML2Designators.end(); ++ad)
379 query->getAttributes().push_back((*ad)->cloneAttribute());
381 SAML2SOAPClient client(soaper);
382 client.sendSAML(query, mcc, loc.get());
383 srt = client.receiveSAML();
385 catch (exception& ex) {
386 m_log.error("exception making SAML query: %s", ex.what());
392 m_log.error("unable to successfully query for attributes");
395 saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
398 m_log.error("message was not a samlp:Response");
402 const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
403 if (assertions.size()>1)
404 m_log.warn("simple resolver only supports one assertion in the query response");
406 auto_ptr<saml2p::Response> wrapper(response);
407 saml2::Assertion* newtoken = assertions.front();
409 if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
410 m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
415 policy.evaluate(*newtoken);
416 if (!policy.isSecure())
417 throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
418 saml2::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
419 tokval.validateAssertion(*newtoken);
421 catch (exception& ex) {
422 m_log.error("assertion failed policy/validation: %s", ex.what());
428 ctx.getResolvedAssertions().push_back(newtoken);
430 // Finally, extract and filter the result.
432 AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
434 Locker extlocker(extractor);
435 extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
438 AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
440 BasicFilteringContext fc(ctx.getApplication(), AA);
441 Locker filtlocker(filter);
442 filter->filterAttributes(fc, ctx.getResolvedAttributes());
445 catch (exception& ex) {
446 m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
447 for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
448 ctx.getResolvedAttributes().clear();
454 void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
457 xmltooling::NDC ndc("resolveAttributes");
460 QueryContext& qctx = dynamic_cast<QueryContext&>(ctx);
461 if (!qctx.doQuery()) {
462 m_log.debug("found AttributeStatement in input to new session, skipping query");
466 if (qctx.getNameID() && qctx.getEntityDescriptor()) {
467 m_log.debug("attempting SAML 2.0 attribute query");
468 if (!SAML2Query(qctx)) {
469 m_log.debug("attempting SAML 1.x attribute query");
473 m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");