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
27 #include <sys/types.h>
30 using namespace xmlproviders::logging;
31 using namespace shibboleth;
37 class XMLCredentialsImpl : public ReloadableXMLFileImpl
40 XMLCredentialsImpl(const char* pathname) : ReloadableXMLFileImpl(pathname) { init(); }
41 XMLCredentialsImpl(const DOMElement* e) : ReloadableXMLFileImpl(e) { init(); }
43 ~XMLCredentialsImpl();
45 typedef map<string,ICredResolver*> resolvermap_t;
46 resolvermap_t m_resolverMap;
49 class XMLCredentials : public ICredentials, public ReloadableXMLFile
52 XMLCredentials(const DOMElement* e) : ReloadableXMLFile(e) {}
55 const ICredResolver* lookup(const char* id) const;
58 virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
59 virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
64 IPlugIn* XMLCredentialsFactory(const DOMElement* e)
66 auto_ptr<XMLCredentials> creds(new XMLCredentials(e));
67 creds->getImplementation();
68 return creds.release();
71 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const char* pathname, bool first) const
73 return new XMLCredentialsImpl(pathname);
76 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const DOMElement* e, bool first) const
78 return new XMLCredentialsImpl(e);
81 void XMLCredentialsImpl::init()
84 saml::NDC ndc("init");
86 Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Credentials");
89 if (!saml::XML::isElementNamed(m_root,::XML::CREDS_NS,SHIB_L(Credentials))) {
90 log.error("Construction requires a valid creds file: (creds:Credentials as root element)");
91 throw CredentialException("Construction requires a valid creds file: (creds:Credentials as root element)");
94 DOMElement* child=saml::XML::getFirstChildElement(m_root);
97 auto_ptr_char id(child->getAttributeNS(NULL,SHIB_L(Id)));
99 if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(FileResolver)))
100 cr_type="edu.internet2.middleware.shibboleth.common.Credentials.FileCredentialResolver";
101 else if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(CustomResolver))) {
102 auto_ptr_char c(child->getAttributeNS(NULL,SHIB_L(Class)));
106 if (!cr_type.empty()) {
108 IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(cr_type.c_str(),child);
109 ICredResolver* cr=dynamic_cast<ICredResolver*>(plugin);
111 m_resolverMap[id.get()]=cr;
113 log.error("plugin was not a credential resolver");
114 throw UnsupportedExtensionException("plugin was not a credential resolver");
117 catch (SAMLException& e) {
118 log.error("failed to instantiate credential resolver (%s): %s", id.get(), e.what());
123 log.error("unknown or unimplemented type of credential resolver (%s)", id.get());
124 throw CredentialException("Unknown or unimplemented type of credential resolver");
127 child=saml::XML::getNextSiblingElement(child);
130 catch (SAMLException& e) {
131 log.errorStream() << "Error while parsing creds configuration: " << e.what() << CategoryStream::ENDLINE;
132 for (resolvermap_t::iterator j=m_resolverMap.begin(); j!=m_resolverMap.end(); j++)
138 log.error("Unexpected error while parsing creds configuration");
139 for (resolvermap_t::iterator j=m_resolverMap.begin(); j!=m_resolverMap.end(); j++)
146 XMLCredentialsImpl::~XMLCredentialsImpl()
148 for (resolvermap_t::iterator j=m_resolverMap.begin(); j!=m_resolverMap.end(); j++)
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())