Fix constructor.
[shibboleth/resolver.git] / shibresolver / resolver.cpp
1 /*
2  *  Copyright 2010 JANET(UK)
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * resolver.cpp
19  *
20  * An embeddable component interface to Shibboleth SP attribute processing.
21  */
22
23 #include "internal.h"
24
25 #include <shibsp/ServiceProvider.h>
26 #include <shibsp/attribute/Attribute.h>
27 #include <shibsp/remoting/ListenerService.h>
28
29 using namespace shibresolver;
30 using namespace shibsp;
31 #ifndef SHIBSP_LITE
32 using namespace opensaml;
33 #endif
34 using namespace xmltooling;
35 using namespace std;
36
37 namespace shibresolver {
38     class SHIBRESOLVER_DLLLOCAL RemotedResolver : public Remoted {
39     public:
40         RemotedResolver() {}
41         ~RemotedResolver() {}
42
43         void receive(DDF& in, ostream& out);
44     };
45
46     static RemotedResolver g_Remoted;
47 };
48
49 ShibbolethResolver* ShibbolethResolver::create()
50 {
51     return new ShibbolethResolver();
52 }
53
54 ShibbolethResolver::ShibbolethResolver()
55 {
56     m_mapper = NULL;
57     m_sp = NULL;
58 }
59
60 ShibbolethResolver::~ShibbolethResolver()
61 {
62     for_each(m_resolvedAttributes.begin(), m_resolvedAttributes.end(), xmltooling::cleanup<Attribute>());
63     if (m_mapper)
64         m_mapper->unlock();
65     if (m_sp)
66         m_sp->unlock();
67 }
68
69 void ShibbolethResolver::setServiceURI(const char* uri)
70 {
71     m_serviceURI.erase();
72     if (uri)
73         m_serviceURI = uri;
74 }
75
76 void ShibbolethResolver::setApplicationID(const char* appID)
77 {
78     m_appID.erase();
79     if (appID)
80         m_appID = appID;
81 }
82
83 void ShibbolethResolver::setIssuer(const char* issuer)
84 {
85     m_issuer.erase();
86     if (issuer)
87         m_issuer = issuer;
88 }
89
90 void ShibbolethResolver::addToken(
91 #ifdef SHIBSP_LITE
92         const XMLObject* token
93 #else
94         const saml2::Assertion* token
95 #endif
96     )
97 {
98     if (token)
99         m_tokens.push_back(token);
100 }
101
102 void ShibbolethResolver::addAttribute(Attribute* attr)
103 {
104     if (attr)
105         m_inputAttributes.push_back(attr);
106 }
107
108 vector<Attribute*>& ShibbolethResolver::getResolvedAttributes()
109 {
110     return m_resolvedAttributes;
111 }
112
113 RequestMapper::Settings ShibbolethResolver::getSettings() const
114 {
115     return m_settings;
116 }
117
118 void ShibbolethResolver::resolve()
119 {
120 }
121
122 void RemotedResolver::receive(DDF& in, ostream& out)
123 {
124 }
125
126 bool ShibbolethResolver::init(unsigned long features, const char* config, bool rethrow)
127 {
128     SPConfig::getConfig().setFeatures(features | SPConfig::AttributeResolution | SPConfig::Metadata | SPConfig::Trust);
129     if (!SPConfig::getConfig().init())
130         return false;
131     if (!SPConfig::getConfig().instantiate(config, rethrow))
132         return false;
133     return true;
134 }
135
136 /**
137     * Shuts down runtime.
138     *
139     * Each process using the library SHOULD call this function exactly once before terminating itself.
140     */
141 void ShibbolethResolver::term()
142 {
143     SPConfig::getConfig().term();
144 }
145
146
147 extern "C" int SHIBRESOLVER_EXPORTS xmltooling_extension_init(void*)
148 {
149 #ifdef SHIBRESOLVER_SHIBSP_HAS_REMOTING
150     SPConfig& conf = SPConfig::getConfig();
151     if (conf.isEnabled(SPConfig::OutOfProcess) && !conf.isEnabled(SPConfig::InProcess) && conf.isEnabled(SPConfig::Listener))
152         conf.getServiceProvider()->regListener("org.project-moonshot.shibresolver", &g_Remoted);
153 #endif
154     return 0;   // signal success
155 }
156
157 extern "C" void SHIBRESOLVER_EXPORTS xmltooling_extension_term()
158 {
159     // Factories normally get unregistered during library shutdown, so no work usually required here.
160 }