Attribute filtering code.
[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(session.getNameID()) {
70         }
71         
72         QueryContext(
73             const Application& application,
74             const EntityDescriptor* issuer,
75             const NameID* nameid,
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) {
79
80             if (tokens) {
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()) {
84                         m_query = false;
85                     }
86                     else {
87                         const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(*t);
88                         if (token1 && !token1->getAttributeStatements().empty()) {
89                             m_query = false;
90                         }
91                     }
92                 }
93             }
94         }
95         
96         ~QueryContext() {
97             if (m_metadata)
98                 m_metadata->unlock();
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>());
101         }
102     
103         bool doQuery() const {
104             return m_query;
105         }
106
107         const Application& getApplication() const {
108             return m_app;
109         }
110         const EntityDescriptor* getEntityDescriptor() const {
111             if (m_entity)
112                 return m_entity;
113             if (m_session && m_session->getEntityID()) {
114                 m_metadata = m_app.getMetadataProvider();
115                 if (m_metadata) {
116                     m_metadata->lock();
117                     return m_entity = m_metadata->getEntityDescriptor(m_session->getEntityID());
118                 }
119             }
120             return NULL;
121         }
122         const NameID* getNameID() const {
123             return m_nameid;
124         }
125         const Session* getSession() const {
126             return m_session;
127         }        
128         multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
129             return m_attributes;
130         }
131         vector<opensaml::Assertion*>& getResolvedAssertions() {
132             return m_assertions;
133         }
134
135     private:
136         bool m_query;
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;
144     };
145     
146     class SHIBSP_DLLLOCAL QueryResolver : public AttributeResolver
147     {
148     public:
149         QueryResolver(const DOMElement* e);
150         ~QueryResolver() {
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>());
153         }
154
155         Lockable* lock() {return this;}
156         void unlock() {}
157         
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
164             ) const {
165             return new QueryContext(application,issuer,nameid,tokens,attributes);
166         }
167
168         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
169             return new QueryContext(application,session);
170         }
171
172         void resolveAttributes(ResolutionContext& ctx) const;
173
174     private:
175         bool SAML1Query(QueryContext& ctx) const;
176         bool SAML2Query(QueryContext& ctx) const;
177
178         Category& m_log;
179         vector<AttributeDesignator*> m_SAML1Designators;
180         vector<saml2::Attribute*> m_SAML2Designators;
181     };
182
183     AttributeResolver* SHIBSP_DLLLOCAL QueryResolverFactory(const DOMElement* const & e)
184     {
185         return new QueryResolver(e);
186     }
187     
188 };
189
190 void SHIBSP_API shibsp::registerAttributeResolvers()
191 {
192     SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
193 }
194
195 QueryResolver::QueryResolver(const DOMElement* e) : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver"))
196 {
197 #ifdef _DEBUG
198     xmltooling::NDC ndc("QueryResolver");
199 #endif
200     
201     DOMElement* child = XMLHelper::getFirstChildElement(e);
202     while (child) {
203         try {
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());
207                 if (down) {
208                     m_SAML2Designators.push_back(down);
209                     obj.release();
210                 }
211             }
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());
215                 if (down) {
216                     m_SAML1Designators.push_back(down);
217                     obj.release();
218                 }
219             }
220         }
221         catch (exception& ex) {
222             m_log.error("exception loading attribute designator: %s", ex.what());
223         }
224         child = XMLHelper::getNextSiblingElement(child);
225     }
226 }
227
228 bool QueryResolver::SAML1Query(QueryContext& ctx) const
229 {
230 #ifdef _DEBUG
231     xmltooling::NDC ndc("query");
232 #endif
233
234     int version = 1;
235     const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML11_PROTOCOL_ENUM);
236     if (!AA) {
237         AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML10_PROTOCOL_ENUM);
238         version = 0;
239     }
240     if (!AA) {
241         m_log.info("no SAML 1.x AttributeAuthority role found in metadata");
242         return false;
243     }
244
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");
251
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) {
256         try {
257             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
258                 continue;
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);
275
276             SAML1SOAPClient client(soaper);
277             client.sendSAML(request, mcc, loc.get());
278             response = client.receiveSAML();
279         }
280         catch (exception& ex) {
281             m_log.error("exception making SAML query: %s", ex.what());
282             soaper.reset();
283         }
284     }
285
286     if (!response) {
287         m_log.error("unable to successfully query for attributes");
288         return false;
289     }
290
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");
294
295     auto_ptr<saml1p::Response> wrapper(response);
296     saml1::Assertion* newtoken = assertions.front();
297
298     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
299         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
300         return true;
301     }
302
303     try {
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);
309     }
310     catch (exception& ex) {
311         m_log.error("assertion failed policy/validation: %s", ex.what());
312         return true;
313     }
314
315     newtoken->detach();
316     wrapper.release();
317     ctx.getResolvedAssertions().push_back(newtoken);
318
319     // Finally, extract and filter the result.
320     try {
321         AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
322         if (extractor) {
323             Locker extlocker(extractor);
324             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
325         }
326
327         AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
328         if (filter) {
329             BasicFilteringContext fc(ctx.getApplication(), AA);
330             Locker filtlocker(filter);
331             filter->filterAttributes(fc, ctx.getResolvedAttributes());
332         }
333     }
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();
338     }
339
340     return true;
341 }
342
343 bool QueryResolver::SAML2Query(QueryContext& ctx) const
344 {
345 #ifdef _DEBUG
346     xmltooling::NDC ndc("query");
347 #endif
348
349     const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML20P_NS);
350     if (!AA) {
351         m_log.info("no SAML 2 AttributeAuthority role found in metadata");
352         return false;
353     }
354
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");
361
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) {
366         try {
367             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
368                 continue;
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());
380
381             SAML2SOAPClient client(soaper);
382             client.sendSAML(query, mcc, loc.get());
383             srt = client.receiveSAML();
384         }
385         catch (exception& ex) {
386             m_log.error("exception making SAML query: %s", ex.what());
387             soaper.reset();
388         }
389     }
390
391     if (!srt) {
392         m_log.error("unable to successfully query for attributes");
393         return false;
394     }
395     saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
396     if (!response) {
397         delete srt;
398         m_log.error("message was not a samlp:Response");
399         return true;
400     }
401
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");
405
406     auto_ptr<saml2p::Response> wrapper(response);
407     saml2::Assertion* newtoken = assertions.front();
408
409     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
410         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
411         return true;
412     }
413
414     try {
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);
420     }
421     catch (exception& ex) {
422         m_log.error("assertion failed policy/validation: %s", ex.what());
423         return true;
424     }
425
426     newtoken->detach();
427     wrapper.release();
428     ctx.getResolvedAssertions().push_back(newtoken);
429
430     // Finally, extract and filter the result.
431     try {
432         AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
433         if (extractor) {
434             Locker extlocker(extractor);
435             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
436         }
437
438         AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
439         if (filter) {
440             BasicFilteringContext fc(ctx.getApplication(), AA);
441             Locker filtlocker(filter);
442             filter->filterAttributes(fc, ctx.getResolvedAttributes());
443         }
444     }
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();
449     }
450
451     return true;
452 }
453
454 void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
455 {
456 #ifdef _DEBUG
457     xmltooling::NDC ndc("resolveAttributes");
458 #endif
459
460     QueryContext& qctx = dynamic_cast<QueryContext&>(ctx);
461     if (!qctx.doQuery()) {
462         m_log.debug("found AttributeStatement in input to new session, skipping query");
463         return;
464     }
465
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");
470             SAML1Query(qctx);
471         }
472     }
473     m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");
474 }