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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * MetadataAttributeExtractor.cpp
24 * AttributeExtractor for SAML metadata content.
28 #include "Application.h"
29 #include "ServiceProvider.h"
30 #include "attribute/SimpleAttribute.h"
31 #include "attribute/AttributeDecoder.h"
32 #include "attribute/resolver/AttributeExtractor.h"
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>
43 using namespace shibsp;
44 using namespace opensaml::saml2md;
45 using namespace opensaml;
46 using namespace xmltooling;
47 using namespace boost;
52 #if defined (_MSC_VER)
53 #pragma warning( push )
54 #pragma warning( disable : 4250 )
57 class MetadataExtractor : public AttributeExtractor
60 MetadataExtractor(const DOMElement* e);
61 ~MetadataExtractor() {}
71 void extractAttributes(
72 const Application& application,
73 const RoleDescriptor* issuer,
74 const XMLObject& xmlObject,
75 vector<shibsp::Attribute*>& attributes
77 extractAttributes(application, nullptr, issuer, xmlObject, attributes);
80 void extractAttributes(
81 const Application& application,
82 const GenericRequest* request,
83 const RoleDescriptor* issuer,
84 const XMLObject& xmlObject,
85 vector<shibsp::Attribute*>& attributes
87 void getAttributeIds(vector<string>& attributes) const;
90 string m_attributeProfiles,
99 typedef boost::tuple< string,xstring,boost::shared_ptr<AttributeDecoder> > contact_tuple_t;
100 typedef boost::tuple< string,int,int,boost::shared_ptr<AttributeDecoder> > logo_tuple_t;
101 vector<contact_tuple_t> m_contacts; // tuple is attributeID, contact type, decoder
102 vector<logo_tuple_t> m_logos; // tuple is attributeID, height, width, decoder
104 template <class T> void doLangSensitive(const GenericRequest*, const vector<T*>&, const string&, vector<shibsp::Attribute*>&) const;
105 void doContactPerson(const RoleDescriptor*, const contact_tuple_t&, vector<shibsp::Attribute*>&) const;
106 void doLogo(const GenericRequest*, const vector<Logo*>&,const logo_tuple_t&, vector<shibsp::Attribute*>&) const;
109 #if defined (_MSC_VER)
110 #pragma warning( pop )
113 AttributeExtractor* SHIBSP_DLLLOCAL MetadataAttributeExtractorFactory(const DOMElement* const & e)
115 return new MetadataExtractor(e);
118 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
119 static const XMLCh _formatter[] = UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
122 MetadataExtractor::MetadataExtractor(const DOMElement* e)
123 : m_attributeProfiles(XMLHelper::getAttrString(e, nullptr, AttributeProfile::LOCAL_NAME)),
124 m_errorURL(XMLHelper::getAttrString(e, nullptr, RoleDescriptor::ERRORURL_ATTRIB_NAME)),
125 m_displayName(XMLHelper::getAttrString(e, nullptr, DisplayName::LOCAL_NAME)),
126 m_description(XMLHelper::getAttrString(e, nullptr, Description::LOCAL_NAME)),
127 m_informationURL(XMLHelper::getAttrString(e, nullptr, InformationURL::LOCAL_NAME)),
128 m_privacyURL(XMLHelper::getAttrString(e, nullptr, PrivacyStatementURL::LOCAL_NAME)),
129 m_orgName(XMLHelper::getAttrString(e, nullptr, OrganizationName::LOCAL_NAME)),
130 m_orgDisplayName(XMLHelper::getAttrString(e, nullptr, OrganizationDisplayName::LOCAL_NAME)),
131 m_orgURL(XMLHelper::getAttrString(e, nullptr, OrganizationURL::LOCAL_NAME))
133 e = e ? XMLHelper::getFirstChildElement(e) : nullptr;
135 if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2SPCONFIG_NS, ContactPerson::LOCAL_NAME)) {
136 string id(XMLHelper::getAttrString(e, nullptr, _id));
137 const XMLCh* type = e->getAttributeNS(nullptr, ContactPerson::CONTACTTYPE_ATTRIB_NAME);
138 if (!id.empty() && type && *type) {
139 boost::shared_ptr<AttributeDecoder> decoder(SPConfig::getConfig().AttributeDecoderManager.newPlugin(DOMAttributeDecoderType, e));
140 m_contacts.push_back(contact_tuple_t(id, type, decoder));
143 else if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2SPCONFIG_NS, Logo::LOCAL_NAME)) {
144 string id(XMLHelper::getAttrString(e, nullptr, _id));
145 int h(XMLHelper::getAttrInt(e, 0, Logo::HEIGHT_ATTRIB_NAME));
146 int w(XMLHelper::getAttrInt(e, 0, Logo::WIDTH_ATTRIB_NAME));
148 boost::shared_ptr<AttributeDecoder> decoder(SPConfig::getConfig().AttributeDecoderManager.newPlugin(DOMAttributeDecoderType, e));
149 m_logos.push_back(logo_tuple_t(id, h, w, decoder));
152 e = XMLHelper::getNextSiblingElement(e);
156 void MetadataExtractor::getAttributeIds(vector<string>& attributes) const
158 if (!m_attributeProfiles.empty())
159 attributes.push_back(m_attributeProfiles);
160 if (!m_errorURL.empty())
161 attributes.push_back(m_errorURL);
162 if (!m_displayName.empty())
163 attributes.push_back(m_displayName);
164 if (!m_description.empty())
165 attributes.push_back(m_description);
166 if (!m_informationURL.empty())
167 attributes.push_back(m_informationURL);
168 if (!m_privacyURL.empty())
169 attributes.push_back(m_privacyURL);
170 if (!m_orgName.empty())
171 attributes.push_back(m_orgName);
172 if (!m_orgDisplayName.empty())
173 attributes.push_back(m_orgDisplayName);
174 if (!m_orgURL.empty())
175 attributes.push_back(m_orgURL);
176 for (vector<contact_tuple_t>::const_iterator c = m_contacts.begin(); c != m_contacts.end(); ++c)
177 attributes.push_back(c->get<0>());
178 for (vector<logo_tuple_t>::const_iterator l = m_logos.begin(); l != m_logos.end(); ++l)
179 attributes.push_back(l->get<0>());
182 void MetadataExtractor::extractAttributes(
183 const Application& application,
184 const GenericRequest* request,
185 const RoleDescriptor* issuer,
186 const XMLObject& xmlObject,
187 vector<shibsp::Attribute*>& attributes
190 const RoleDescriptor* roleToExtract = dynamic_cast<const RoleDescriptor*>(&xmlObject);
194 if (!m_attributeProfiles.empty()) {
195 const vector<AttributeProfile*>* profiles = nullptr;
196 const IDPSSODescriptor* idpRole = dynamic_cast<const IDPSSODescriptor*>(roleToExtract);
198 profiles = &(idpRole->getAttributeProfiles());
201 const AttributeAuthorityDescriptor* aaRole = dynamic_cast<const AttributeAuthorityDescriptor*>(roleToExtract);
203 profiles = &(aaRole->getAttributeProfiles());
206 if (profiles && !profiles->empty()) {
207 auto_ptr<SimpleAttribute> attr(new SimpleAttribute(vector<string>(1, m_attributeProfiles)));
208 for (indirect_iterator<vector<AttributeProfile*>::const_iterator> i = make_indirect_iterator(profiles->begin());
209 i != make_indirect_iterator(profiles->end()); ++i) {
210 auto_ptr_char temp(i->getProfileURI());
212 attr->getValues().push_back(temp.get());
214 if (attr->valueCount() > 0) {
215 attributes.push_back(attr.get());
221 if (!m_errorURL.empty() && roleToExtract->getErrorURL()) {
222 auto_ptr_char temp(roleToExtract->getErrorURL());
223 if (temp.get() && *temp.get()) {
224 auto_ptr<SimpleAttribute> attr(new SimpleAttribute(vector<string>(1, m_errorURL)));
225 attr->getValues().push_back(temp.get());
226 attributes.push_back(attr.get());
231 if (!m_displayName.empty() || !m_description.empty() || !m_informationURL.empty() || !m_privacyURL.empty()) {
232 const Extensions* exts = roleToExtract->getExtensions();
235 for (vector<XMLObject*>::const_iterator ext = exts->getUnknownXMLObjects().begin(); ext != exts->getUnknownXMLObjects().end(); ++ext) {
236 ui = dynamic_cast<const UIInfo*>(*ext);
238 doLangSensitive(request, ui->getDisplayNames(), m_displayName, attributes);
239 doLangSensitive(request, ui->getDescriptions(), m_description, attributes);
240 doLangSensitive(request, ui->getInformationURLs(), m_informationURL, attributes);
241 doLangSensitive(request, ui->getPrivacyStatementURLs(), m_privacyURL, attributes);
242 const vector<Logo*>& logos = ui->getLogos();
243 if (!logos.empty()) {
245 m_logos.begin(), m_logos.end(),
246 boost::bind(&MetadataExtractor::doLogo, this, request, boost::ref(logos), _1, boost::ref(attributes))
255 if (!m_orgName.empty() || !m_orgDisplayName.empty() || !m_orgURL.empty()) {
256 const Organization* org = roleToExtract->getOrganization();
258 org = dynamic_cast<EntityDescriptor*>(roleToExtract->getParent())->getOrganization();
260 doLangSensitive(request, org->getOrganizationNames(), m_orgName, attributes);
261 doLangSensitive(request, org->getOrganizationDisplayNames(), m_orgDisplayName, attributes);
262 doLangSensitive(request, org->getOrganizationURLs(), m_orgURL, attributes);
267 m_contacts.begin(), m_contacts.end(),
268 boost::bind(&MetadataExtractor::doContactPerson, this, roleToExtract, _1, boost::ref(attributes))
272 template <class T> void MetadataExtractor::doLangSensitive(
273 const GenericRequest* request, const vector<T*>& objects, const string& id, vector<shibsp::Attribute*>& attributes
276 if (objects.empty() || id.empty())
280 if (request && request->startLangMatching()) {
282 for (typename vector<T*>::const_iterator i = objects.begin(); !match && i != objects.end(); ++i) {
283 if (request->matchLang((*i)->getLang()))
286 } while (!match && request->continueLangMatching());
289 match = objects.front();
291 auto_arrayptr<char> temp(toUTF8(match->getTextContent()));
292 if (temp.get() && *temp.get()) {
293 auto_ptr<SimpleAttribute> attr(new SimpleAttribute(vector<string>(1, id)));
294 attr->getValues().push_back(temp.get());
295 attributes.push_back(attr.get());
300 void MetadataExtractor::doLogo(
301 const GenericRequest* request, const vector<Logo*>& logos, const logo_tuple_t& params, vector<shibsp::Attribute*>& attributes
308 Logo* match = nullptr;
309 int h = params.get<1>(), w = params.get<2>(), sizediff, bestdiff = INT_MAX;
310 if (request && request->startLangMatching()) {
312 for (vector<Logo*>::const_iterator i = logos.begin(); i != logos.end(); ++i) {
313 if (!(*i)->getLang() || request->matchLang((*i)->getLang())) {
316 dim = (*i)->getHeight();
317 sizediff += abs(h - dim.second);
320 dim = (*i)->getWidth();
321 sizediff += abs(w - dim.second);
323 if (sizediff < bestdiff) {
328 if (match && bestdiff == 0)
331 if (match && bestdiff == 0)
333 } while (request->continueLangMatching());
335 else if (h > 0 || w > 0) {
336 for (vector<Logo*>::const_iterator i = logos.begin(); i != logos.end(); ++i) {
339 dim = (*i)->getHeight();
340 sizediff += abs(h - dim.second);
343 dim = (*i)->getWidth();
344 sizediff += abs(w - dim.second);
346 if (sizediff < bestdiff) {
350 if (match && bestdiff == 0)
356 match = logos.front();
358 if (!match->getDOM()) {
361 vector<string> ids(1, params.get<0>());
362 auto_ptr<Attribute> attr(params.get<3>()->decode(ids, match));
364 attributes.push_back(attr.get());
369 void MetadataExtractor::doContactPerson(
370 const RoleDescriptor* role, const contact_tuple_t& params, vector<shibsp::Attribute*>& attributes
373 const XMLCh* ctype = params.get<1>().c_str();
374 static bool (*eq)(const XMLCh*, const XMLCh*) = &XMLString::equals;
375 const ContactPerson* cp = find_if(role->getContactPersons(),boost::bind(eq, ctype, boost::bind(&ContactPerson::getContactType, _1)));
377 cp = find_if(dynamic_cast<EntityDescriptor*>(role->getParent())->getContactPersons(),
378 boost::bind(eq, ctype, boost::bind(&ContactPerson::getContactType, _1)));
385 vector<string> ids(1, params.get<0>());
386 auto_ptr<Attribute> attr(params.get<2>()->decode(ids, cp));
388 attributes.push_back(attr.get());