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 * ScopedAttributeDecoder.cpp
24 * Decodes SAML into ScopedAttributes.
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/ScopedAttribute.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 Scope[] = UNICODE_LITERAL_5(S,c,o,p,e);
42 static const XMLCh scopeDelimiter[] = UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,i,t,e,r);
44 class SHIBSP_DLLLOCAL ScopedAttributeDecoder : virtual public AttributeDecoder
47 ScopedAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_delimiter('@') {
48 if (e && e->hasAttributeNS(nullptr,scopeDelimiter)) {
49 auto_ptr_char d(e->getAttributeNS(nullptr,scopeDelimiter));
50 m_delimiter = *(d.get());
53 ~ScopedAttributeDecoder() {}
56 shibsp::Attribute* decode(
57 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
59 return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
62 shibsp::Attribute* decode(
63 const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
70 AttributeDecoder* SHIBSP_DLLLOCAL ScopedAttributeDecoderFactory(const DOMElement* const & e)
72 return new ScopedAttributeDecoder(e);
76 shibsp::Attribute* ScopedAttributeDecoder::decode(
77 const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
82 const XMLCh* xmlscope;
83 xmltooling::QName scopeqname(nullptr,Scope);
84 auto_ptr<ScopedAttribute> scoped(new ScopedAttribute(ids, m_delimiter));
85 vector< pair<string,string> >& dest = scoped->getValues();
86 pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
88 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.Scoped");
90 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
91 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
93 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
94 valrange = valueRange(request, values);
95 if (log.isDebugEnabled()) {
96 auto_ptr_char n(saml2attr->getName());
98 "decoding ScopedAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
99 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
104 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
106 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
107 valrange = valueRange(request, values);
108 if (log.isDebugEnabled()) {
109 auto_ptr_char n(saml1attr->getAttributeName());
111 "decoding ScopedAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
112 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
117 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
122 for (; valrange.first != valrange.second; ++valrange.first) {
123 if (!(*valrange.first)->hasChildren()) {
124 val = toUTF8((*valrange.first)->getTextContent());
126 const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*valrange.first);
127 xmlscope = aexo ? aexo->getAttribute(scopeqname) : nullptr;
128 if (xmlscope && *xmlscope) {
129 auto_arrayptr<char> noninlinescope(toUTF8(xmlscope));
130 dest.push_back(pair<string,string>(val,noninlinescope.get()));
133 scope = strchr(val, m_delimiter);
137 dest.push_back(pair<string,string>(val,scope));
139 log.warn("ignoring unscoped AttributeValue");
142 log.warn("ignoring unscoped AttributeValue");
147 log.warn("skipping empty AttributeValue");
152 log.warn("skipping complex AttributeValue");
156 return dest.empty() ? nullptr : _decode(scoped.release());
159 const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
161 if (log.isDebugEnabled()) {
162 auto_ptr_char f(saml2name->getFormat());
163 log.debug("decoding ScopedAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
165 val = toUTF8(saml2name->getName());
168 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
170 if (log.isDebugEnabled()) {
171 auto_ptr_char f(saml1name->getFormat());
173 "decoding ScopedAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
174 ids.front().c_str(), f.get() ? f.get() : "unspecified"
177 val = toUTF8(saml1name->getName());
180 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
185 if (val && *val && *val != m_delimiter) {
186 scope = strchr(val, m_delimiter);
190 dest.push_back(pair<string,string>(val,scope));
192 log.warn("ignoring NameID with no scope");
195 log.warn("ignoring NameID with no scope delimiter (%c)", m_delimiter);
199 log.warn("ignoring empty NameID");
202 return dest.empty() ? nullptr : _decode(scoped.release());