Scarab  v3.3.0
Project 8 C++ Utility Library
authentication.cc
Go to the documentation of this file.
1 /*
2  * mt_authentication.cc
3  *
4  * Created on: Jun 15, 2015
5  * Author: N.S. Oblath
6  */
7 
8 #define SCARAB_API_EXPORTS
9 
10 #include "authentication.hh"
11 
12 #include "logger.hh"
13 #include "param_codec.hh"
14 #include "path.hh"
15 
16 using std::string;
17 
18 namespace scarab
19 {
20  LOGGER( mtlog, "authentication" );
21 
22  authentication::authentication( const string& a_auth_filename ) :
23  param_node(),
24  f_auth_filename( a_auth_filename ),
25  f_is_loaded( false )
26  {
27  load( a_auth_filename );
28  }
29 
31  {
32  }
33 
34  bool authentication::load( const string& a_auth_file )
35  {
36  this->clear();
37 
38  f_auth_filename = a_auth_file;
39 
40  // expand path with standard assumptions
41  path t_auth_file_path = expand_path( a_auth_file );
42 
43  // is the file there?
44  bool t_auth_file_present = false;
45  if( ! t_auth_file_path.empty() )
46  {
47  LDEBUG( mtlog, "Looking for authentication file: <" << t_auth_file_path << ">" );
48  try
49  {
50  t_auth_file_present = exists( t_auth_file_path ) && is_regular_file( t_auth_file_path );
51  if( ! t_auth_file_present )
52  {
53  LERROR( mtlog, "File either doesn't exist (" << exists( t_auth_file_path ) << ") or isn't a regular file (" << is_regular_file( t_auth_file_path ) << ")" );
54  return false;
55  }
56  }
57  catch( boost::filesystem::filesystem_error& e )
58  {
59  LERROR( mtlog, "Unable to determine if the authentication file is a regular file: " << e.what() );
60  return false;
61  }
62  }
63 
64  // if so, load it
65  if( t_auth_file_present )
66  {
67  param_translator t_translator;
68  std::unique_ptr< param > t_read_file( t_translator.read_file( t_auth_file_path.string() ) );
69  if( t_read_file == NULL )
70  {
71  LERROR( mtlog, "Unable to parse authentication file" );
72  return false;
73  }
74  else if( ! t_read_file->is_node() )
75  {
76  LERROR( mtlog, "Authentication file must translate to a node" );
77  return false;
78  }
79  else
80  {
81  this->param_node::operator=( std::move(t_read_file->as_node()) );
82  LDEBUG( mtlog, "Authentications:\n" << *this );
83  f_is_loaded = true;
84  return true;
85  }
86  }
87  else
88  {
89  LWARN( mtlog, "Authentications not loaded" );
90  return false;
91  }
92  }
93 
94 } /* namespace scarab */
#define LWARN(...)
Definition: logger.hh:381
fs::path path
Definition: path.hh:25
path expand_path(const string &a_path)
Definition: path.cc:19
LOGGER(mtlog, "authentication")
#define LERROR(...)
Definition: logger.hh:382
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
#define LDEBUG(...)
Definition: logger.hh:377
bool load(const std::string &a_auth_file)
param_node & operator=(const param_node &rhs)
Definition: param_node.cc:54
param_ptr_t read_file(const std::string &a_filename, const param_node &a_options=param_node())
Definition: param_codec.cc:47
authentication(const std::string &a_auth_file)