4 #include "RelationalAccess/AuthenticationCredentials.h"
5 #include "CoralCommon/Cipher.h"
6 #include "RelationalAccess/AuthenticationServiceException.h"
7 #include "CoralKernel/IPropertyManager.h"
8 #include "CoralKernel/Property.h"
9 #include "CoralKernel/Context.h"
12 #include "xercesc/parsers/XercesDOMParser.hpp"
13 #include "xercesc/framework/MemBufInputSource.hpp"
14 #include "xercesc/dom/DOM.hpp"
15 #include "xercesc/sax/HandlerBase.hpp"
16 #include "xercesc/util/XMLString.hpp"
17 #include "xercesc/util/PlatformUtils.hpp"
25 #include <boost/filesystem.hpp>
26 #include <boost/version.hpp>
27 #include <boost/bind.hpp>
29 #include "CoralBase/MessageStream.h"
36 m_connectionName(connectionName),
42 for (std::map<std::string, coral::AuthenticationCredentials*>::iterator iData = m_data.begin(); iData != m_data.end();
55 std::map<std::string, coral::AuthenticationCredentials*>::iterator iRole = m_data.find(role);
56 if (iRole == m_data.end()) {
57 iRole = m_data.insert(std::make_pair(role,
new coral::AuthenticationCredentials(m_serviceName))).first;
80 :
coral::
Service(
key), m_isInitialized(
false), m_inputFileName(
""), m_data(), m_mutexLock(), m_callbackID(0) {
81 boost::function1<void, std::string> cb(
84 coral::Property* pm = dynamic_cast<coral::Property*>(
93 for (std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection = m_data.begin();
94 iConnection != m_data.end();
96 delete iConnection->second;
101 if (boost::filesystem::is_directory(boostAuthPath)) {
105 m_inputFileName = boostAuthPath.string();
110 coral::MessageStream
log(
"cond::XMLAuthenticationService::processFile");
121 << coral::MessageStream::endmsg;
127 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 47
153 xercesc::MemBufInputSource* memBufInputSource =
nullptr;
156 xercesc::XercesDOMParser
parser;
157 parser.setValidationScheme(xercesc::XercesDOMParser::Val_Always);
158 parser.setDoNamespaces(
true);
160 xercesc::HandlerBase errorHandler;
161 parser.setErrorHandler(&errorHandler);
163 const char* bufferId =
"InMemoryDocument";
166 memBufInputSource =
new xercesc::MemBufInputSource((
const XMLByte*)
buffer, strlen(
buffer), bufferId,
false);
168 parser.parse(*memBufInputSource);
170 xercesc::DOMDocument* document =
parser.getDocument();
173 xercesc::XMLString::transcode(
"connection", tempStr, 19);
175 xercesc::DOMNodeList* connectionList = document->getElementsByTagName(tempStr);
177 if (connectionList) {
178 XMLSize_t numberOfConnections = connectionList->getLength();
180 for (XMLSize_t iConnection = 0; iConnection < numberOfConnections; ++iConnection) {
181 xercesc::DOMNode* connectionNode = connectionList->item(iConnection);
183 if (connectionNode) {
184 char* connectionName =
185 xercesc::XMLString::transcode(connectionNode->getAttributes()->item(0)->getNodeValue());
191 std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection =
192 m_data.find(sConnectionName);
193 if (iConnection != m_data.end()) {
194 credential = iConnection->second;
197 log <<
coral::Debug <<
"Credential parameters for connection string \"" << sConnectionName
198 <<
"\" have already been defined. Only new elements are appended, while existing will be ignored."
199 << coral::MessageStream::endmsg;
202 m_data.insert(std::make_pair(sConnectionName, credential));
205 xercesc::DOMNodeList* parameterList = connectionNode->getChildNodes();
211 xercesc::DOMNode* parameterNode = parameterList->item(iParameter);
213 if (parameterNode && parameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
214 char* nodeName = xercesc::XMLString::transcode(parameterNode->getNodeName());
218 if (sNodeName ==
"parameter") {
219 char* parameterName =
220 xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
223 char* parameterValue =
224 xercesc::XMLString::transcode(parameterNode->getAttributes()->item(1)->getNodeValue());
229 }
else if (sNodeName ==
"role") {
231 xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
236 xercesc::DOMNodeList* roleParameterList = parameterNode->getChildNodes();
238 if (roleParameterList) {
239 XMLSize_t numberOfRoleParameters = roleParameterList->getLength();
241 for (XMLSize_t iRoleParameter = 0; iRoleParameter < numberOfRoleParameters; ++iRoleParameter) {
242 xercesc::DOMNode* roleParameterNode = roleParameterList->item(iRoleParameter);
243 if (roleParameterNode && roleParameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
244 char* roleNodeName = xercesc::XMLString::transcode(roleParameterNode->getNodeName());
248 if (sRoleNodeName ==
"parameter") {
249 char* roleParameterName = xercesc::XMLString::transcode(
250 roleParameterNode->getAttributes()->item(0)->getNodeValue());
251 std::string sRoleParameterName = roleParameterName;
253 char* roleParameterValue = xercesc::XMLString::transcode(
254 roleParameterNode->getAttributes()->item(1)->getNodeValue());
255 std::string sRoleParameterValue = roleParameterValue;
272 }
catch (
const xercesc::XMLException& toCatch) {
273 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
279 }
catch (
const xercesc::DOMException& toCatch) {
280 char* message = xercesc::XMLString::transcode(toCatch.msg);
286 }
catch (
const xercesc::SAXException& toCatch) {
287 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
297 << coral::MessageStream::endmsg;
300 if (memBufInputSource)
301 delete memBufInputSource;
306 coral::MessageStream
log(
"cond::XMLAuthenticationService::initialize");
311 log <<
coral::Debug <<
"Could not open \"" << m_inputFileName <<
"\" for reading" << coral::MessageStream::endmsg;
317 }
catch (
const xercesc::XMLException& toCatch) {
318 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
327 for (std::set<std::string>::const_reverse_iterator iFileName =
inputFileNames.rbegin();
338 if (!m_isInitialized)
344 for (std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection = m_data.begin();
345 iConnection != m_data.end();
347 delete iConnection->second;
349 m_isInitialized =
false;
354 boost::mutex::scoped_lock
lock(m_mutexLock);
355 if (!m_isInitialized) {
356 const_cast<cond::XMLAuthenticationService::XMLAuthenticationService*>(
this)->initialize();
358 std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::const_iterator iConnection =
360 if (iConnection == m_data.end())
362 return iConnection->second->credentials();
367 boost::mutex::scoped_lock
lock(m_mutexLock);
368 if (!m_isInitialized) {
369 const_cast<cond::XMLAuthenticationService::XMLAuthenticationService*>(
this)->initialize();
371 std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::const_iterator iConnection =
373 if (iConnection == m_data.end())
375 return iConnection->second->credentials(role);
379 coral::MessageStream
log(
"cond::XMLAuthenticationService::verifyFileName");
384 if (boost::filesystem::exists(m_inputFileName)) {
385 if (boost::filesystem::is_directory(m_inputFileName)) {
387 log <<
coral::Error <<
"Provided path \"" << m_inputFileName <<
"\" is a directory."
388 << coral::MessageStream::endmsg;
398 const char* thePathVariable = std::getenv(
"CORAL_AUTH_PATH");
399 if (!thePathVariable)
402 <<
"\" not found in the current directory. Trying in the search path." << coral::MessageStream::endmsg;
406 if (boost::filesystem::exists(searchPath)) {
407 if (!boost::filesystem::is_directory(searchPath)) {
408 log <<
coral::Debug <<
"Search path \"" << searchPath <<
"\" is not a directory." << coral::MessageStream::endmsg;
415 log <<
coral::Debug <<
"Search path \"" << searchPath <<
"\" does not exist." << coral::MessageStream::endmsg;