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() {}
64 shibsp::Attribute* decode(
65 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
67 return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
70 shibsp::Attribute* decode(
71 const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
76 string m_format,m_formatter;
77 bool m_defaultQualifiers;
80 AttributeDecoder* SHIBSP_DLLLOCAL NameIDFromScopedAttributeDecoderFactory(const DOMElement* const & e)
82 return new NameIDFromScopedAttributeDecoder(e);
86 shibsp::Attribute* NameIDFromScopedAttributeDecoder::decode(
87 const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
93 const XMLCh* xmlscope;
94 xmltooling::QName scopeqname(nullptr,Scope);
95 auto_ptr<NameIDAttribute> nameid(
96 new NameIDAttribute(ids, (!m_formatter.empty()) ? m_formatter.c_str() : DEFAULT_NAMEID_FORMATTER, m_hashAlg.c_str())
98 vector<NameIDAttribute::Value>& dest = nameid->getValues();
99 pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
101 Category& log = Category::getInstance(SHIBSP_LOGCAT ".AttributeDecoder.NameIDFromScoped");
103 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
104 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
106 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
107 valrange = valueRange(request, values);
108 if (log.isDebugEnabled()) {
109 auto_ptr_char n(saml2attr->getName());
111 "decoding NameIDAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
112 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
117 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
119 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
120 valrange = valueRange(request, values);
121 if (log.isDebugEnabled()) {
122 auto_ptr_char n(saml1attr->getAttributeName());
124 "decoding NameIDAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
125 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
130 log.warn("XMLObject type not recognized by NameIDFromScopedAttributeDecoder, no values returned");
135 for (; valrange.first != valrange.second; ++valrange.first) {
136 if (!(*valrange.first)->hasChildren()) {
137 val = toUTF8((*valrange.first)->getTextContent());
139 dest.push_back(NameIDAttribute::Value());
140 NameIDAttribute::Value& destval = dest.back();
141 const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*valrange.first);
142 xmlscope = aexo ? aexo->getAttribute(scopeqname) : nullptr;
143 if (!xmlscope || !*xmlscope) {
144 // Terminate the value at the scope delimiter.
145 if (scope = strchr(val, m_delimeter))
148 destval.m_Name = val;
149 destval.m_Format = m_format;
150 if (m_defaultQualifiers && assertingParty)
151 destval.m_NameQualifier = assertingParty;
152 if (m_defaultQualifiers && relyingParty)
153 destval.m_SPNameQualifier = relyingParty;
156 log.warn("skipping empty AttributeValue");
161 log.warn("skipping complex AttributeValue");
165 return dest.empty() ? nullptr : nameid.release();
168 log.warn("XMLObject type not recognized by NameIDFromScopedAttributeDecoder, no values returned");