Genericize string values and scopes, add value/scope functors.
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / QueryAttributeResolver.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * QueryAttributeResolver.cpp
19  * 
20  * AttributeResolver based on SAML queries.
21  */
22
23 #include "internal.h"
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"
35
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>
51
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;
61 using namespace std;
62
63 namespace shibsp {
64
65     class SHIBSP_DLLLOCAL QueryContext : public ResolutionContext
66     {
67     public:
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(NULL) {
70         }
71         
72         QueryContext(
73             const Application& application,
74             const EntityDescriptor* issuer,
75             const NameID* nameid,
76             const char* authncontext_class=NULL,
77             const char* authncontext_decl=NULL,
78             const vector<const opensaml::Assertion*>* tokens=NULL,
79             const multimap<string,Attribute*>* attributes=NULL
80             ) : m_query(true), m_app(application), m_session(NULL), m_metadata(NULL), m_entity(issuer),
81                 m_nameid(nameid), m_class(authncontext_class), m_decl(authncontext_decl) {
82
83             if (tokens) {
84                 for (vector<const opensaml::Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
85                     const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(*t);
86                     if (token2 && !token2->getAttributeStatements().empty()) {
87                         m_query = false;
88                     }
89                     else {
90                         const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(*t);
91                         if (token1 && !token1->getAttributeStatements().empty()) {
92                             m_query = false;
93                         }
94                     }
95                 }
96             }
97         }
98         
99         ~QueryContext() {
100             if (m_metadata)
101                 m_metadata->unlock();
102             for_each(m_attributes.begin(), m_attributes.end(), cleanup_pair<string,shibsp::Attribute>());
103             for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
104         }
105     
106         bool doQuery() const {
107             return m_query;
108         }
109
110         const Application& getApplication() const {
111             return m_app;
112         }
113         const EntityDescriptor* getEntityDescriptor() const {
114             if (m_entity)
115                 return m_entity;
116             if (m_session && m_session->getEntityID()) {
117                 m_metadata = m_app.getMetadataProvider();
118                 if (m_metadata) {
119                     m_metadata->lock();
120                     return m_entity = m_metadata->getEntityDescriptor(m_session->getEntityID());
121                 }
122             }
123             return NULL;
124         }
125         const NameID* getNameID() const {
126             return m_session ? m_session->getNameID() : m_nameid;
127         }
128         const char* getClassRef() const {
129             return m_session ? m_session->getAuthnContextClassRef() :  m_class;
130         }
131         const char* getDeclRef() const {
132             return m_session ? m_session->getAuthnContextDeclRef() : m_decl;
133         }
134         const Session* getSession() const {
135             return m_session;
136         }
137         multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
138             return m_attributes;
139         }
140         vector<opensaml::Assertion*>& getResolvedAssertions() {
141             return m_assertions;
142         }
143
144     private:
145         bool m_query;
146         const Application& m_app;
147         const Session* m_session;
148         mutable MetadataProvider* m_metadata;
149         mutable const EntityDescriptor* m_entity;
150         const NameID* m_nameid;
151         const char* m_class;
152         const char* m_decl;
153         multimap<string,shibsp::Attribute*> m_attributes;
154         vector<opensaml::Assertion*> m_assertions;
155     };
156     
157     class SHIBSP_DLLLOCAL QueryResolver : public AttributeResolver
158     {
159     public:
160         QueryResolver(const DOMElement* e);
161         ~QueryResolver() {
162             for_each(m_SAML1Designators.begin(), m_SAML1Designators.end(), xmltooling::cleanup<AttributeDesignator>());
163             for_each(m_SAML2Designators.begin(), m_SAML2Designators.end(), xmltooling::cleanup<saml2::Attribute>());
164         }
165
166         Lockable* lock() {return this;}
167         void unlock() {}
168         
169         ResolutionContext* createResolutionContext(
170             const Application& application,
171             const EntityDescriptor* issuer,
172             const NameID* nameid,
173             const char* authncontext_class=NULL,
174             const char* authncontext_decl=NULL,
175             const vector<const opensaml::Assertion*>* tokens=NULL,
176             const multimap<string,shibsp::Attribute*>* attributes=NULL
177             ) const {
178             return new QueryContext(application,issuer,nameid,authncontext_class,authncontext_decl,tokens,attributes);
179         }
180
181         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
182             return new QueryContext(application,session);
183         }
184
185         void resolveAttributes(ResolutionContext& ctx) const;
186
187     private:
188         bool SAML1Query(QueryContext& ctx) const;
189         bool SAML2Query(QueryContext& ctx) const;
190
191         Category& m_log;
192         vector<AttributeDesignator*> m_SAML1Designators;
193         vector<saml2::Attribute*> m_SAML2Designators;
194     };
195
196     AttributeResolver* SHIBSP_DLLLOCAL QueryResolverFactory(const DOMElement* const & e)
197     {
198         return new QueryResolver(e);
199     }
200     
201 };
202
203 void SHIBSP_API shibsp::registerAttributeResolvers()
204 {
205     SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
206 }
207
208 QueryResolver::QueryResolver(const DOMElement* e) : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver"))
209 {
210 #ifdef _DEBUG
211     xmltooling::NDC ndc("QueryResolver");
212 #endif
213     
214     DOMElement* child = XMLHelper::getFirstChildElement(e);
215     while (child) {
216         try {
217             if (XMLHelper::isNodeNamed(e, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
218                 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
219                 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
220                 if (down) {
221                     m_SAML2Designators.push_back(down);
222                     obj.release();
223                 }
224             }
225             else if (XMLHelper::isNodeNamed(e, samlconstants::SAML1P_NS, AttributeDesignator::LOCAL_NAME)) {
226                 auto_ptr<XMLObject> obj(AttributeDesignatorBuilder::buildOneFromElement(child));
227                 AttributeDesignator* down = dynamic_cast<AttributeDesignator*>(obj.get());
228                 if (down) {
229                     m_SAML1Designators.push_back(down);
230                     obj.release();
231                 }
232             }
233         }
234         catch (exception& ex) {
235             m_log.error("exception loading attribute designator: %s", ex.what());
236         }
237         child = XMLHelper::getNextSiblingElement(child);
238     }
239 }
240
241 bool QueryResolver::SAML1Query(QueryContext& ctx) const
242 {
243 #ifdef _DEBUG
244     xmltooling::NDC ndc("query");
245 #endif
246
247     int version = 1;
248     const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML11_PROTOCOL_ENUM);
249     if (!AA) {
250         AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML10_PROTOCOL_ENUM);
251         version = 0;
252     }
253     if (!AA) {
254         m_log.info("no SAML 1.x AttributeAuthority role found in metadata");
255         return false;
256     }
257
258     shibsp::SecurityPolicy policy(ctx.getApplication());
259     MetadataCredentialCriteria mcc(*AA);
260     shibsp::SOAPClient soaper(policy);
261     const PropertySet* policySettings =
262         ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
263     pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
264
265     auto_ptr_XMLCh binding(samlconstants::SAML1_BINDING_SOAP);
266     saml1p::Response* response=NULL;
267     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
268     for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
269         try {
270             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
271                 continue;
272             auto_ptr_char loc((*ep)->getLocation());
273             auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
274             NameIdentifier* nameid = NameIdentifierBuilder::buildNameIdentifier();
275             nameid->setName(ctx.getNameID()->getName());
276             nameid->setFormat(ctx.getNameID()->getFormat());
277             nameid->setNameQualifier(ctx.getNameID()->getNameQualifier());
278             saml1::Subject* subject = saml1::SubjectBuilder::buildSubject();
279             subject->setNameIdentifier(nameid);
280             saml1p::AttributeQuery* query = saml1p::AttributeQueryBuilder::buildAttributeQuery();
281             query->setSubject(subject);
282             query->setResource(issuer.get());
283             for (vector<AttributeDesignator*>::const_iterator ad = m_SAML1Designators.begin(); ad!=m_SAML1Designators.end(); ++ad)
284                 query->getAttributeDesignators().push_back((*ad)->cloneAttributeDesignator());
285             Request* request = RequestBuilder::buildRequest();
286             request->setAttributeQuery(query);
287             request->setMinorVersion(version);
288
289             SAML1SOAPClient client(soaper);
290             client.sendSAML(request, mcc, loc.get());
291             response = client.receiveSAML();
292         }
293         catch (exception& ex) {
294             m_log.error("exception making SAML query: %s", ex.what());
295             soaper.reset();
296         }
297     }
298
299     if (!response) {
300         m_log.error("unable to successfully query for attributes");
301         return false;
302     }
303
304     const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
305     if (assertions.size()>1)
306         m_log.warn("simple resolver only supports one assertion in the query response");
307
308     auto_ptr<saml1p::Response> wrapper(response);
309     saml1::Assertion* newtoken = assertions.front();
310
311     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
312         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
313         return true;
314     }
315
316     try {
317         policy.evaluate(*newtoken);
318         if (!policy.isSecure())
319             throw SecurityPolicyException("Security of SAML 1.x query result not established.");
320         saml1::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
321         tokval.validateAssertion(*newtoken);
322     }
323     catch (exception& ex) {
324         m_log.error("assertion failed policy/validation: %s", ex.what());
325         return true;
326     }
327
328     newtoken->detach();
329     wrapper.release();
330     ctx.getResolvedAssertions().push_back(newtoken);
331
332     // Finally, extract and filter the result.
333     try {
334         AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
335         if (extractor) {
336             Locker extlocker(extractor);
337             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
338         }
339
340         AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
341         if (filter) {
342             BasicFilteringContext fc(ctx.getApplication(), ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
343             Locker filtlocker(filter);
344             filter->filterAttributes(fc, ctx.getResolvedAttributes());
345         }
346     }
347     catch (exception& ex) {
348         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
349         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
350         ctx.getResolvedAttributes().clear();
351     }
352
353     return true;
354 }
355
356 bool QueryResolver::SAML2Query(QueryContext& ctx) const
357 {
358 #ifdef _DEBUG
359     xmltooling::NDC ndc("query");
360 #endif
361
362     const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML20P_NS);
363     if (!AA) {
364         m_log.info("no SAML 2 AttributeAuthority role found in metadata");
365         return false;
366     }
367
368     shibsp::SecurityPolicy policy(ctx.getApplication());
369     MetadataCredentialCriteria mcc(*AA);
370     shibsp::SOAPClient soaper(policy);
371     const PropertySet* policySettings =
372         ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
373     pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
374
375     auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
376     saml2p::StatusResponseType* srt=NULL;
377     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
378     for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
379         try {
380             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
381                 continue;
382             auto_ptr_char loc((*ep)->getLocation());
383             auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
384             saml2::Subject* subject = saml2::SubjectBuilder::buildSubject();
385             subject->setNameID(ctx.getNameID()->cloneNameID());
386             saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
387             query->setSubject(subject);
388             Issuer* iss = IssuerBuilder::buildIssuer();
389             iss->setName(issuer.get());
390             query->setIssuer(iss);
391             for (vector<saml2::Attribute*>::const_iterator ad = m_SAML2Designators.begin(); ad!=m_SAML2Designators.end(); ++ad)
392                 query->getAttributes().push_back((*ad)->cloneAttribute());
393
394             SAML2SOAPClient client(soaper);
395             client.sendSAML(query, mcc, loc.get());
396             srt = client.receiveSAML();
397         }
398         catch (exception& ex) {
399             m_log.error("exception making SAML query: %s", ex.what());
400             soaper.reset();
401         }
402     }
403
404     if (!srt) {
405         m_log.error("unable to successfully query for attributes");
406         return false;
407     }
408     saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
409     if (!response) {
410         delete srt;
411         m_log.error("message was not a samlp:Response");
412         return true;
413     }
414
415     const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
416     if (assertions.size()>1)
417         m_log.warn("simple resolver only supports one assertion in the query response");
418
419     auto_ptr<saml2p::Response> wrapper(response);
420     saml2::Assertion* newtoken = assertions.front();
421
422     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
423         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
424         return true;
425     }
426
427     try {
428         policy.evaluate(*newtoken);
429         if (!policy.isSecure())
430             throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
431         saml2::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
432         tokval.validateAssertion(*newtoken);
433     }
434     catch (exception& ex) {
435         m_log.error("assertion failed policy/validation: %s", ex.what());
436         return true;
437     }
438
439     newtoken->detach();
440     wrapper.release();
441     ctx.getResolvedAssertions().push_back(newtoken);
442
443     // Finally, extract and filter the result.
444     try {
445         AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
446         if (extractor) {
447             Locker extlocker(extractor);
448             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
449         }
450
451         AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
452         if (filter) {
453             BasicFilteringContext fc(ctx.getApplication(), ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
454             Locker filtlocker(filter);
455             filter->filterAttributes(fc, ctx.getResolvedAttributes());
456         }
457     }
458     catch (exception& ex) {
459         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
460         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
461         ctx.getResolvedAttributes().clear();
462     }
463
464     return true;
465 }
466
467 void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
468 {
469 #ifdef _DEBUG
470     xmltooling::NDC ndc("resolveAttributes");
471 #endif
472
473     QueryContext& qctx = dynamic_cast<QueryContext&>(ctx);
474     if (!qctx.doQuery()) {
475         m_log.debug("found AttributeStatement in input to new session, skipping query");
476         return;
477     }
478
479     if (qctx.getNameID() && qctx.getEntityDescriptor()) {
480         m_log.debug("attempting SAML 2.0 attribute query");
481         if (!SAML2Query(qctx)) {
482             m_log.debug("attempting SAML 1.x attribute query");
483             SAML1Query(qctx);
484         }
485     }
486     m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");
487 }