5 #include "RelationalAccess/AuthenticationCredentials.h"
6 #include "CoralCommon/Cipher.h"
7 #include "RelationalAccess/AuthenticationServiceException.h"
8 #include "CoralKernel/IPropertyManager.h"
9 #include "CoralKernel/Property.h"
10 #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"
34 const std::string& connectionName ):
35 m_serviceName( serviceName ),
36 m_connectionName( connectionName ),
37 m_default( new coral::AuthenticationCredentials( serviceName ) ),
44 for ( std::map< std::string, coral::AuthenticationCredentials* >::iterator iData = m_data.begin();
45 iData != m_data.end(); ++iData )
51 const std::string&
value )
53 m_default->registerItem( item, value );
58 const std::string&
value,
59 const std::string& role )
61 std::map< std::string, coral::AuthenticationCredentials* >::iterator iRole = m_data.find( role );
62 if ( iRole == m_data.end() ) {
63 iRole = m_data.insert( std::make_pair( role,
new coral::AuthenticationCredentials( m_serviceName ) ) ).first;
65 iRole->second->registerItem( item, value );
68 const coral::IAuthenticationCredentials&
74 const coral::IAuthenticationCredentials&
90 m_isInitialized(
false ),
91 m_inputFileName(
"" ),
107 for ( std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.begin();
108 iConnection != m_data.end(); ++iConnection )
delete iConnection->second;
115 if(boost::filesystem::is_directory(boostAuthPath)){
119 m_inputFileName = boostAuthPath.string();
126 coral::MessageStream
log(
"cond::XMLAuthenticationService::processFile");
131 std::string
cont(
"");
133 inputFile.
read(inputFileName);
142 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 47
143 std::string
name = filePath.filename().string();
145 std::string name = filePath.leaf();
168 xercesc::MemBufInputSource* memBufInputSource = 0;
172 xercesc::XercesDOMParser
parser;
173 parser.setValidationScheme( xercesc::XercesDOMParser::Val_Always );
174 parser.setDoNamespaces(
true );
176 xercesc::HandlerBase errorHandler;
177 parser.setErrorHandler( &errorHandler );
180 const char* bufferId=
"InMemoryDocument";
181 const char* buffer = cont.c_str();
183 memBufInputSource =
new xercesc::MemBufInputSource( (
const XMLByte*)buffer,strlen(buffer),bufferId,
false );
185 parser.parse(*memBufInputSource);
187 xercesc::DOMDocument* document = parser.getDocument();
190 xercesc::XMLString::transcode(
"connection", tempStr, 19);
192 xercesc::DOMNodeList* connectionList = document->getElementsByTagName( tempStr );
194 if ( connectionList )
196 XMLSize_t numberOfConnections = connectionList->getLength();
198 for ( XMLSize_t iConnection = 0; iConnection < numberOfConnections; ++iConnection )
200 xercesc::DOMNode* connectionNode = connectionList->item( iConnection );
202 if ( connectionNode )
204 char* connectionName = xercesc::XMLString::transcode( connectionNode->getAttributes()->item( 0 )->getNodeValue() );
205 std::string sConnectionName = connectionName;
210 std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.find( sConnectionName );
211 if ( iConnection != m_data.end() ) {
212 credential = iConnection->second;
215 log<<
coral::Debug<<
"Credential parameters for connection string \""
217 <<
"\" have already been defined. Only new elements are appended, while existing will be ignored."
221 m_data.insert( std::make_pair( sConnectionName, credential ) );
224 xercesc::DOMNodeList* parameterList = connectionNode->getChildNodes();
228 XMLSize_t numberOfParameters = parameterList->getLength();
230 for ( XMLSize_t iParameter = 0; iParameter < numberOfParameters; ++iParameter )
232 xercesc::DOMNode* parameterNode = parameterList->item( iParameter );
234 if ( parameterNode && parameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE )
236 char* nodeName = xercesc::XMLString::transcode( parameterNode->getNodeName() );
237 std::string sNodeName = nodeName;
240 if ( sNodeName ==
"parameter" ) {
241 char* parameterName = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 0 )->getNodeValue() );
242 std::string sParameterName = parameterName;
244 char* parameterValue = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 1 )->getNodeValue() );
245 std::string sParameterValue = parameterValue;
250 else if ( sNodeName ==
"role" ) {
251 char* roleName = xercesc::XMLString::transcode( parameterNode->getAttributes()->item( 0 )->getNodeValue() );
252 std::string sRoleName = roleName;
256 xercesc::DOMNodeList* roleParameterList = parameterNode->getChildNodes();
259 if ( roleParameterList )
261 XMLSize_t numberOfRoleParameters = roleParameterList->getLength();
263 for ( XMLSize_t iRoleParameter = 0; iRoleParameter < numberOfRoleParameters; ++iRoleParameter )
265 xercesc::DOMNode* roleParameterNode = roleParameterList->item( iRoleParameter );
266 if ( roleParameterNode && roleParameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE )
268 char* roleNodeName = xercesc::XMLString::transcode( roleParameterNode->getNodeName() );
269 std::string sRoleNodeName = roleNodeName;
272 if ( sRoleNodeName ==
"parameter" ) {
273 char* roleParameterName = xercesc::XMLString::transcode( roleParameterNode->getAttributes()->item( 0 )->getNodeValue() );
274 std::string sRoleParameterName = roleParameterName;
276 char* roleParameterValue = xercesc::XMLString::transcode( roleParameterNode->getAttributes()->item( 1 )->getNodeValue() );
277 std::string sRoleParameterValue = roleParameterValue;
295 catch (
const xercesc::XMLException& toCatch )
297 char*
message = xercesc::XMLString::transcode( toCatch.getMessage() );
304 catch (
const xercesc::DOMException& toCatch )
306 char*
message = xercesc::XMLString::transcode( toCatch.msg );
313 catch (
const xercesc::SAXException& toCatch )
315 char*
message = xercesc::XMLString::transcode( toCatch.getMessage() );
328 if(memBufInputSource)
delete memBufInputSource;
336 coral::MessageStream
log(
"cond::XMLAuthenticationService::initialize");
337 std::set< std::string > inputFileNames = this->verifyFileName();
338 if ( inputFileNames.empty() )
348 xercesc::XMLPlatformUtils::Initialize();
350 catch (
const xercesc::XMLException& toCatch )
352 char*
message = xercesc::XMLString::transcode( toCatch.getMessage() );
361 for ( std::set< std::string >::const_reverse_iterator iFileName = inputFileNames.rbegin();
362 iFileName != inputFileNames.rend(); ++iFileName ) {
368 xercesc::XMLPlatformUtils::Terminate();
371 if(!m_isInitialized)
reset();
376 for ( std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::iterator iConnection = m_data.begin();
377 iConnection != m_data.end(); ++iConnection )
delete iConnection->second;
379 m_isInitialized =
false;
384 const coral::IAuthenticationCredentials&
387 boost::mutex::scoped_lock
lock(m_mutexLock);
388 if ( ! m_isInitialized ) {
391 std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::const_iterator iConnection = m_data.find( connectionString );
392 if ( iConnection == m_data.end() )
393 throw coral::UnknownConnectionException( this->
name(), connectionString );
398 const coral::IAuthenticationCredentials&
400 const std::string& role )
const
402 boost::mutex::scoped_lock
lock(m_mutexLock);
403 if ( ! m_isInitialized ) {
406 std::map< std::string, cond::XMLAuthenticationService::DataSourceEntry* >::const_iterator iConnection = m_data.find( connectionString );
407 if ( iConnection == m_data.end() )
408 throw coral::UnknownConnectionException( this->
name(), connectionString );
413 std::set< std::string >
416 coral::MessageStream
log(
"cond::XMLAuthenticationService::verifyFileName");
421 if ( boost::filesystem::exists( m_inputFileName ) ) {
422 if(boost::filesystem::is_directory( m_inputFileName )){
428 fileNames.insert( fullPath.string() );
429 if(filePath.is_complete())
return fileNames;
433 const char* thePathVariable = ::getenv(
"CORAL_AUTH_PATH" );
434 if ( ! thePathVariable )
return fileNames;
437 std::string searchPath(thePathVariable);
439 if(boost::filesystem::exists(searchPath)){
440 if(!boost::filesystem::is_directory( searchPath )){
445 fullPath /= filePath;
446 fileNames.insert( fullPath.string() );
virtual char const * what() const
void appendCredentialItemForRole(const std::string &item, const std::string &value, const std::string &role)
bool initialize()
Service framework related initialization.
static PFTauRenderPlugin instance
static const std::string COND_AUTH_PATH_PROPERTY
static const std::string serviceName
void reset()
Reset parsed data.
#define DEFINE_CORALSERVICE(type, name)
std::set< std::string > verifyFileName()
Verifies the existence of the authentication files.
bool read(const std::string &fileName)
XMLAuthenticationService(const std::string &name)
Standard Constructor.
void setAuthenticationPath(const std::string &inputPath)
Sets the input file name.
virtual ~XMLAuthenticationService()
Standard Destructor.
const std::string & content() const
bool processFile(const std::string &inputFileName)
Parses an xml file.
void appendCredentialItem(const std::string &item, const std::string &value)
coral::Property::CallbackID m_callbackID
const coral::IAuthenticationCredentials & credentials() const
constexpr char XML_AUTHENTICATION_FILE[]
~DataSourceEntry()
Destructor.
void reset(double vett[256])
const coral::IAuthenticationCredentials & credentials(const std::string &connectionString) const