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 * StringAttributeDecoder.cpp
24 * Decodes SAML into SimpleAttributes.
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/SimpleAttribute.h"
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
34 using namespace shibsp;
35 using namespace opensaml::saml1;
36 using namespace opensaml::saml2;
37 using namespace xmltooling;
41 class SHIBSP_DLLLOCAL StringAttributeDecoder : virtual public AttributeDecoder
44 StringAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
45 ~StringAttributeDecoder() {}
47 shibsp::Attribute* decode(
48 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
52 AttributeDecoder* SHIBSP_DLLLOCAL StringAttributeDecoderFactory(const DOMElement* const & e)
54 return new StringAttributeDecoder(e);
58 shibsp::Attribute* StringAttributeDecoder::decode(
59 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
63 auto_ptr<SimpleAttribute> simple(new SimpleAttribute(ids));
64 vector<string>& dest = simple->getValues();
65 vector<XMLObject*>::const_iterator v,stop;
67 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.String");
69 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
70 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
72 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
75 if (log.isDebugEnabled()) {
76 auto_ptr_char n(saml2attr->getName());
78 "decoding SimpleAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
79 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
84 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
86 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
89 if (log.isDebugEnabled()) {
90 auto_ptr_char n(saml1attr->getAttributeName());
92 "decoding SimpleAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
93 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
98 log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
103 for (; v!=stop; ++v) {
104 if (!(*v)->hasChildren()) {
105 val = toUTF8((*v)->getTextContent());
109 log.warn("skipping empty AttributeValue");
113 log.warn("skipping complex AttributeValue");
117 return dest.empty() ? nullptr : _decode(simple.release());
120 const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
122 if (log.isDebugEnabled()) {
123 auto_ptr_char f(saml2name->getFormat());
124 log.debug("decoding SimpleAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
126 val = toUTF8(saml2name->getName());
129 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
131 if (log.isDebugEnabled()) {
132 auto_ptr_char f(saml1name->getFormat());
134 "decoding SimpleAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
135 ids.front().c_str(), f.get() ? f.get() : "unspecified"
138 val = toUTF8(saml1name->getName());
141 log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
149 log.warn("ignoring empty NameID");
151 return dest.empty() ? nullptr : _decode(simple.release());