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 * NameIDFromNameIDFromScopedAttributeDecoder.cpp
24 * Decodes SAML "scoped" attributes 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 defaultQualifiers[] =UNICODE_LITERAL_17(d,e,f,a,u,l,t,Q,u,a,l,i,f,i,e,r,s);
42 static const XMLCh format[] = UNICODE_LITERAL_6(f,o,r,m,a,t);
43 static const XMLCh formatter[] = UNICODE_LITERAL_9(f,o,r,m,a,t,t,e,r);
44 static const XMLCh Scope[] = UNICODE_LITERAL_5(S,c,o,p,e);
45 static const XMLCh scopeDelimeter[] = UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,e,t,e,r);
47 class SHIBSP_DLLLOCAL NameIDFromScopedAttributeDecoder : virtual public AttributeDecoder
50 NameIDFromScopedAttributeDecoder(const DOMElement* e)
51 : AttributeDecoder(e),
53 m_format(XMLHelper::getAttrString(e, nullptr, format)),
54 m_formatter(XMLHelper::getAttrString(e, nullptr, formatter)),
55 m_defaultQualifiers(XMLHelper::getAttrBool(e, false, defaultQualifiers)) {
56 if (e && e->hasAttributeNS(nullptr,scopeDelimeter)) {
57 auto_ptr_char d(e->getAttributeNS(nullptr,scopeDelimeter));
58 m_delimeter = *(d.get());
61 ~NameIDFromScopedAttributeDecoder() {}
63 shibsp::Attribute* decode(
64 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
69 string m_format,m_formatter;
70 bool m_defaultQualifiers;
73 AttributeDecoder* SHIBSP_DLLLOCAL NameIDFromScopedAttributeDecoderFactory(const DOMElement* const & e)
75 return new NameIDFromScopedAttributeDecoder(e);
79 shibsp::Attribute* NameIDFromScopedAttributeDecoder::decode(
80 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
86 const XMLCh* xmlscope;
87 xmltooling::QName scopeqname(nullptr,Scope);
88 auto_ptr<NameIDAttribute> nameid(
89 new NameIDAttribute(ids, (!m_formatter.empty()) ? m_formatter.c_str() : DEFAULT_NAMEID_FORMATTER)
91 vector<NameIDAttribute::Value>& dest = nameid->getValues();
92 vector<XMLObject*>::const_iterator v,stop;
94 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.NameIDFromScoped");
96 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
97 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
99 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
102 if (log.isDebugEnabled()) {
103 auto_ptr_char n(saml2attr->getName());
105 "decoding NameIDAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
106 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
111 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
113 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
116 if (log.isDebugEnabled()) {
117 auto_ptr_char n(saml1attr->getAttributeName());
119 "decoding NameIDAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
120 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
125 log.warn("XMLObject type not recognized by NameIDFromScopedAttributeDecoder, no values returned");
130 for (; v!=stop; ++v) {
131 if (!(*v)->hasChildren()) {
132 val = toUTF8((*v)->getTextContent());
134 dest.push_back(NameIDAttribute::Value());
135 NameIDAttribute::Value& destval = dest.back();
136 const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*v);
137 xmlscope = aexo ? aexo->getAttribute(scopeqname) : nullptr;
138 if (!xmlscope || !*xmlscope) {
139 // Terminate the value at the scope delimiter.
140 if (scope = strchr(val, m_delimeter))
143 destval.m_Name = val;
144 destval.m_Format = m_format;
145 if (m_defaultQualifiers && assertingParty)
146 destval.m_NameQualifier = assertingParty;
147 if (m_defaultQualifiers && relyingParty)
148 destval.m_SPNameQualifier = relyingParty;
151 log.warn("skipping empty AttributeValue");
156 log.warn("skipping complex AttributeValue");
160 return dest.empty() ? nullptr : _decode(nameid.release());
163 log.warn("XMLObject type not recognized by NameIDFromScopedAttributeDecoder, no values returned");