2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * NameIDAttributeDecoder.cpp
\r
20 * Decodes SAML into NameIDAttributes
\r
23 #include "internal.h"
\r
24 #include "attribute/AttributeDecoder.h"
\r
25 #include "attribute/NameIDAttribute.h"
\r
27 #include <saml/saml1/core/Assertions.h>
\r
28 #include <saml/saml2/core/Assertions.h>
\r
30 using namespace shibsp;
\r
31 using namespace opensaml::saml1;
\r
32 using namespace opensaml::saml2;
\r
33 using namespace xmltooling;
\r
34 using namespace std;
\r
37 static XMLCh formatter[] = UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
\r
39 class SHIBSP_DLLLOCAL NameIDAttributeDecoder : virtual public AttributeDecoder
\r
42 NameIDAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_formatter(e ? e->getAttributeNS(NULL,formatter) : NULL) {}
\r
43 ~NameIDAttributeDecoder() {}
\r
45 shibsp::Attribute* decode(
\r
46 const char* id, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL
\r
51 const NameIDType* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty=NULL, const char* relyingParty=NULL
\r
54 const NameIdentifier* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty=NULL, const char* relyingParty=NULL
\r
56 auto_ptr_char m_formatter;
\r
59 AttributeDecoder* SHIBSP_DLLLOCAL NameIDAttributeDecoderFactory(const DOMElement* const & e)
\r
61 return new NameIDAttributeDecoder(e);
\r
65 shibsp::Attribute* NameIDAttributeDecoder::decode(
\r
66 const char* id, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
\r
69 auto_ptr<NameIDAttribute> nameid(
\r
70 new NameIDAttribute(id, (m_formatter.get() && *m_formatter.get()) ? m_formatter.get() : DEFAULT_NAMEID_FORMATTER)
\r
72 nameid->setCaseSensitive(m_caseSensitive);
\r
73 vector<NameIDAttribute::Value>& dest = nameid->getValues();
\r
74 vector<XMLObject*>::const_iterator v,stop;
\r
76 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder");
\r
78 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
\r
79 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
\r
81 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
\r
83 stop = values.end();
\r
84 if (log.isDebugEnabled()) {
\r
85 auto_ptr_char n(saml2attr->getName());
\r
86 log.debug("decoding NameIDAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)", id, n.get() ? n.get() : "unnamed", values.size());
\r
90 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
\r
92 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
\r
94 stop = values.end();
\r
95 if (log.isDebugEnabled()) {
\r
96 auto_ptr_char n(saml1attr->getAttributeName());
\r
97 log.debug("decoding NameIDAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)", id, n.get() ? n.get() : "unnamed", values.size());
\r
101 log.warn("XMLObject type not recognized by NameIDAttributeDecoder, no values returned");
\r
106 for (; v!=stop; ++v) {
\r
107 const NameIDType* n2 = dynamic_cast<const NameIDType*>(*v);
\r
111 const NameIdentifier* n1=dynamic_cast<const NameIdentifier*>(*v);
\r
114 else if ((*v)->hasChildren()) {
\r
115 const list<XMLObject*>& values = (*v)->getOrderedChildren();
\r
116 for (list<XMLObject*>::const_iterator vv = values.begin(); vv!=values.end(); ++vv) {
\r
117 if (n2=dynamic_cast<const NameIDType*>(*vv))
\r
119 else if (n1=dynamic_cast<const NameIdentifier*>(*vv))
\r
122 log.warn("skipping AttributeValue without a recognizable NameID/NameIdentifier");
\r
128 return dest.empty() ? NULL : nameid.release();
\r
131 const NameIDType* saml2name = dynamic_cast<const NameIDType*>(xmlObject);
\r
133 if (log.isDebugEnabled()) {
\r
134 auto_ptr_char f(saml2name->getFormat());
\r
135 log.debug("decoding NameIDAttribute (%s) from SAML 2 NameID with Format (%s)", id, f.get() ? f.get() : "unspecified");
\r
137 extract(saml2name, dest);
\r
140 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
\r
142 if (log.isDebugEnabled()) {
\r
143 auto_ptr_char f(saml1name->getFormat());
\r
144 log.debug("decoding NameIDAttribute (%s) from SAML 1 NameIdentifier with Format (%s)", id, f.get() ? f.get() : "unspecified");
\r
146 extract(saml1name, dest);
\r
149 log.warn("XMLObject type not recognized by NameIDAttributeDecoder, no values returned");
\r
154 return dest.empty() ? NULL : nameid.release();
\r
157 void NameIDAttributeDecoder::extract(
\r
158 const NameIDType* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
\r
161 char* name = toUTF8(n->getName());
\r
162 if (name && *name) {
\r
163 dest.push_back(NameIDAttribute::Value());
\r
164 NameIDAttribute::Value& val = dest.back();
\r
166 char* str = toUTF8(n->getFormat());
\r
168 val.m_Format = str;
\r
172 str = toUTF8(n->getNameQualifier());
\r
174 val.m_NameQualifier = str;
\r
175 else if (assertingParty)
\r
176 val.m_NameQualifier = assertingParty;
\r
179 str = toUTF8(n->getSPNameQualifier());
\r
181 val.m_SPNameQualifier = str;
\r
182 else if (relyingParty)
\r
183 val.m_SPNameQualifier = relyingParty;
\r
186 str = toUTF8(n->getSPProvidedID());
\r
188 val.m_SPProvidedID = str;
\r
195 void NameIDAttributeDecoder::extract(
\r
196 const NameIdentifier* n, vector<NameIDAttribute::Value>& dest, const char* assertingParty, const char* relyingParty
\r
199 char* name = toUTF8(n->getName());
\r
200 if (name && *name) {
\r
201 dest.push_back(NameIDAttribute::Value());
\r
202 NameIDAttribute::Value& val = dest.back();
\r
204 char* str = toUTF8(n->getFormat());
\r
206 val.m_Format = str;
\r
210 str = toUTF8(n->getNameQualifier());
\r
212 val.m_NameQualifier = str;
\r
213 else if (assertingParty)
\r
214 val.m_NameQualifier = assertingParty;
\r
218 val.m_SPNameQualifier = relyingParty;
\r