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 * XMLAttributeDecoder.cpp
24 * Decodes arbitrary XML into an XMLAttribute.
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/XMLAttribute.h"
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33 #include <xmltooling/util/XMLHelper.h>
35 using namespace shibsp;
36 using namespace opensaml;
37 using namespace xmltooling;
41 class SHIBSP_DLLLOCAL XMLAttributeDecoder : virtual public AttributeDecoder
44 XMLAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
45 ~XMLAttributeDecoder() {}
49 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
51 return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
55 const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
59 DDF convert(DOMElement* e, bool nameit=true) const;
60 auto_ptr_char m_formatter;
61 map<pair<xstring,xstring>,string> m_tagMap;
64 AttributeDecoder* SHIBSP_DLLLOCAL XMLAttributeDecoderFactory(const DOMElement* const & e)
66 return new XMLAttributeDecoder(e);
71 Attribute* XMLAttributeDecoder::decode(
72 const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
78 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.XML");
80 auto_ptr<XMLAttribute> attr(new XMLAttribute(ids));
81 vector<string>& dest = attr->getValues();
83 // Handle any non-Attribute object directly.
84 if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
85 DOMElement* e = xmlObject->getDOM();
87 if (log.isDebugEnabled()) {
89 "decoding XMLAttribute (%s) from XMLObject (%s)",
91 (xmlObject->getSchemaType() ? xmlObject->getSchemaType()->toString() : xmlObject->getElementQName().toString()).c_str()
94 dest.push_back(string());
95 XMLHelper::serialize(e, dest.back());
98 log.warn("skipping XMLObject without a backing DOM");
100 return dest.empty() ? nullptr : _decode(attr.release());
103 pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
105 const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
107 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
108 valrange = valueRange(request, values);
109 if (log.isDebugEnabled()) {
110 auto_ptr_char n(saml2attr->getName());
112 "decoding XMLAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
113 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
118 const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
120 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
121 valrange = valueRange(request, values);
122 if (log.isDebugEnabled()) {
123 auto_ptr_char n(saml1attr->getAttributeName());
125 "decoding XMLAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
126 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
131 log.warn("XMLObject type not recognized by XMLAttributeDecoder, no values returned");
136 for (; valrange.first != valrange.second; ++valrange.first) {
137 DOMElement* e = (*valrange.first)->getDOM();
139 dest.push_back(string());
140 XMLHelper::serialize(e, dest.back());
143 log.warn("skipping AttributeValue without a backing DOM");
146 return dest.empty() ? nullptr : _decode(attr.release());