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>
33 using namespace shibboleth;
35 using namespace log4cpp;
40 class XMLCredentialsImpl : public ReloadableXMLFileImpl
43 XMLCredentialsImpl(const char* pathname) : ReloadableXMLFileImpl(pathname) { init(); }
44 XMLCredentialsImpl(const DOMElement* e) : ReloadableXMLFileImpl(e) { init(); }
46 ~XMLCredentialsImpl();
48 typedef map<string,ICredResolver*> resolvermap_t;
49 resolvermap_t m_resolverMap;
52 class XMLCredentials : public ICredentials, public ReloadableXMLFile
55 XMLCredentials(const DOMElement* e) : ReloadableXMLFile(e) {}
58 const ICredResolver* lookup(const char* id) const;
61 virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
62 virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
67 IPlugIn* XMLCredentialsFactory(const DOMElement* e)
69 auto_ptr<XMLCredentials> creds(new XMLCredentials(e));
70 creds->getImplementation();
71 return creds.release();
74 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const char* pathname, bool first) const
76 return new XMLCredentialsImpl(pathname);
79 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const DOMElement* e, bool first) const
81 return new XMLCredentialsImpl(e);
84 void XMLCredentialsImpl::init()
87 saml::NDC ndc("init");
89 Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Credentials");
92 if (!saml::XML::isElementNamed(m_root,::XML::CREDS_NS,SHIB_L(Credentials))) {
93 log.error("Construction requires a valid creds file: (creds:Credentials as root element)");
94 throw CredentialException("Construction requires a valid creds file: (creds:Credentials as root element)");
97 DOMElement* child=saml::XML::getFirstChildElement(m_root);
100 auto_ptr<char> id(XMLString::transcode(child->getAttributeNS(NULL,SHIB_L(Id))));
102 if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(FileResolver)))
103 cr_type="edu.internet2.middleware.shibboleth.common.Credentials.FileCredentialResolver";
104 else if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(CustomResolver))) {
105 auto_ptr_char c(child->getAttributeNS(NULL,SHIB_L(Class)));
109 if (!cr_type.empty()) {
111 IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(cr_type.c_str(),child);
112 ICredResolver* cr=dynamic_cast<ICredResolver*>(plugin);
114 m_resolverMap[id.get()]=cr;
116 log.error("plugin was not a credential resolver");
117 throw UnsupportedExtensionException("plugin was not a credential resolver");
120 catch (SAMLException& e) {
121 log.error("failed to instantiate credential resolver (%s): %s", id.get(), e.what());
126 log.error("unknown or unimplemented type of credential resolver (%s)", id.get());
127 throw CredentialException("Unknown or unimplemented type of credential resolver");
130 child=saml::XML::getNextSiblingElement(child);
133 catch (SAMLException& e) {
134 log.errorStream() << "Error while parsing creds configuration: " << e.what() << CategoryStream::ENDLINE;
135 this->~XMLCredentialsImpl();
140 log.error("Unexpected error while parsing creds configuration");
141 this->~XMLCredentialsImpl();
147 XMLCredentialsImpl::~XMLCredentialsImpl()
149 for_each(m_resolverMap.begin(),m_resolverMap.end(),xmltooling::cleanup_pair<string,ICredResolver>());
152 const ICredResolver* XMLCredentials::lookup(const char* id) const
155 XMLCredentialsImpl* impl=dynamic_cast<XMLCredentialsImpl*>(getImplementation());
156 XMLCredentialsImpl::resolvermap_t::const_iterator i=impl->m_resolverMap.find(id);
157 if (i!=impl->m_resolverMap.end())