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 * NameIDAttributeDecoder.cpp
24 * Decodes SAML into NameIDAttributes.
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/NameIDAttribute.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 static const XMLCh formatter[] = UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
42 static const XMLCh defaultQualifiers[] = UNICODE_LITERAL_17(d,e,f,a,u,l,t,Q,u,a,l,i,f,i,e,r,s);
44 class SHIBSP_DLLLOCAL NameIDAttributeDecoder : virtual public AttributeDecoder
47 NameIDAttributeDecoder(const DOMElement* e)
48 : AttributeDecoder(e),
49 m_formatter(XMLHelper::getAttrString(e, nullptr, formatter)),
50 m_defaultQualifiers(XMLHelper::getAttrBool(e, false, defaultQualifiers)) {
52 ~NameIDAttributeDecoder() {}
54 shibsp::Attribute* decode(
55 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
60 const NameIDType* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
63 const NameIdentifier* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
66 bool m_defaultQualifiers;
69 AttributeDecoder* SHIBSP_DLLLOCAL NameIDAttributeDecoderFactory(const DOMElement* const & e)
71 return new NameIDAttributeDecoder(e);
75 shibsp::Attribute* NameIDAttributeDecoder::decode(
76 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
79 auto_ptr<NameIDAttribute> nameid(
80 new NameIDAttribute(ids, (!m_formatter.empty()) ? m_formatter.c_str() : DEFAULT_NAMEID_FORMATTER)
82 vector<NameIDAttribute::Value>& dest = nameid->getValues();
83 vector<XMLObject*>::const_iterator v,stop;
85 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.NameID");
87 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
88 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
90 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
93 if (log.isDebugEnabled()) {
94 auto_ptr_char n(saml2attr->getName());
96 "decoding NameIDAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
97 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
102 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
104 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
107 if (log.isDebugEnabled()) {
108 auto_ptr_char n(saml1attr->getAttributeName());
110 "decoding NameIDAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
111 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
116 log.warn("XMLObject type not recognized by NameIDAttributeDecoder, no values returned");
121 for (; v!=stop; ++v) {
122 const NameIDType* n2 = dynamic_cast<const NameIDType*>(*v);
124 log.debug("decoding AttributeValue element of saml2:NameIDType type");
125 extract(n2, dest, assertingParty, relyingParty);
128 const NameIdentifier* n1=dynamic_cast<const NameIdentifier*>(*v);
130 log.debug("decoding AttributeValue element of saml1:NameIdentifier type");
131 extract(n1, dest, assertingParty, relyingParty);
133 else if ((*v)->hasChildren()) {
134 const list<XMLObject*>& values = (*v)->getOrderedChildren();
135 for (list<XMLObject*>::const_iterator vv = values.begin(); vv!=values.end(); ++vv) {
136 if (n2=dynamic_cast<const NameIDType*>(*vv)) {
137 log.debug("decoding saml2:NameID child element of AttributeValue");
138 extract(n2, dest, assertingParty, relyingParty);
140 else if (n1=dynamic_cast<const NameIdentifier*>(*vv)) {
141 log.debug("decoding saml1:NameIdentifier child element of AttributeValue");
142 extract(n1, dest, assertingParty, relyingParty);
145 log.warn("skipping AttributeValue child element not recognizable as NameID/NameIdentifier");
150 log.warn("AttributeValue was not of a supported type and contains no child elements");
155 return dest.empty() ? nullptr : _decode(nameid.release());
158 const NameIDType* saml2name = dynamic_cast<const NameIDType*>(xmlObject);
160 if (log.isDebugEnabled()) {
161 auto_ptr_char f(saml2name->getFormat());
162 log.debug("decoding NameIDAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
164 extract(saml2name, dest, assertingParty, relyingParty);
167 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
169 if (log.isDebugEnabled()) {
170 auto_ptr_char f(saml1name->getFormat());
172 "decoding NameIDAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
173 ids.front().c_str(), f.get() ? f.get() : "unspecified"
176 extract(saml1name, dest, assertingParty, relyingParty);
179 log.warn("XMLObject type not recognized by NameIDAttributeDecoder, no values returned");
184 return dest.empty() ? nullptr : _decode(nameid.release());
187 void NameIDAttributeDecoder::extract(
188 const NameIDType* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
191 auto_arrayptr<char> name(toUTF8(n->getName()));
192 if (name.get() && *name.get()) {
193 dest.push_back(NameIDAttribute::Value());
194 NameIDAttribute::Value& val = dest.back();
195 val.m_Name = name.get();
196 char* str = toUTF8(n->getFormat());
202 str = toUTF8(n->getNameQualifier());
204 val.m_NameQualifier = str;
205 else if (m_defaultQualifiers && assertingParty)
206 val.m_NameQualifier = assertingParty;
209 str = toUTF8(n->getSPNameQualifier());
211 val.m_SPNameQualifier = str;
212 else if (m_defaultQualifiers && relyingParty)
213 val.m_SPNameQualifier = relyingParty;
216 str = toUTF8(n->getSPProvidedID());
218 val.m_SPProvidedID = str;
224 void NameIDAttributeDecoder::extract(
225 const NameIdentifier* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
228 auto_arrayptr<char> name(toUTF8(n->getName()));
229 if (name.get() && *name.get()) {
230 dest.push_back(NameIDAttribute::Value());
231 NameIDAttribute::Value& val = dest.back();
232 val.m_Name = name.get();
233 char* str = toUTF8(n->getFormat());
239 str = toUTF8(n->getNameQualifier());
241 val.m_NameQualifier = str;
242 else if (m_defaultQualifiers && assertingParty)
243 val.m_NameQualifier = assertingParty;
246 if (m_defaultQualifiers && relyingParty)
247 val.m_SPNameQualifier = relyingParty;