https://issues.shibboleth.net/jira/browse/SSPCPP-339
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / MetadataAttributeExtractor.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * MetadataAttributeExtractor.cpp
23  *
24  * AttributeExtractor for SAML metadata content.
25  */
26
27 #include "internal.h"
28 #include "Application.h"
29 #include "ServiceProvider.h"
30 #include "attribute/SimpleAttribute.h"
31 #include "attribute/AttributeDecoder.h"
32 #include "attribute/resolver/AttributeExtractor.h"
33
34 #include <boost/bind.hpp>
35 #include <boost/shared_ptr.hpp>
36 #include <boost/iterator/indirect_iterator.hpp>
37 #include <boost/tuple/tuple.hpp>
38 #include <saml/saml2/metadata/Metadata.h>
39 #include <xmltooling/util/XMLHelper.h>
40 #include <xercesc/util/XMLStringTokenizer.hpp>
41 #include <xercesc/util/XMLUniDefs.hpp>
42
43 using namespace shibsp;
44 using namespace opensaml::saml2md;
45 using namespace opensaml;
46 using namespace xmltooling;
47 using namespace boost;
48 using namespace std;
49
50 namespace shibsp {
51
52 #if defined (_MSC_VER)
53     #pragma warning( push )
54     #pragma warning( disable : 4250 )
55 #endif
56
57     class MetadataExtractor : public AttributeExtractor
58     {
59     public:
60         MetadataExtractor(const DOMElement* e);
61         ~MetadataExtractor() {}
62
63         Lockable* lock() {
64             return this;
65         }
66
67         void unlock() {
68         }
69
70         // deprecated
71         void extractAttributes(
72             const Application& application,
73             const RoleDescriptor* issuer,
74             const XMLObject& xmlObject,
75             vector<shibsp::Attribute*>& attributes
76             ) const {
77             extractAttributes(application, nullptr, issuer, xmlObject, attributes);
78         }
79
80         void extractAttributes(
81             const Application& application,
82             const GenericRequest* request,
83             const RoleDescriptor* issuer,
84             const XMLObject& xmlObject,
85             vector<shibsp::Attribute*>& attributes
86             ) const;
87         void getAttributeIds(vector<string>& attributes) const;
88
89     private:
90         string m_attributeProfiles,
91             m_errorURL,
92             m_displayName,
93             m_description,
94             m_informationURL,
95             m_privacyURL,
96             m_orgName,
97             m_orgDisplayName,
98             m_orgURL;
99         typedef tuple< string,xstring,boost::shared_ptr<AttributeDecoder> > contact_tuple_t;
100         vector<contact_tuple_t> m_contacts;  // tuple is attributeID, contact type, decoder
101
102         template <class T> void doLangSensitive(const GenericRequest*, const vector<T*>&, const string&, vector<shibsp::Attribute*>&) const;
103         void doContactPerson(const RoleDescriptor*, const contact_tuple_t&, vector<shibsp::Attribute*>&) const;
104     };
105
106 #if defined (_MSC_VER)
107     #pragma warning( pop )
108 #endif
109
110     AttributeExtractor* SHIBSP_DLLLOCAL MetadataAttributeExtractorFactory(const DOMElement* const & e)
111     {
112         return new MetadataExtractor(e);
113     }
114
115     static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
116     static const XMLCh _formatter[] = UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
117 };
118
119 MetadataExtractor::MetadataExtractor(const DOMElement* e)
120     : m_attributeProfiles(XMLHelper::getAttrString(e, nullptr, AttributeProfile::LOCAL_NAME)),
121         m_errorURL(XMLHelper::getAttrString(e, nullptr, RoleDescriptor::ERRORURL_ATTRIB_NAME)),
122         m_displayName(XMLHelper::getAttrString(e, nullptr, DisplayName::LOCAL_NAME)),
123         m_description(XMLHelper::getAttrString(e, nullptr, Description::LOCAL_NAME)),
124         m_informationURL(XMLHelper::getAttrString(e, nullptr, InformationURL::LOCAL_NAME)),
125         m_privacyURL(XMLHelper::getAttrString(e, nullptr, PrivacyStatementURL::LOCAL_NAME)),
126         m_orgName(XMLHelper::getAttrString(e, nullptr, OrganizationName::LOCAL_NAME)),
127         m_orgDisplayName(XMLHelper::getAttrString(e, nullptr, OrganizationDisplayName::LOCAL_NAME)),
128         m_orgURL(XMLHelper::getAttrString(e, nullptr, OrganizationURL::LOCAL_NAME))
129 {
130     e = e ? XMLHelper::getFirstChildElement(e, ContactPerson::LOCAL_NAME) : nullptr;
131     while (e) {
132         string id(XMLHelper::getAttrString(e, nullptr, _id));
133         const XMLCh* type = e->getAttributeNS(nullptr, ContactPerson::CONTACTTYPE_ATTRIB_NAME);
134         if (!id.empty() && type && *type) {
135             boost::shared_ptr<AttributeDecoder> decoder(SPConfig::getConfig().AttributeDecoderManager.newPlugin(DOMAttributeDecoderType, e));
136             m_contacts.push_back(contact_tuple_t(id, type, decoder));
137         }
138         e = XMLHelper::getNextSiblingElement(e, ContactPerson::LOCAL_NAME);
139     }
140 }
141
142 void MetadataExtractor::getAttributeIds(vector<string>& attributes) const
143 {
144     if (!m_attributeProfiles.empty())
145         attributes.push_back(m_attributeProfiles);
146     if (!m_errorURL.empty())
147         attributes.push_back(m_errorURL);
148     if (!m_displayName.empty())
149         attributes.push_back(m_displayName);
150     if (!m_description.empty())
151         attributes.push_back(m_description);
152     if (!m_informationURL.empty())
153         attributes.push_back(m_informationURL);
154     if (!m_privacyURL.empty())
155         attributes.push_back(m_privacyURL);
156     if (!m_orgName.empty())
157         attributes.push_back(m_orgName);
158     if (!m_orgDisplayName.empty())
159         attributes.push_back(m_orgDisplayName);
160     if (!m_orgURL.empty())
161         attributes.push_back(m_orgURL);
162     static void (vector<string>::* push_back)(const string&) = &vector<string>::push_back;
163     static const string& (contact_tuple_t::* tget)() const = &contact_tuple_t::get<0>;
164     for_each(m_contacts.begin(), m_contacts.end(), boost::bind(push_back, boost::ref(attributes), boost::bind(tget, _1)));
165 }
166
167 void MetadataExtractor::extractAttributes(
168     const Application& application,
169     const GenericRequest* request,
170     const RoleDescriptor* issuer,
171     const XMLObject& xmlObject,
172     vector<shibsp::Attribute*>& attributes
173     ) const
174 {
175     const RoleDescriptor* roleToExtract = dynamic_cast<const RoleDescriptor*>(&xmlObject);
176     if (!roleToExtract)
177         return;
178
179     if (!m_attributeProfiles.empty()) {
180         const vector<AttributeProfile*>* profiles = nullptr;
181         const IDPSSODescriptor* idpRole = dynamic_cast<const IDPSSODescriptor*>(roleToExtract);
182         if (idpRole) {
183             profiles = &(idpRole->getAttributeProfiles());
184         }
185         else {
186             const AttributeAuthorityDescriptor* aaRole = dynamic_cast<const AttributeAuthorityDescriptor*>(roleToExtract);
187             if (aaRole) {
188                 profiles = &(aaRole->getAttributeProfiles());
189             }
190         }
191         if (profiles && !profiles->empty()) {
192             auto_ptr<SimpleAttribute> attr(new SimpleAttribute(vector<string>(1, m_attributeProfiles)));
193             for (indirect_iterator<vector<AttributeProfile*>::const_iterator> i = make_indirect_iterator(profiles->begin());
194                     i != make_indirect_iterator(profiles->end()); ++i) {
195                 auto_ptr_char temp(i->getProfileURI());
196                 if (temp.get())
197                     attr->getValues().push_back(temp.get());
198             }
199             if (attr->valueCount() > 0) {
200                 attributes.push_back(attr.get());
201                 attr.release();
202             }
203         }
204     }
205
206     if (!m_errorURL.empty() && roleToExtract->getErrorURL()) {
207         auto_ptr_char temp(roleToExtract->getErrorURL());
208         if (temp.get() && *temp.get()) {
209             auto_ptr<SimpleAttribute> attr(new SimpleAttribute(vector<string>(1, m_errorURL)));
210             attr->getValues().push_back(temp.get());
211             attributes.push_back(attr.get());
212             attr.release();
213         }
214     }
215
216     if (!m_displayName.empty() || !m_description.empty() || !m_informationURL.empty() || !m_privacyURL.empty()) {
217         const Extensions* exts = roleToExtract->getExtensions();
218         if (exts) {
219             const UIInfo* ui;
220             for (vector<XMLObject*>::const_iterator ext = exts->getUnknownXMLObjects().begin(); ext != exts->getUnknownXMLObjects().end(); ++ext) {
221                 ui = dynamic_cast<const UIInfo*>(*ext);
222                 if (ui) {
223                     doLangSensitive(request, ui->getDisplayNames(), m_displayName, attributes);
224                     doLangSensitive(request, ui->getDescriptions(), m_description, attributes);
225                     doLangSensitive(request, ui->getInformationURLs(), m_informationURL, attributes);
226                     doLangSensitive(request, ui->getPrivacyStatementURLs(), m_privacyURL, attributes);
227                     break;
228                 }
229             }
230         }
231     }
232
233     if (!m_orgName.empty() || !m_orgDisplayName.empty() || !m_orgURL.empty()) {
234         const Organization* org = roleToExtract->getOrganization();
235         if (!org)
236             org = dynamic_cast<EntityDescriptor*>(roleToExtract->getParent())->getOrganization();
237         if (org) {
238             doLangSensitive(request, org->getOrganizationNames(), m_orgName, attributes);
239             doLangSensitive(request, org->getOrganizationDisplayNames(), m_orgDisplayName, attributes);
240             doLangSensitive(request, org->getOrganizationURLs(), m_orgURL, attributes);
241         }
242     }
243
244     for_each(
245         m_contacts.begin(), m_contacts.end(),
246         boost::bind(&MetadataExtractor::doContactPerson, this, roleToExtract, _1, boost::ref(attributes))
247         );
248 }
249
250 template <class T> void MetadataExtractor::doLangSensitive(
251     const GenericRequest* request, const vector<T*>& objects, const string& id, vector<shibsp::Attribute*>& attributes
252     ) const
253 {
254     if (objects.empty() || id.empty())
255         return;
256
257     T* match = nullptr;
258     if (request && request->startLangMatching()) {
259         do {
260             for (vector<T*>::const_iterator i = objects.begin(); !match && i != objects.end(); ++i) {
261                 if (request->matchLang((*i)->getLang()))
262                     match = *i;
263             }
264         } while (!match && request->continueLangMatching());
265     }
266     if (!match)
267         match = objects.front();
268
269     auto_ptr_char temp(match->getTextContent());
270     if (temp.get() && *temp.get()) {
271         auto_ptr<SimpleAttribute> attr(new SimpleAttribute(vector<string>(1, id)));
272         attr->getValues().push_back(temp.get());
273         attributes.push_back(attr.get());
274         attr.release();
275     }
276 }
277
278 void MetadataExtractor::doContactPerson(
279     const RoleDescriptor* role, const contact_tuple_t& params, vector<shibsp::Attribute*>& attributes
280     ) const
281 {
282     const XMLCh* ctype = params.get<1>().c_str();
283     static bool (*eq)(const XMLCh*, const XMLCh*) = &XMLString::equals;
284     const ContactPerson* cp = find_if(role->getContactPersons(),boost::bind(eq, ctype, boost::bind(&ContactPerson::getContactType, _1)));
285     if (!cp) {
286         cp = find_if(dynamic_cast<EntityDescriptor*>(role->getParent())->getContactPersons(),
287                 boost::bind(eq, ctype, boost::bind(&ContactPerson::getContactType, _1)));
288     }
289
290     if (cp) {
291         if (!cp->getDOM()) {
292             cp->marshall();
293         }
294         vector<string> ids(1, params.get<0>());
295         auto_ptr<Attribute> attr(params.get<2>()->decode(ids, cp));
296         if (attr.get()) {
297             attributes.push_back(attr.get());
298             attr.release();
299         }
300     }
301 }