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 * NameIDAttributeDecoder.cpp
20 * Decodes SAML into NameIDAttributes.
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/NameIDAttribute.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 static const XMLCh formatter[] = UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
38 static const XMLCh defaultQualifiers[] = UNICODE_LITERAL_17(d,e,f,a,u,l,t,Q,u,a,l,i,f,i,e,r,s);
40 class SHIBSP_DLLLOCAL NameIDAttributeDecoder : virtual public AttributeDecoder
43 NameIDAttributeDecoder(const DOMElement* e)
44 : AttributeDecoder(e), m_formatter(e ? e->getAttributeNS(nullptr, formatter) : nullptr), m_defaultQualifiers(false) {
45 const XMLCh* flag = e ? e->getAttributeNS(nullptr, defaultQualifiers) : nullptr;
46 if (flag && (*flag == chLatin_t || *flag == chDigit_1))
47 m_defaultQualifiers = true;
49 ~NameIDAttributeDecoder() {}
51 shibsp::Attribute* decode(
52 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
57 const NameIDType* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
60 const NameIdentifier* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
62 auto_ptr_char m_formatter;
63 bool m_defaultQualifiers;
66 AttributeDecoder* SHIBSP_DLLLOCAL NameIDAttributeDecoderFactory(const DOMElement* const & e)
68 return new NameIDAttributeDecoder(e);
72 shibsp::Attribute* NameIDAttributeDecoder::decode(
73 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
76 auto_ptr<NameIDAttribute> nameid(
77 new NameIDAttribute(ids, (m_formatter.get() && *m_formatter.get()) ? m_formatter.get() : DEFAULT_NAMEID_FORMATTER)
79 vector<NameIDAttribute::Value>& dest = nameid->getValues();
80 vector<XMLObject*>::const_iterator v,stop;
82 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.NameID");
84 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
85 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
87 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
90 if (log.isDebugEnabled()) {
91 auto_ptr_char n(saml2attr->getName());
93 "decoding NameIDAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
94 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
99 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
101 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
104 if (log.isDebugEnabled()) {
105 auto_ptr_char n(saml1attr->getAttributeName());
107 "decoding NameIDAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
108 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
113 log.warn("XMLObject type not recognized by NameIDAttributeDecoder, no values returned");
118 for (; v!=stop; ++v) {
119 const NameIDType* n2 = dynamic_cast<const NameIDType*>(*v);
121 extract(n2, dest, assertingParty, relyingParty);
123 const NameIdentifier* n1=dynamic_cast<const NameIdentifier*>(*v);
125 extract(n1, dest, assertingParty, relyingParty);
126 else if ((*v)->hasChildren()) {
127 const list<XMLObject*>& values = (*v)->getOrderedChildren();
128 for (list<XMLObject*>::const_iterator vv = values.begin(); vv!=values.end(); ++vv) {
129 if (n2=dynamic_cast<const NameIDType*>(*vv))
130 extract(n2, dest, assertingParty, relyingParty);
131 else if (n1=dynamic_cast<const NameIdentifier*>(*vv))
132 extract(n1, dest, assertingParty, relyingParty);
134 log.warn("skipping AttributeValue without a recognizable NameID/NameIdentifier");
140 return dest.empty() ? nullptr : _decode(nameid.release());
143 const NameIDType* saml2name = dynamic_cast<const NameIDType*>(xmlObject);
145 if (log.isDebugEnabled()) {
146 auto_ptr_char f(saml2name->getFormat());
147 log.debug("decoding NameIDAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
149 extract(saml2name, dest, assertingParty, relyingParty);
152 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
154 if (log.isDebugEnabled()) {
155 auto_ptr_char f(saml1name->getFormat());
157 "decoding NameIDAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
158 ids.front().c_str(), f.get() ? f.get() : "unspecified"
161 extract(saml1name, dest, assertingParty, relyingParty);
164 log.warn("XMLObject type not recognized by NameIDAttributeDecoder, no values returned");
169 return dest.empty() ? nullptr : _decode(nameid.release());
172 void NameIDAttributeDecoder::extract(
173 const NameIDType* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
176 auto_arrayptr<char> name(toUTF8(n->getName()));
177 if (name.get() && *name.get()) {
178 dest.push_back(NameIDAttribute::Value());
179 NameIDAttribute::Value& val = dest.back();
180 val.m_Name = name.get();
181 char* str = toUTF8(n->getFormat());
187 str = toUTF8(n->getNameQualifier());
189 val.m_NameQualifier = str;
190 else if (m_defaultQualifiers && assertingParty)
191 val.m_NameQualifier = assertingParty;
194 str = toUTF8(n->getSPNameQualifier());
196 val.m_SPNameQualifier = str;
197 else if (m_defaultQualifiers && relyingParty)
198 val.m_SPNameQualifier = relyingParty;
201 str = toUTF8(n->getSPProvidedID());
203 val.m_SPProvidedID = str;
209 void NameIDAttributeDecoder::extract(
210 const NameIdentifier* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
213 auto_arrayptr<char> name(toUTF8(n->getName()));
214 if (name.get() && *name.get()) {
215 dest.push_back(NameIDAttribute::Value());
216 NameIDAttribute::Value& val = dest.back();
217 val.m_Name = name.get();
218 char* str = toUTF8(n->getFormat());
224 str = toUTF8(n->getNameQualifier());
226 val.m_NameQualifier = str;
227 else if (m_defaultQualifiers && assertingParty)
228 val.m_NameQualifier = assertingParty;
231 if (m_defaultQualifiers && relyingParty)
232 val.m_SPNameQualifier = relyingParty;