2 * Copyright 2001-2005 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.
17 /* XMLCredentials.cpp - a credentials implementation that uses an XML file
28 #include <sys/types.h>
31 #include <log4cpp/Category.hh>
32 #include <shibsp/exceptions.h>
34 using namespace shibsp;
35 using namespace shibboleth;
37 using namespace xmltooling;
38 using namespace log4cpp;
43 class XMLCredentialsImpl : public ReloadableXMLFileImpl
46 XMLCredentialsImpl(const char* pathname) : ReloadableXMLFileImpl(pathname) { init(); }
47 XMLCredentialsImpl(const DOMElement* e) : ReloadableXMLFileImpl(e) { init(); }
49 ~XMLCredentialsImpl();
51 typedef map<string,ICredResolver*> resolvermap_t;
52 resolvermap_t m_resolverMap;
55 class XMLCredentials : public ICredentials, public ReloadableXMLFile
58 XMLCredentials(const DOMElement* e) : ReloadableXMLFile(e) {}
61 const ICredResolver* lookup(const char* id) const;
64 virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
65 virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
70 IPlugIn* XMLCredentialsFactory(const DOMElement* e)
72 auto_ptr<XMLCredentials> creds(new XMLCredentials(e));
73 creds->getImplementation();
74 return creds.release();
77 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const char* pathname, bool first) const
79 return new XMLCredentialsImpl(pathname);
82 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const DOMElement* e, bool first) const
84 return new XMLCredentialsImpl(e);
87 void XMLCredentialsImpl::init()
90 saml::NDC ndc("init");
92 Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Credentials");
95 if (!saml::XML::isElementNamed(m_root,::XML::CREDS_NS,SHIB_L(Credentials))) {
96 log.error("Construction requires a valid creds file: (creds:Credentials as root element)");
97 throw ConfigurationException("Construction requires a valid creds file: (creds:Credentials as root element)");
100 DOMElement* child=saml::XML::getFirstChildElement(m_root);
103 auto_ptr<char> id(XMLString::transcode(child->getAttributeNS(NULL,SHIB_L(Id))));
105 if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(FileResolver)))
106 cr_type="edu.internet2.middleware.shibboleth.common.Credentials.FileCredentialResolver";
107 else if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(CustomResolver))) {
108 xmltooling::auto_ptr_char c(child->getAttributeNS(NULL,SHIB_L(Class)));
112 if (!cr_type.empty()) {
114 IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(cr_type.c_str(),child);
115 ICredResolver* cr=dynamic_cast<ICredResolver*>(plugin);
117 m_resolverMap[id.get()]=cr;
119 log.error("plugin was not a credential resolver");
120 throw UnknownExtensionException("plugin was not a credential resolver");
123 catch (exception& e) {
124 log.error("failed to instantiate credential resolver (%s): %s", id.get(), e.what());
129 log.error("unknown or unimplemented type of credential resolver (%s)", id.get());
130 throw UnknownExtensionException("Unknown or unimplemented type of credential resolver");
133 child=saml::XML::getNextSiblingElement(child);
136 catch (exception& e) {
137 log.errorStream() << "Error while parsing creds configuration: " << e.what() << CategoryStream::ENDLINE;
138 this->~XMLCredentialsImpl();
143 log.error("Unexpected error while parsing creds configuration");
144 this->~XMLCredentialsImpl();
150 XMLCredentialsImpl::~XMLCredentialsImpl()
152 for_each(m_resolverMap.begin(),m_resolverMap.end(),xmltooling::cleanup_pair<string,ICredResolver>());
155 const ICredResolver* XMLCredentials::lookup(const char* id) const
158 XMLCredentialsImpl* impl=dynamic_cast<XMLCredentialsImpl*>(getImplementation());
159 XMLCredentialsImpl::resolvermap_t::const_iterator i=impl->m_resolverMap.find(id);
160 if (i!=impl->m_resolverMap.end())