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