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"
27 #include <boost/bind.hpp>
28 #include "CoralBase/MessageStream.h"
35 m_connectionName(connectionName),
41 for (std::map<std::string, coral::AuthenticationCredentials*>::iterator iData = m_data.begin(); iData != m_data.end();
54 std::map<std::string, coral::AuthenticationCredentials*>::iterator iRole = m_data.find(role);
55 if (iRole == m_data.end()) {
56 iRole = m_data.insert(std::make_pair(role,
new coral::AuthenticationCredentials(m_serviceName))).first;
79 :
coral::
Service(
key), m_isInitialized(
false), m_inputFileName(
""), m_data(), m_mutexLock(), m_callbackID(0) {
80 boost::function1<void, std::string> cb(
83 coral::Property* pm = dynamic_cast<coral::Property*>(
92 for (std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection = m_data.begin();
93 iConnection != m_data.end();
95 delete iConnection->second;
100 if (std::filesystem::is_directory(AuthPath)) {
104 m_inputFileName = AuthPath.string();
109 coral::MessageStream
log(
"cond::XMLAuthenticationService::processFile");
120 << coral::MessageStream::endmsg;
148 xercesc::MemBufInputSource* memBufInputSource =
nullptr;
151 xercesc::XercesDOMParser
parser;
152 parser.setValidationScheme(xercesc::XercesDOMParser::Val_Always);
153 parser.setDoNamespaces(
true);
155 xercesc::HandlerBase errorHandler;
156 parser.setErrorHandler(&errorHandler);
158 const char* bufferId =
"InMemoryDocument";
161 memBufInputSource =
new xercesc::MemBufInputSource((
const XMLByte*)
buffer, strlen(
buffer), bufferId,
false);
163 parser.parse(*memBufInputSource);
165 xercesc::DOMDocument* document =
parser.getDocument();
168 xercesc::XMLString::transcode(
"connection", tempStr, 19);
170 xercesc::DOMNodeList* connectionList = document->getElementsByTagName(tempStr);
172 if (connectionList) {
173 XMLSize_t numberOfConnections = connectionList->getLength();
175 for (XMLSize_t iConnection = 0; iConnection < numberOfConnections; ++iConnection) {
176 xercesc::DOMNode* connectionNode = connectionList->item(iConnection);
178 if (connectionNode) {
179 char* connectionName =
180 xercesc::XMLString::transcode(connectionNode->getAttributes()->item(0)->getNodeValue());
186 std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection =
187 m_data.find(sConnectionName);
188 if (iConnection != m_data.end()) {
189 credential = iConnection->second;
192 log <<
coral::Debug <<
"Credential parameters for connection string \"" << sConnectionName
193 <<
"\" have already been defined. Only new elements are appended, while existing will be ignored."
194 << coral::MessageStream::endmsg;
197 m_data.insert(std::make_pair(sConnectionName, credential));
200 xercesc::DOMNodeList* parameterList = connectionNode->getChildNodes();
206 xercesc::DOMNode* parameterNode = parameterList->item(iParameter);
208 if (parameterNode && parameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
209 char* nodeName = xercesc::XMLString::transcode(parameterNode->getNodeName());
213 if (sNodeName ==
"parameter") {
214 char* parameterName =
215 xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
218 char* parameterValue =
219 xercesc::XMLString::transcode(parameterNode->getAttributes()->item(1)->getNodeValue());
224 }
else if (sNodeName ==
"role") {
226 xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
231 xercesc::DOMNodeList* roleParameterList = parameterNode->getChildNodes();
233 if (roleParameterList) {
234 XMLSize_t numberOfRoleParameters = roleParameterList->getLength();
236 for (XMLSize_t iRoleParameter = 0; iRoleParameter < numberOfRoleParameters; ++iRoleParameter) {
237 xercesc::DOMNode* roleParameterNode = roleParameterList->item(iRoleParameter);
238 if (roleParameterNode && roleParameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
239 char* roleNodeName = xercesc::XMLString::transcode(roleParameterNode->getNodeName());
243 if (sRoleNodeName ==
"parameter") {
244 char* roleParameterName = xercesc::XMLString::transcode(
245 roleParameterNode->getAttributes()->item(0)->getNodeValue());
246 std::string sRoleParameterName = roleParameterName;
248 char* roleParameterValue = xercesc::XMLString::transcode(
249 roleParameterNode->getAttributes()->item(1)->getNodeValue());
250 std::string sRoleParameterValue = roleParameterValue;
267 }
catch (
const xercesc::XMLException& toCatch) {
268 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
274 }
catch (
const xercesc::DOMException& toCatch) {
275 char* message = xercesc::XMLString::transcode(toCatch.msg);
281 }
catch (
const xercesc::SAXException& toCatch) {
282 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
292 << coral::MessageStream::endmsg;
295 if (memBufInputSource)
296 delete memBufInputSource;
301 coral::MessageStream
log(
"cond::XMLAuthenticationService::initialize");
306 log <<
coral::Debug <<
"Could not open \"" << m_inputFileName <<
"\" for reading" << coral::MessageStream::endmsg;
312 }
catch (
const xercesc::XMLException& toCatch) {
313 char* message = xercesc::XMLString::transcode(toCatch.getMessage());
322 for (std::set<std::string>::const_reverse_iterator iFileName =
inputFileNames.rbegin();
333 if (!m_isInitialized)
339 for (std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection = m_data.begin();
340 iConnection != m_data.end();
342 delete iConnection->second;
344 m_isInitialized =
false;
349 boost::mutex::scoped_lock
lock(m_mutexLock);
350 if (!m_isInitialized) {
351 const_cast<cond::XMLAuthenticationService::XMLAuthenticationService*>(
this)->initialize();
353 std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::const_iterator iConnection =
355 if (iConnection == m_data.end())
357 return iConnection->second->credentials();
362 boost::mutex::scoped_lock
lock(m_mutexLock);
363 if (!m_isInitialized) {
364 const_cast<cond::XMLAuthenticationService::XMLAuthenticationService*>(
this)->initialize();
366 std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::const_iterator iConnection =
368 if (iConnection == m_data.end())
370 return iConnection->second->credentials(role);
374 coral::MessageStream
log(
"cond::XMLAuthenticationService::verifyFileName");
379 if (std::filesystem::exists(m_inputFileName)) {
380 if (std::filesystem::is_directory(m_inputFileName)) {
382 log <<
coral::Error <<
"Provided path \"" << m_inputFileName <<
"\" is a directory."
383 << coral::MessageStream::endmsg;
393 const char* thePathVariable = std::getenv(
"CORAL_AUTH_PATH");
394 if (!thePathVariable)
397 <<
"\" not found in the current directory. Trying in the search path." << coral::MessageStream::endmsg;
401 if (std::filesystem::exists(searchPath)) {
402 if (!std::filesystem::is_directory(searchPath)) {
403 log <<
coral::Debug <<
"Search path \"" << searchPath <<
"\" is not a directory." << coral::MessageStream::endmsg;
410 log <<
coral::Debug <<
"Search path \"" << searchPath <<
"\" does not exist." << coral::MessageStream::endmsg;