2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * ScopedAttributeDecoder.cpp
\r
20 * Decodes SAML into ScopedAttributes
\r
23 #include "internal.h"
\r
24 #include "attribute/AttributeDecoder.h"
\r
25 #include "attribute/ScopedAttribute.h"
\r
27 #include <saml/saml1/core/Assertions.h>
\r
28 #include <saml/saml2/core/Assertions.h>
\r
30 using namespace shibsp;
\r
31 using namespace opensaml::saml1;
\r
32 using namespace opensaml::saml2;
\r
33 using namespace xmltooling;
\r
34 using namespace std;
\r
37 static const XMLCh Scope[] = UNICODE_LITERAL_5(S,c,o,p,e);
\r
38 static const XMLCh scopeDelimeter[] = UNICODE_LITERAL_14(s,c,o,p,e,D,e,l,i,m,e,t,e,r);
\r
40 class SHIBSP_DLLLOCAL ScopedAttributeDecoder : virtual public AttributeDecoder
\r
43 ScopedAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_delimeter('@') {
\r
45 if (e->hasAttributeNS(NULL,scopeDelimeter)) {
\r
46 auto_ptr_char d(e->getAttributeNS(NULL,scopeDelimeter));
\r
47 m_delimeter = *(d.get());
\r
51 ~ScopedAttributeDecoder() {}
\r
53 shibsp::Attribute* decode(
\r
54 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL
\r
61 AttributeDecoder* SHIBSP_DLLLOCAL ScopedAttributeDecoderFactory(const DOMElement* const & e)
\r
63 return new ScopedAttributeDecoder(e);
\r
67 shibsp::Attribute* ScopedAttributeDecoder::decode(
\r
68 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
\r
73 const XMLCh* xmlscope;
\r
74 QName scopeqname(NULL,Scope);
\r
75 auto_ptr<ScopedAttribute> scoped(new ScopedAttribute(ids, m_delimeter));
\r
76 scoped->setCaseSensitive(m_caseSensitive);
\r
77 vector< pair<string,string> >& dest = scoped->getValues();
\r
78 vector<XMLObject*>::const_iterator v,stop;
\r
80 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder");
\r
82 if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
\r
83 const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
\r
85 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
\r
87 stop = values.end();
\r
88 if (log.isDebugEnabled()) {
\r
89 auto_ptr_char n(saml2attr->getName());
\r
91 "decoding ScopedAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
\r
92 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
\r
97 const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
\r
99 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
\r
100 v = values.begin();
\r
101 stop = values.end();
\r
102 if (log.isDebugEnabled()) {
\r
103 auto_ptr_char n(saml1attr->getAttributeName());
\r
105 "decoding ScopedAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
\r
106 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
\r
111 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
\r
116 for (; v!=stop; ++v) {
\r
117 if (!(*v)->hasChildren()) {
\r
118 val = toUTF8((*v)->getTextContent());
\r
120 const AttributeExtensibleXMLObject* aexo=dynamic_cast<const AttributeExtensibleXMLObject*>(*v);
\r
121 xmlscope = aexo->getAttribute(scopeqname);
\r
122 if (xmlscope && *xmlscope) {
\r
123 scope = toUTF8(xmlscope);
\r
124 dest.push_back(make_pair(val,scope));
\r
128 scope = strchr(val, m_delimeter);
\r
132 dest.push_back(make_pair(val,scope));
\r
134 log.warn("ignoring unscoped AttributeValue");
\r
137 log.warn("ignoring unscoped AttributeValue");
\r
142 log.warn("skipping empty AttributeValue");
\r
147 log.warn("skipping complex AttributeValue");
\r
151 return dest.empty() ? NULL : scoped.release();
\r
154 const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
\r
156 if (log.isDebugEnabled()) {
\r
157 auto_ptr_char f(saml2name->getFormat());
\r
158 log.debug("decoding ScopedAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
\r
160 val = toUTF8(saml2name->getName());
\r
163 const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
\r
165 if (log.isDebugEnabled()) {
\r
166 auto_ptr_char f(saml1name->getFormat());
\r
168 "decoding ScopedAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
\r
169 ids.front().c_str(), f.get() ? f.get() : "unspecified"
\r
172 val = toUTF8(saml1name->getName());
\r
175 log.warn("XMLObject type not recognized by ScopedAttributeDecoder, no values returned");
\r
180 if (val && *val && *val!=m_delimeter) {
\r
181 scope = strchr(val, m_delimeter);
\r
185 dest.push_back(make_pair(val,scope));
\r
187 log.warn("ignoring NameID with no scope");
\r
190 log.warn("ignoring NameID with no scope delimiter (%c)", m_delimeter);
\r
194 log.warn("ignoring empty NameID");
\r
197 return dest.empty() ? NULL : scoped.release();
\r