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() {}
55 shibsp::Attribute* decode(
56 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
63 AttributeDecoder* SHIBSP_DLLLOCAL ScopedAttributeDecoderFactory(const DOMElement* const & e)
65 return new ScopedAttributeDecoder(e);
69 shibsp::Attribute* ScopedAttributeDecoder::decode(
70 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
75 const XMLCh* xmlscope;
76 xmltooling::QName scopeqname(nullptr,Scope);
77 auto_ptr<ScopedAttribute> scoped(new ScopedAttribute(ids, m_delimiter));
78 vector< pair<string,string> >& dest = scoped->getValues();
79 vector<XMLObject*>::const_iterator v,stop;
81 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.Scoped");
83 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
84 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
86 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
89 if (log.isDebugEnabled()) {
90 auto_ptr_char n(saml2attr->getName());
92 "decoding ScopedAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
93 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
98 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
100 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
103 if (log.isDebugEnabled()) {
104 auto_ptr_char n(saml1attr->getAttributeName());
106 "decoding ScopedAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
107 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
112 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
117 for (; v!=stop; ++v) {
118 if (!(*v)->hasChildren()) {
119 val = toUTF8((*v)->getTextContent());
121 const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*v);
122 xmlscope = aexo ? aexo->getAttribute(scopeqname) : nullptr;
123 if (xmlscope && *xmlscope) {
124 scope = toUTF8(xmlscope);
125 dest.push_back(pair<string,string>(val,scope));
129 scope = strchr(val, m_delimiter);
133 dest.push_back(pair<string,string>(val,scope));
135 log.warn("ignoring unscoped AttributeValue");
138 log.warn("ignoring unscoped AttributeValue");
143 log.warn("skipping empty AttributeValue");
148 log.warn("skipping complex AttributeValue");
152 return dest.empty() ? nullptr : _decode(scoped.release());
155 const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
157 if (log.isDebugEnabled()) {
158 auto_ptr_char f(saml2name->getFormat());
159 log.debug("decoding ScopedAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
161 val = toUTF8(saml2name->getName());
164 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
166 if (log.isDebugEnabled()) {
167 auto_ptr_char f(saml1name->getFormat());
169 "decoding ScopedAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
170 ids.front().c_str(), f.get() ? f.get() : "unspecified"
173 val = toUTF8(saml1name->getName());
176 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
181 if (val && *val && *val!=m_delimiter) {
182 scope = strchr(val, m_delimiter);
186 dest.push_back(pair<string,string>(val,scope));
188 log.warn("ignoring NameID with no scope");
191 log.warn("ignoring NameID with no scope delimiter (%c)", m_delimiter);
195 log.warn("ignoring empty NameID");
198 return dest.empty() ? nullptr : _decode(scoped.release());