2 * Copyright 2001-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 * StringAttributeDecoder.cpp
20 * Decodes SAML into SimpleAttributes.
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/SimpleAttribute.h"
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
30 using namespace shibsp;
31 using namespace opensaml::saml1;
32 using namespace opensaml::saml2;
33 using namespace xmltooling;
37 class SHIBSP_DLLLOCAL StringAttributeDecoder : virtual public AttributeDecoder
40 StringAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
41 ~StringAttributeDecoder() {}
43 shibsp::Attribute* decode(
44 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
48 AttributeDecoder* SHIBSP_DLLLOCAL StringAttributeDecoderFactory(const DOMElement* const & e)
50 return new StringAttributeDecoder(e);
54 shibsp::Attribute* StringAttributeDecoder::decode(
55 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
59 auto_ptr<SimpleAttribute> simple(new SimpleAttribute(ids));
60 vector<string>& dest = simple->getValues();
61 vector<XMLObject*>::const_iterator v,stop;
63 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.String");
65 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
66 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
68 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
71 if (log.isDebugEnabled()) {
72 auto_ptr_char n(saml2attr->getName());
74 "decoding SimpleAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
75 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
80 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
82 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
85 if (log.isDebugEnabled()) {
86 auto_ptr_char n(saml1attr->getAttributeName());
88 "decoding SimpleAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
89 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
94 log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
99 for (; v!=stop; ++v) {
100 if (!(*v)->hasChildren()) {
101 val = toUTF8((*v)->getTextContent());
105 log.warn("skipping empty AttributeValue");
109 log.warn("skipping complex AttributeValue");
113 return dest.empty() ? nullptr : _decode(simple.release());
116 const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
118 if (log.isDebugEnabled()) {
119 auto_ptr_char f(saml2name->getFormat());
120 log.debug("decoding SimpleAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
122 val = toUTF8(saml2name->getName());
125 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
127 if (log.isDebugEnabled()) {
128 auto_ptr_char f(saml1name->getFormat());
130 "decoding SimpleAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
131 ids.front().c_str(), f.get() ? f.get() : "unspecified"
134 val = toUTF8(saml1name->getName());
137 log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
145 log.warn("ignoring empty NameID");
147 return dest.empty() ? nullptr : _decode(simple.release());