2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * ScopedAttributeDecoder.cpp
20 * Decodes SAML into ScopedAttributes.
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/ScopedAttribute.h"
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
30 using namespace shibsp;
31 using namespace opensaml::saml1;
32 using namespace opensaml::saml2;
33 using namespace xmltooling;
37 static const XMLCh Scope[] = UNICODE_LITERAL_5(S,c,o,p,e);
38 static const XMLCh scopeDelimiter[] = UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,i,t,e,r);
40 class SHIBSP_DLLLOCAL ScopedAttributeDecoder : virtual public AttributeDecoder
43 ScopedAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_delimiter('@') {
44 if (e && e->hasAttributeNS(NULL,scopeDelimiter)) {
45 auto_ptr_char d(e->getAttributeNS(NULL,scopeDelimiter));
46 m_delimiter = *(d.get());
49 ~ScopedAttributeDecoder() {}
51 shibsp::Attribute* decode(
52 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL
59 AttributeDecoder* SHIBSP_DLLLOCAL ScopedAttributeDecoderFactory(const DOMElement* const & e)
61 return new ScopedAttributeDecoder(e);
65 shibsp::Attribute* ScopedAttributeDecoder::decode(
66 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
71 const XMLCh* xmlscope;
72 xmltooling::QName scopeqname(NULL,Scope);
73 auto_ptr<ScopedAttribute> scoped(new ScopedAttribute(ids, m_delimiter));
74 vector< pair<string,string> >& dest = scoped->getValues();
75 vector<XMLObject*>::const_iterator v,stop;
77 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.Scoped");
79 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
80 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
82 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
85 if (log.isDebugEnabled()) {
86 auto_ptr_char n(saml2attr->getName());
88 "decoding ScopedAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
89 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
94 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
96 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
99 if (log.isDebugEnabled()) {
100 auto_ptr_char n(saml1attr->getAttributeName());
102 "decoding ScopedAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
103 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
108 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
113 for (; v!=stop; ++v) {
114 if (!(*v)->hasChildren()) {
115 val = toUTF8((*v)->getTextContent());
117 const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*v);
118 xmlscope = aexo ? aexo->getAttribute(scopeqname) : NULL;
119 if (xmlscope && *xmlscope) {
120 scope = toUTF8(xmlscope);
121 dest.push_back(pair<string,string>(val,scope));
125 scope = strchr(val, m_delimiter);
129 dest.push_back(pair<string,string>(val,scope));
131 log.warn("ignoring unscoped AttributeValue");
134 log.warn("ignoring unscoped AttributeValue");
139 log.warn("skipping empty AttributeValue");
144 log.warn("skipping complex AttributeValue");
148 return dest.empty() ? NULL : _decode(scoped.release());
151 const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
153 if (log.isDebugEnabled()) {
154 auto_ptr_char f(saml2name->getFormat());
155 log.debug("decoding ScopedAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
157 val = toUTF8(saml2name->getName());
160 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
162 if (log.isDebugEnabled()) {
163 auto_ptr_char f(saml1name->getFormat());
165 "decoding ScopedAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
166 ids.front().c_str(), f.get() ? f.get() : "unspecified"
169 val = toUTF8(saml1name->getName());
172 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
177 if (val && *val && *val!=m_delimiter) {
178 scope = strchr(val, m_delimiter);
182 dest.push_back(pair<string,string>(val,scope));
184 log.warn("ignoring NameID with no scope");
187 log.warn("ignoring NameID with no scope delimiter (%c)", m_delimiter);
191 log.warn("ignoring empty NameID");
194 return dest.empty() ? NULL : _decode(scoped.release());