File: CaseFoldingAttributeResolver.cpp

package info (click to toggle)
shibboleth-sp2 2.6.0%2Bdfsg1-4%2Bdeb9u1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 7,896 kB
  • sloc: cpp: 39,404; sh: 11,726; makefile: 866; xml: 371; ansic: 35
file content (221 lines) | stat: -rw-r--r-- 8,096 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
 * Licensed to the University Corporation for Advanced Internet
 * Development, Inc. (UCAID) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the
 * License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */

/**
 * CaseFoldingAttributeResolver.cpp
 * 
 * Attribute Resolver plugins for upcasing and downcasing.
 */

#include "internal.h"

#include <algorithm>
#include <shibsp/exceptions.h>
#include <shibsp/SessionCache.h>
#include <shibsp/attribute/SimpleAttribute.h>
#include <shibsp/attribute/resolver/AttributeResolver.h>
#include <shibsp/attribute/resolver/ResolutionContext.h>
#include <xmltooling/XMLToolingConfig.h>
#include <xmltooling/util/XMLHelper.h>
#include <xercesc/util/XMLUniDefs.hpp>

using namespace shibsp;
using namespace xmltooling;
using namespace xercesc;
using namespace std;

namespace shibsp {

    class SHIBSP_DLLLOCAL FoldingContext : public ResolutionContext
    {
    public:
        FoldingContext(const vector<Attribute*>* attributes) : m_inputAttributes(attributes) {
        }

        ~FoldingContext() {
            for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<Attribute>());
        }

        const vector<Attribute*>* getInputAttributes() const {
            return m_inputAttributes;
        }
        vector<Attribute*>& getResolvedAttributes() {
            return m_attributes;
        }
        vector<opensaml::Assertion*>& getResolvedAssertions() {
            return m_assertions;
        }

    private:
        const vector<Attribute*>* m_inputAttributes;
        vector<Attribute*> m_attributes;
        static vector<opensaml::Assertion*> m_assertions;   // empty dummy
    };


    class SHIBSP_DLLLOCAL CaseFoldingAttributeResolver : public AttributeResolver
    {
    public:
        enum case_t {
            _up,
            _down
        };

        CaseFoldingAttributeResolver(const DOMElement* e, case_t direction);
        virtual ~CaseFoldingAttributeResolver() {}

        Lockable* lock() {
            return this;
        }
        void unlock() {
        }

        ResolutionContext* createResolutionContext(
            const Application& application,
            const opensaml::saml2md::EntityDescriptor* issuer,
            const XMLCh* protocol,
            const opensaml::saml2::NameID* nameid=nullptr,
            const XMLCh* authncontext_class=nullptr,
            const XMLCh* authncontext_decl=nullptr,
            const vector<const opensaml::Assertion*>* tokens=nullptr,
            const vector<Attribute*>* attributes=nullptr
            ) const {
            // Make sure new method gets run.
            return createResolutionContext(application, nullptr, issuer, protocol, nameid, authncontext_class, authncontext_decl, tokens, attributes);
        }

        ResolutionContext* createResolutionContext(
            const Application& application,
            const GenericRequest* request,
            const opensaml::saml2md::EntityDescriptor* issuer,
            const XMLCh* protocol,
            const opensaml::saml2::NameID* nameid=nullptr,
            const XMLCh* authncontext_class=nullptr,
            const XMLCh* authncontext_decl=nullptr,
            const vector<const opensaml::Assertion*>* tokens=nullptr,
            const vector<Attribute*>* attributes=nullptr
            ) const {
            return new FoldingContext(attributes);
        }

        ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
            return new FoldingContext(&session.getAttributes());
        }

        void resolveAttributes(ResolutionContext& ctx) const;

        void getAttributeIds(vector<string>& attributes) const {
            if (!m_dest.empty() && !m_dest.front().empty())
                attributes.push_back(m_dest.front());
        }

    private:
        Category& m_log;
        case_t m_direction;
        string m_source;
        vector<string> m_dest;
    };

    static const XMLCh dest[] =             UNICODE_LITERAL_4(d,e,s,t);
    static const XMLCh source[] =           UNICODE_LITERAL_6(s,o,u,r,c,e);

    AttributeResolver* SHIBSP_DLLLOCAL UpperCaseAttributeResolverFactory(const DOMElement* const & e)
    {
        return new CaseFoldingAttributeResolver(e, CaseFoldingAttributeResolver::_up);
    }

    AttributeResolver* SHIBSP_DLLLOCAL LowerCaseAttributeResolverFactory(const DOMElement* const & e)
    {
        return new CaseFoldingAttributeResolver(e, CaseFoldingAttributeResolver::_down);
    }
};

vector<opensaml::Assertion*> FoldingContext::m_assertions;

CaseFoldingAttributeResolver::CaseFoldingAttributeResolver(const DOMElement* e, case_t direction)
    : m_log(Category::getInstance(SHIBSP_LOGCAT ".AttributeResolver.CaseFolding")),
        m_direction(direction),
        m_source(XMLHelper::getAttrString(e, nullptr, source)),
        m_dest(1, XMLHelper::getAttrString(e, nullptr, dest))
{
    if (m_source.empty())
        throw ConfigurationException("CaseFolding AttributeResolver requires source attribute.");
}


void CaseFoldingAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
{
    FoldingContext& fctx = dynamic_cast<FoldingContext&>(ctx);
    if (!fctx.getInputAttributes())
        return;

    auto_ptr<SimpleAttribute> destwrapper;

    for (vector<Attribute*>::const_iterator a = fctx.getInputAttributes()->begin(); a != fctx.getInputAttributes()->end(); ++a) {
        if (m_source != (*a)->getId() || (*a)->valueCount() == 0) {
            continue;
        }

        SimpleAttribute* dest = nullptr;
        if (m_dest.empty() || m_dest.front().empty()) {
            // Can we transform in-place?
            dest = dynamic_cast<SimpleAttribute*>(*a);
            if (!dest) {
                m_log.warn("can't %scase non-simple attribute (%s) 'in place'", (m_direction==_up ? "up" : "down"), m_source.c_str());
                continue;
            }
            m_log.debug("applying in-place transform to source attribute (%s)", m_source.c_str());
        }
        else if (!destwrapper.get()) {
            // Create a destination attribute.
            destwrapper.reset(new SimpleAttribute(m_dest));
            m_log.debug("applying transform from source attribute (%s) to dest attribute (%s)", m_source.c_str(), m_dest.front().c_str());
        }

        for (size_t i = 0; i < (*a)->valueCount(); ++i) {
            try {
                XMLCh* srcval = fromUTF8((*a)->getSerializedValues()[i].c_str());
                if (srcval) {
                    auto_arrayptr<XMLCh> valjanitor(srcval);
                    (m_direction == _up) ? XMLString::upperCase(srcval) : XMLString::lowerCase(srcval);
                    auto_arrayptr<char> narrow(toUTF8(srcval));
                    if (dest) {
                        // Modify in place.
                        dest->getValues()[i] = narrow.get();
                    }
                    else {
                        // Add to new object.
                        destwrapper->getValues().push_back(narrow.get());
                    }
                }
            }
            catch (XMLException& ex) {
                auto_ptr_char msg(ex.getMessage());
                m_log.error("caught error performing conversion: %s", msg.get());
            }
        }
    }

    // Save off new object.
    if (destwrapper.get()) {
        ctx.getResolvedAttributes().push_back(destwrapper.get());
        destwrapper.release();
    }
}