2 * Copyright 2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * DiscoverableMetadataProvider.cpp
20 * A metadata provider that provides a JSON feed of IdP discovery information.
24 #include "binding/SAMLArtifact.h"
25 #include "saml2/metadata/Metadata.h"
26 #include "saml2/metadata/DiscoverableMetadataProvider.h"
30 #include <xmltooling/logging.h>
31 #include <xmltooling/XMLToolingConfig.h>
33 using namespace opensaml::saml2md;
34 using namespace xmltooling;
37 DiscoverableMetadataProvider::DiscoverableMetadataProvider(const DOMElement* e) : MetadataProvider(e), m_legacyOrgNames(false)
39 static const XMLCh legacyOrgNames[] = UNICODE_LITERAL_14(l,e,g,a,c,y,O,r,g,N,a,m,e,s);
40 m_legacyOrgNames = XMLHelper::getAttrBool(e, false, legacyOrgNames);
43 DiscoverableMetadataProvider::~DiscoverableMetadataProvider()
47 void DiscoverableMetadataProvider::generateFeed()
51 const XMLObject* object = getMetadata();
52 disco(m_feed, dynamic_cast<const EntitiesDescriptor*>(object), first);
53 disco(m_feed, dynamic_cast<const EntityDescriptor*>(object), first);
55 SAMLConfig::getConfig().generateRandomBytes(m_feedTag, 4);
56 m_feedTag = SAMLArtifact::toHex(m_feedTag);
59 string DiscoverableMetadataProvider::getCacheTag() const
64 void DiscoverableMetadataProvider::outputFeed(ostream& os, bool& first, bool wrapArray) const
68 if (!m_feed.empty()) {
79 static string& json_safe(string& s, const char* buf)
110 void DiscoverableMetadataProvider::disco(string& s, const EntityDescriptor* entity, bool& first) const
112 time_t now = time(nullptr);
113 if (entity && entity->isValid(now)) {
114 const vector<IDPSSODescriptor*>& idps = entity->getIDPSSODescriptors();
116 auto_ptr_char entityid(entity->getEntityID());
117 // Open a struct and output id: entityID.
122 s += "\n{\n \"entityID\": \"";
123 json_safe(s, entityid.get());
125 bool extFound = false;
126 for (vector<IDPSSODescriptor*>::const_iterator idp = idps.begin(); !extFound && idp != idps.end(); ++idp) {
127 if ((*idp)->isValid(now) && (*idp)->getExtensions()) {
128 const vector<XMLObject*>& exts = const_cast<const Extensions*>((*idp)->getExtensions())->getUnknownXMLObjects();
129 for (vector<XMLObject*>::const_iterator ext = exts.begin(); !extFound && ext != exts.end(); ++ext) {
130 const UIInfo* info = dynamic_cast<UIInfo*>(*ext);
133 const vector<DisplayName*>& dispnames = info->getDisplayNames();
134 if (!dispnames.empty()) {
135 s += ",\n \"DisplayNames\": [";
136 for (vector<DisplayName*>::const_iterator dispname = dispnames.begin(); dispname != dispnames.end(); ++dispname) {
137 if (dispname != dispnames.begin())
139 auto_arrayptr<char> val(toUTF8((*dispname)->getName()));
140 auto_ptr_char lang((*dispname)->getLang());
141 s += "\n {\n \"value\": \"";
142 json_safe(s, val.get());
143 s += "\",\n \"lang\": \"";
150 const vector<Description*>& descs = info->getDescriptions();
151 if (!descs.empty()) {
152 s += ",\n \"Descriptions\": [";
153 for (vector<Description*>::const_iterator desc = descs.begin(); desc != descs.end(); ++desc) {
154 if (desc != descs.begin())
156 auto_arrayptr<char> val(toUTF8((*desc)->getDescription()));
157 auto_ptr_char lang((*desc)->getLang());
158 s += "\n {\n \"value\": \"";
159 json_safe(s, val.get());
160 s += "\",\n \"lang\": \"";
167 const vector<Keywords*>& keywords = info->getKeywordss();
168 if (!keywords.empty()) {
169 s += ",\n \"Keywords\": [";
170 for (vector<Keywords*>::const_iterator words = keywords.begin(); words != keywords.end(); ++words) {
171 if (words != keywords.begin())
173 auto_arrayptr<char> val(toUTF8((*words)->getValues()));
174 auto_ptr_char lang((*words)->getLang());
175 s += "\n {\n \"value\": \"";
176 json_safe(s, val.get());
177 s += "\",\n \"lang\": \"";
184 const vector<InformationURL*>& infurls = info->getInformationURLs();
185 if (!infurls.empty()) {
186 s += ",\n \"InformationURLs\": [";
187 for (vector<InformationURL*>::const_iterator infurl = infurls.begin(); infurl != infurls.end(); ++infurl) {
188 if (infurl != infurls.begin())
190 auto_ptr_char val((*infurl)->getURL());
191 auto_ptr_char lang((*infurl)->getLang());
192 s += "\n {\n \"value\": \"";
193 json_safe(s, val.get());
194 s += "\",\n \"lang\": \"";
201 const vector<PrivacyStatementURL*>& privs = info->getPrivacyStatementURLs();
202 if (!privs.empty()) {
203 s += ",\n \"PrivacyStatementURLs\": [";
204 for (vector<PrivacyStatementURL*>::const_iterator priv = privs.begin(); priv != privs.end(); ++priv) {
205 if (priv != privs.begin())
207 auto_ptr_char val((*priv)->getURL());
208 auto_ptr_char lang((*priv)->getLang());
209 s += "\n {\n \"value\": \"";
210 json_safe(s, val.get());
211 s += "\",\n \"lang\": \"";
218 const vector<Logo*>& logos = info->getLogos();
219 if (!logos.empty()) {
220 s += ",\n \"Logos\": [";
221 for (vector<Logo*>::const_iterator logo = logos.begin(); logo != logos.end(); ++logo) {
222 if (logo != logos.begin())
225 auto_ptr_char val((*logo)->getURL());
226 s += " \"value\": \"";
227 json_safe(s, val.get());
229 ht << (*logo)->getHeight().second;
230 s += "\",\n \"height\": \"";
233 wt << (*logo)->getWidth().second;
234 s += "\",\n \"width\": \"";
237 if ((*logo)->getLang()) {
238 auto_ptr_char lang((*logo)->getLang());
239 s += ",\n \"lang\": \"";
252 if (m_legacyOrgNames && !extFound) {
253 const Organization* org = nullptr;
254 for (vector<IDPSSODescriptor*>::const_iterator idp = idps.begin(); !org && idp != idps.end(); ++idp) {
255 if ((*idp)->isValid(now))
256 org = (*idp)->getOrganization();
259 org = entity->getOrganization();
261 const vector<OrganizationDisplayName*>& odns = org->getOrganizationDisplayNames();
263 s += ",\n \"DisplayNames\": [";
264 for (vector<OrganizationDisplayName*>::const_iterator dispname = odns.begin(); dispname != odns.end(); ++dispname) {
265 if (dispname != odns.begin())
267 auto_arrayptr<char> val(toUTF8((*dispname)->getName()));
268 auto_ptr_char lang((*dispname)->getLang());
269 s += "\n {\n \"value\": \"";
270 json_safe(s, val.get());
271 s += "\",\n \"lang\": \"";
286 void DiscoverableMetadataProvider::disco(string& s, const EntitiesDescriptor* group, bool& first) const
289 const vector<EntitiesDescriptor*>& groups = group->getEntitiesDescriptors();
290 for (vector<EntitiesDescriptor*>::const_iterator i = groups.begin(); i != groups.end(); ++i)
293 const vector<EntityDescriptor*>& sites = group->getEntityDescriptors();
294 for (vector<EntityDescriptor*>::const_iterator j = sites.begin(); j != sites.end(); ++j)