73169039faa77d58110f09de64be3c6f9da3b68b
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / DiscoverableMetadataProvider.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  * DiscoverableMetadataProvider.cpp
23  *
24  * A metadata provider that provides a JSON feed of IdP discovery information.
25  */
26
27 #include "internal.h"
28 #include "binding/SAMLArtifact.h"
29 #include "saml2/metadata/EntityMatcher.h"
30 #include "saml2/metadata/Metadata.h"
31 #include "saml2/metadata/DiscoverableMetadataProvider.h"
32
33 #include <fstream>
34 #include <sstream>
35 #include <boost/lambda/bind.hpp>
36 #include <boost/lambda/casts.hpp>
37 #include <boost/lambda/lambda.hpp>
38 #include <boost/iterator/indirect_iterator.hpp>
39 #include <xmltooling/logging.h>
40 #include <xmltooling/XMLToolingConfig.h>
41
42 using namespace opensaml::saml2;
43 using namespace opensaml::saml2md;
44 using namespace xmltooling::logging;
45 using namespace xmltooling;
46 using namespace boost::lambda;
47 using namespace boost;
48 using namespace std;
49
50 DiscoverableMetadataProvider::DiscoverableMetadataProvider(const DOMElement* e) : MetadataProvider(e), m_legacyOrgNames(false)
51 {
52     static const XMLCh legacyOrgNames[] =   UNICODE_LITERAL_14(l,e,g,a,c,y,O,r,g,N,a,m,e,s);
53     static const XMLCh matcher[] =          UNICODE_LITERAL_7(m,a,t,c,h,e,r);
54     static const XMLCh tagsInFeed[] =       UNICODE_LITERAL_10(t,a,g,s,I,n,F,e,e,d);
55     static const XMLCh _type[] =            UNICODE_LITERAL_4(t,y,p,e);
56     static const XMLCh DiscoveryFilter[] =  UNICODE_LITERAL_15(D,i,s,c,o,v,e,r,y,F,i,l,t,e,r);
57
58     m_legacyOrgNames = XMLHelper::getAttrBool(e, false, legacyOrgNames);
59     m_entityAttributes = XMLHelper::getAttrBool(e, false, tagsInFeed);
60
61     e = e ? XMLHelper::getFirstChildElement(e, DiscoveryFilter) : nullptr;
62     while (e) {
63         string t(XMLHelper::getAttrString(e, nullptr, _type));
64         if (t == "Whitelist" || t == "Blacklist") {
65             string m(XMLHelper::getAttrString(e, nullptr, matcher));
66             if (!m.empty()) {
67                 try {
68                     boost::shared_ptr<EntityMatcher> temp(SAMLConfig::getConfig().EntityMatcherManager.newPlugin(m, e));
69                     m_discoFilters.push_back(make_pair(t == "Whitelist", temp));
70                 }
71                 catch (std::exception& ex) {
72                     Category::getInstance(SAML_LOGCAT".MetadataProvider.Discoverable").error(
73                         "exception creating <DiscoveryFilter> EntityMatcher: %s", ex.what()
74                         );
75                 }
76             }
77             else {
78                 Category::getInstance(SAML_LOGCAT".MetadataProvider.Discoverable").error("<DiscoveryFilter> requires matcher attribute");
79             }
80         }
81         else {
82             Category::getInstance(SAML_LOGCAT".MetadataProvider.Discoverable").error(
83                 "unknown <DiscoveryFilter> type (%s)", t.empty() ? "none" : t.c_str()
84                 );
85         }
86         e = XMLHelper::getNextSiblingElement(e, DiscoveryFilter);
87     }
88 }
89
90 DiscoverableMetadataProvider::~DiscoverableMetadataProvider()
91 {
92 }
93
94 void DiscoverableMetadataProvider::generateFeed()
95 {
96     m_feed.erase();
97     bool first = true;
98     const XMLObject* object = getMetadata();
99     discoGroup(m_feed, dynamic_cast<const EntitiesDescriptor*>(object), first);
100     discoEntity(m_feed, dynamic_cast<const EntityDescriptor*>(object), first);
101
102     SAMLConfig::getConfig().generateRandomBytes(m_feedTag, 4);
103     m_feedTag = SAMLArtifact::toHex(m_feedTag);
104 }
105
106 string DiscoverableMetadataProvider::getCacheTag() const
107 {
108     return m_feedTag;
109 }
110
111 void DiscoverableMetadataProvider::outputFeed(ostream& os, bool& first, bool wrapArray) const
112 {
113     if (wrapArray)
114         os << '[';
115     if (!m_feed.empty()) {
116         if (first)
117             first = false;
118         else
119             os << ",\n";
120         os << m_feed;
121     }
122     if (wrapArray)
123         os << "\n]";
124 }
125
126 namespace {
127     static string& json_safe(string& s, const char* buf)
128     {
129         for (; *buf; ++buf) {
130             switch (*buf) {
131                 case '\\':
132                 case '"':
133                     s += '\\';
134                     s += *buf;
135                     break;
136                 case '\b':
137                     s += "\\b";
138                     break;
139                 case '\t':
140                     s += "\\t";
141                     break;
142                 case '\n':
143                     s += "\\n";
144                     break;
145                 case '\f':
146                     s += "\\f";
147                     break;
148                 case '\r':
149                     s += "\\r";
150                     break;
151                 default:
152                     s += *buf;
153             }
154         }
155         return s;
156     }
157 };
158
159 void DiscoverableMetadataProvider::discoEntity(string& s, const EntityDescriptor* entity, bool& first) const
160 {
161     time_t now = time(nullptr);
162     if (entity && entity->isValid(now)) {
163
164         // Check filter(s).
165         for (vector< pair < bool, boost::shared_ptr<EntityMatcher> > >::const_iterator f = m_discoFilters.begin(); f != m_discoFilters.end(); ++f) {
166             // The flag is true for a whitelist and false for a blacklist,
167             // so we omit the entity if the match outcome is the inverse.
168             if (f->first != f->second->matches(*entity))
169                 return;
170         }
171
172         const vector<IDPSSODescriptor*>& idps = entity->getIDPSSODescriptors();
173         if (!idps.empty()) {
174             auto_ptr_char entityid(entity->getEntityID());
175             // Open a struct and output id: entityID.
176             if (first)
177                 first = false;
178             else
179                 s += ',';
180             s += "\n{\n \"entityID\": \"";
181             json_safe(s, entityid.get());
182             s += '\"';
183             bool extFound = false;
184             for (indirect_iterator<vector<IDPSSODescriptor*>::const_iterator> idp = make_indirect_iterator(idps.begin());
185                     !extFound && idp != make_indirect_iterator(idps.end()); ++idp) {
186                 if (idp->isValid(now) && idp->getExtensions()) {
187                     const vector<XMLObject*>& exts =  const_cast<const Extensions*>(idp->getExtensions())->getUnknownXMLObjects();
188                     for (vector<XMLObject*>::const_iterator ext = exts.begin(); !extFound && ext != exts.end(); ++ext) {
189                         const UIInfo* info = dynamic_cast<UIInfo*>(*ext);
190                         if (info) {
191                             extFound = true;
192                             const vector<DisplayName*>& dispnames = info->getDisplayNames();
193                             if (!dispnames.empty()) {
194                                 s += ",\n \"DisplayNames\": [";
195                                 for (indirect_iterator<vector<DisplayName*>::const_iterator> dispname = make_indirect_iterator(dispnames.begin());
196                                         dispname != make_indirect_iterator(dispnames.end()); ++dispname) {
197                                     if (dispname.base() != dispnames.begin())
198                                         s += ',';
199                                     auto_arrayptr<char> val(toUTF8(dispname->getName()));
200                                     auto_ptr_char lang(dispname->getLang());
201                                     s += "\n  {\n  \"value\": \"";
202                                     json_safe(s, val.get());
203                                     s += "\",\n  \"lang\": \"";
204                                     s += lang.get();
205                                     s += "\"\n  }";
206                                 }
207                                 s += "\n ]";
208                             }
209
210                             const vector<Description*>& descs = info->getDescriptions();
211                             if (!descs.empty()) {
212                                 s += ",\n \"Descriptions\": [";
213                                 for (indirect_iterator<vector<Description*>::const_iterator> desc = make_indirect_iterator(descs.begin());
214                                         desc != make_indirect_iterator(descs.end()); ++desc) {
215                                     if (desc.base() != descs.begin())
216                                         s += ',';
217                                     auto_arrayptr<char> val(toUTF8(desc->getDescription()));
218                                     auto_ptr_char lang(desc->getLang());
219                                     s += "\n  {\n  \"value\": \"";
220                                     json_safe(s, val.get());
221                                     s += "\",\n  \"lang\": \"";
222                                     s += lang.get();
223                                     s += "\"\n  }";
224                                 }
225                                 s += "\n ]";
226                             }
227
228                             const vector<Keywords*>& keywords = info->getKeywordss();
229                             if (!keywords.empty()) {
230                                 s += ",\n \"Keywords\": [";
231                                 for (indirect_iterator<vector<Keywords*>::const_iterator> words = make_indirect_iterator(keywords.begin());
232                                         words != make_indirect_iterator(keywords.end()); ++words) {
233                                     if (words.base() != keywords.begin())
234                                         s += ',';
235                                     auto_arrayptr<char> val(toUTF8(words->getValues()));
236                                     auto_ptr_char lang(words->getLang());
237                                     s += "\n  {\n  \"value\": \"";
238                                     json_safe(s, val.get());
239                                     s += "\",\n  \"lang\": \"";
240                                     s += lang.get();
241                                     s += "\"\n  }";
242                                 }
243                                 s += "\n ]";
244                             }
245
246                             const vector<InformationURL*>& infurls = info->getInformationURLs();
247                             if (!infurls.empty()) {
248                                 s += ",\n \"InformationURLs\": [";
249                                 for (indirect_iterator<vector<InformationURL*>::const_iterator> infurl = make_indirect_iterator(infurls.begin());
250                                         infurl != make_indirect_iterator(infurls.end()); ++infurl) {
251                                     if (infurl.base() != infurls.begin())
252                                         s += ',';
253                                     auto_ptr_char val(infurl->getURL());
254                                     auto_ptr_char lang(infurl->getLang());
255                                     s += "\n  {\n  \"value\": \"";
256                                     json_safe(s, val.get());
257                                     s += "\",\n  \"lang\": \"";
258                                     s += lang.get();
259                                     s += "\"\n  }";
260                                 }
261                                 s += "\n ]";
262                             }
263
264                             const vector<PrivacyStatementURL*>& privs = info->getPrivacyStatementURLs();
265                             if (!privs.empty()) {
266                                 s += ",\n \"PrivacyStatementURLs\": [";
267                                 for (indirect_iterator<vector<PrivacyStatementURL*>::const_iterator> priv = make_indirect_iterator(privs.begin());
268                                         priv != make_indirect_iterator(privs.end()); ++priv) {
269                                     if (priv.base() != privs.begin())
270                                         s += ',';
271                                     auto_ptr_char val(priv->getURL());
272                                     auto_ptr_char lang(priv->getLang());
273                                     s += "\n  {\n  \"value\": \"";
274                                     json_safe(s, val.get());
275                                     s += "\",\n  \"lang\": \"";
276                                     s += lang.get();
277                                     s += "\"\n  }";
278                                 }
279                                 s += "\n ]";
280                             }
281
282                             const vector<Logo*>& logos = info->getLogos();
283                             if (!logos.empty()) {
284                                 s += ",\n \"Logos\": [";
285                                 for (indirect_iterator<vector<Logo*>::const_iterator> logo = make_indirect_iterator(logos.begin());
286                                         logo != make_indirect_iterator(logos.end()); ++logo) {
287                                     if (logo.base() != logos.begin())
288                                         s += ',';
289                                     s += "\n  {\n";
290                                     auto_ptr_char val(logo->getURL());
291                                     s += "  \"value\": \"";
292                                     json_safe(s, val.get());
293                                     ostringstream ht;
294                                     ht << logo->getHeight().second;
295                                     s += "\",\n  \"height\": \"";
296                                     s += ht.str();
297                                     ostringstream wt;
298                                     wt << logo->getWidth().second;
299                                     s += "\",\n  \"width\": \"";
300                                     s += wt.str();
301                                     s += '\"';
302                                     if (logo->getLang()) {
303                                         auto_ptr_char lang(logo->getLang());
304                                         s += ",\n  \"lang\": \"";
305                                         s += lang.get();
306                                         s += '\"';
307                                     }
308                                     s += "\n  }";
309                                 }
310                                 s += "\n ]";
311                             }
312                         }
313                     }
314                 }
315             }
316
317             if (m_legacyOrgNames && !extFound) {
318                 const Organization* org = nullptr;
319                 for (indirect_iterator<vector<IDPSSODescriptor*>::const_iterator> idp = make_indirect_iterator(idps.begin());
320                         !org && idp != make_indirect_iterator(idps.end()); ++idp) {
321                     if (idp->isValid(now))
322                         org = idp->getOrganization();
323                 }
324                 if (!org)
325                     org = entity->getOrganization();
326                 if (org) {
327                     const vector<OrganizationDisplayName*>& odns = org->getOrganizationDisplayNames();
328                     if (!odns.empty()) {
329                         s += ",\n \"DisplayNames\": [";
330                         for (indirect_iterator<vector<OrganizationDisplayName*>::const_iterator> dispname = make_indirect_iterator(odns.begin());
331                                 dispname != make_indirect_iterator(odns.end()); ++dispname) {
332                             if (dispname.base() != odns.begin())
333                                 s += ',';
334                             auto_arrayptr<char> val(toUTF8(dispname->getName()));
335                             auto_ptr_char lang(dispname->getLang());
336                             s += "\n  {\n  \"value\": \"";
337                             json_safe(s, val.get());
338                             s += "\",\n  \"lang\": \"";
339                             s += lang.get();
340                             s += "\"\n  }";
341                         }
342                         s += "\n ]";
343                     }
344                 }
345             }
346
347             if (m_entityAttributes) {
348                 bool tagfirst = true;
349                 // Check for an EntityAttributes extension in the entity and its parent(s).
350                 const Extensions* exts = entity->getExtensions();
351                 if (exts) {
352                     const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
353                     const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
354                     if (xo)
355                         discoEntityAttributes(s, *dynamic_cast<const EntityAttributes*>(xo), tagfirst);
356                 }
357
358                 const EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(entity->getParent());
359                 while (group) {
360                     exts = group->getExtensions();
361                     if (exts) {
362                         const vector<XMLObject*>& children = exts->getUnknownXMLObjects();
363                         const XMLObject* xo = find_if(children, ll_dynamic_cast<EntityAttributes*>(_1) != ((EntityAttributes*)nullptr));
364                         if (xo)
365                             discoEntityAttributes(s, *dynamic_cast<const EntityAttributes*>(xo), tagfirst);
366                     }
367                     group = dynamic_cast<EntitiesDescriptor*>(group->getParent());
368                 }
369                 if (!tagfirst)
370                     s += "\n ]";
371             }
372
373             // Close the struct;
374             s += "\n}";
375         }
376     }
377 }
378
379 void DiscoverableMetadataProvider::discoGroup(string& s, const EntitiesDescriptor* group, bool& first) const
380 {
381     if (group) {
382         for_each(
383             group->getEntitiesDescriptors().begin(), group->getEntitiesDescriptors().end(),
384             lambda::bind(&DiscoverableMetadataProvider::discoGroup, this, boost::ref(s), _1, boost::ref(first))
385             );
386         for_each(
387             group->getEntityDescriptors().begin(), group->getEntityDescriptors().end(),
388             lambda::bind(&DiscoverableMetadataProvider::discoEntity, this, boost::ref(s), _1, boost::ref(first))
389             );
390     }
391 }
392
393 void DiscoverableMetadataProvider::discoEntityAttributes(std::string& s, const EntityAttributes& ea, bool& first) const
394 {
395     discoAttributes(s, ea.getAttributes(), first);
396     const vector<saml2::Assertion*>& tokens = ea.getAssertions();
397     for (vector<saml2::Assertion*>::const_iterator t = tokens.begin(); t != tokens.end(); ++t) {
398         const vector<AttributeStatement*> statements = const_cast<const saml2::Assertion*>(*t)->getAttributeStatements();
399         for (vector<AttributeStatement*>::const_iterator st = statements.begin(); st != statements.end(); ++st) {
400             discoAttributes(s, const_cast<const AttributeStatement*>(*st)->getAttributes(), first);
401         }
402     }
403 }
404
405 void DiscoverableMetadataProvider::discoAttributes(std::string& s, const vector<Attribute*>& attrs, bool& first) const
406 {
407     for (indirect_iterator<vector<Attribute*>::const_iterator> a = make_indirect_iterator(attrs.begin());
408             a != make_indirect_iterator(attrs.end()); ++a) {
409
410         if (first) {
411             s += ",\n \"EntityAttributes\": [";
412             first = false;
413         }
414         else {
415             s += ',';
416         }
417
418         auto_ptr_char n(a->getName());
419         s += "\n  {\n  \"name\": \"";
420         json_safe(s, n.get());
421         s += "\",\n  \"values\": [";
422         const vector<XMLObject*>& vals = const_cast<const Attribute&>(*a).getAttributeValues();
423         for (indirect_iterator<vector<XMLObject*>::const_iterator> v = make_indirect_iterator(vals.begin());
424                 v != make_indirect_iterator(vals.end()); ++v) {
425             if (v.base() != vals.begin())
426                 s += ',';
427             auto_arrayptr<char> val(toUTF8(v->getTextContent()));
428             s += "\n     \"";
429             if (val.get())
430                 json_safe(s, val.get());
431             s += '\"';
432         }
433         s += "\n  ]\n  }";
434     }
435 }