2 * Copyright 2010 The Danish CLARIN Consortium
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 * Base64AttributeDecoder.cpp
20 * Decodes SAML containing base64-encoded values 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 #include <xercesc/util/Base64.hpp>
32 using namespace shibsp;
33 using namespace opensaml::saml1;
34 using namespace opensaml::saml2;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
40 class SHIBSP_DLLLOCAL Base64AttributeDecoder : virtual public AttributeDecoder
43 Base64AttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
44 ~Base64AttributeDecoder() {}
46 shibsp::Attribute* decode(
47 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
51 AttributeDecoder* SHIBSP_DLLLOCAL Base64AttributeDecoderFactory(const DOMElement* const & e)
53 return new Base64AttributeDecoder(e);
57 shibsp::Attribute* Base64AttributeDecoder::decode(
58 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
61 auto_ptr<SimpleAttribute> simple(new SimpleAttribute(ids));
62 vector<string>& dest = simple->getValues();
63 vector<XMLObject*>::const_iterator v,stop;
65 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.Base64");
67 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
68 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
70 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
73 if (log.isDebugEnabled()) {
74 auto_ptr_char n(saml2attr->getName());
76 "decoding SimpleAttribute (%s) from SAML 2 Attribute (%s) with %lu base64-encoded value(s)",
77 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
82 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
84 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
87 if (log.isDebugEnabled()) {
88 auto_ptr_char n(saml1attr->getAttributeName());
90 "decoding SimpleAttribute (%s) from SAML 1 Attribute (%s) with %lu base64-encoded value(s)",
91 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
96 log.warn("XMLObject type not recognized by Base64AttributeDecoder, no values returned");
101 for (; v!=stop; ++v) {
102 if (!(*v)->hasChildren()) {
103 auto_ptr_char val((*v)->getTextContent());
104 if (val.get() && *val.get()) {
106 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(val.get()),&x);
108 dest.push_back(reinterpret_cast<char*>(decoded));
109 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
110 XMLString::release(&decoded);
112 XMLString::release((char**)&decoded);
116 log.warn("skipping AttributeValue, unable to base64-decode");
120 log.warn("skipping empty AttributeValue");
123 log.warn("skipping complex AttributeValue");
127 return dest.empty() ? nullptr : _decode(simple.release());
130 const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
132 if (log.isDebugEnabled()) {
133 auto_ptr_char f(saml2name->getFormat());
134 log.debug("decoding SimpleAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
136 auto_ptr_char val(saml2name->getName());
137 if (val.get() && *val.get()) {
139 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(val.get()),&x);
141 dest.push_back(reinterpret_cast<char*>(decoded));
142 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
143 XMLString::release(&decoded);
145 XMLString::release((char**)&decoded);
149 log.warn("ignoring NameID, unable to base64-decode");
153 log.warn("ignoring empty NameID");
156 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
158 if (log.isDebugEnabled()) {
159 auto_ptr_char f(saml1name->getFormat());
161 "decoding SimpleAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
162 ids.front().c_str(), f.get() ? f.get() : "unspecified"
165 auto_ptr_char val(saml1name->getName());
166 if (val.get() && *val.get()) {
168 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(val.get()),&x);
170 dest.push_back(reinterpret_cast<char*>(decoded));
171 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
172 XMLString::release(&decoded);
174 XMLString::release((char**)&decoded);
178 log.warn("ignoring NameIdentifier, unable to base64-decode");
182 log.warn("ignoring empty NameIdentifier");
185 log.warn("XMLObject type not recognized by Base64AttributeDecoder, no values returned");
190 return dest.empty() ? nullptr : _decode(simple.release());