Boost code changes
[shibboleth/cpp-sp.git] / shibsp / attribute / StringAttributeDecoder.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * StringAttributeDecoder.cpp
23  *
24  * Decodes SAML into SimpleAttributes.
25  */
26
27 #include "internal.h"
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/SimpleAttribute.h"
30
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33
34 using namespace shibsp;
35 using namespace opensaml::saml1;
36 using namespace opensaml::saml2;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace shibsp {
41     class SHIBSP_DLLLOCAL StringAttributeDecoder : virtual public AttributeDecoder
42     {
43     public:
44         StringAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
45         ~StringAttributeDecoder() {}
46
47         shibsp::Attribute* decode(
48             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
49             ) const;
50     };
51
52     AttributeDecoder* SHIBSP_DLLLOCAL StringAttributeDecoderFactory(const DOMElement* const & e)
53     {
54         return new StringAttributeDecoder(e);
55     }
56 };
57
58 shibsp::Attribute* StringAttributeDecoder::decode(
59     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
60     ) const
61 {
62     auto_ptr<SimpleAttribute> simple(new SimpleAttribute(ids));
63     vector<string>& dest = simple->getValues();
64     vector<XMLObject*>::const_iterator v,stop;
65
66     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.String");
67
68     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
69         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
70         if (saml2attr) {
71             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
72             v = values.begin();
73             stop = values.end();
74             if (log.isDebugEnabled()) {
75                 auto_ptr_char n(saml2attr->getName());
76                 log.debug(
77                     "decoding SimpleAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
78                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
79                     );
80             }
81         }
82         else {
83             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
84             if (saml1attr) {
85                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
86                 v = values.begin();
87                 stop = values.end();
88                 if (log.isDebugEnabled()) {
89                     auto_ptr_char n(saml1attr->getAttributeName());
90                 log.debug(
91                     "decoding SimpleAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
92                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
93                     );
94                 }
95             }
96             else {
97                 log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
98                 return nullptr;
99             }
100         }
101
102         for (; v!=stop; ++v) {
103             if (!(*v)->hasChildren()) {
104                 auto_arrayptr<char> val(toUTF8((*v)->getTextContent()));
105                 if (val.get() && *val.get())
106                     dest.push_back(val.get());
107                 else
108                     log.warn("skipping empty AttributeValue");
109             }
110             else {
111                 log.warn("skipping complex AttributeValue");
112             }
113         }
114
115         return dest.empty() ? nullptr : _decode(simple.release());
116     }
117
118     const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
119     if (saml2name) {
120         if (log.isDebugEnabled()) {
121             auto_ptr_char f(saml2name->getFormat());
122             log.debug("decoding SimpleAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
123         }
124         auto_arrayptr<char> val(toUTF8(saml2name->getName()));
125         if (val.get() && *val.get())
126             dest.push_back(val.get());
127         else
128             log.warn("ignoring empty NameID");
129     }
130     else {
131         const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
132         if (saml1name) {
133             if (log.isDebugEnabled()) {
134                 auto_ptr_char f(saml1name->getFormat());
135                 log.debug(
136                     "decoding SimpleAttribute (%s) from SAML 1 NameIdentifier with Format (%s)",
137                     ids.front().c_str(), f.get() ? f.get() : "unspecified"
138                     );
139             }
140             auto_arrayptr<char> val(toUTF8(saml1name->getName()));
141             if (val.get() && *val.get())
142                 dest.push_back(val.get());
143             else
144                 log.warn("ignoring empty NameIdentifier");
145         }
146         else {
147             log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
148             return nullptr;
149         }
150     }
151
152     return dest.empty() ? nullptr : _decode(simple.release());
153 }