41 const std::string & server_type ) :
44 m_debug_name(debug_name) ,
45 m_server_type(server_type) ,
47 m_check_time(
G::DateTime::now())
49 m_server_type = m_server_type.empty() ? std::string(
"server") : m_server_type ;
50 G_DEBUG(
"GAuth::Secrets: " << m_debug_name <<
": \"" << path <<
"\"" ) ;
51 m_valid = ! path.
str().empty() ;
61 void GAuth::SecretsFile::reread()
const
66 void GAuth::SecretsFile::reread(
int )
71 G_DEBUG(
"GAuth::SecretsFile::reread: file time checked at " << m_check_time <<
": now " << now ) ;
72 if( now != m_check_time )
76 G_DEBUG(
"GAuth::SecretsFile::reread: current file time " << t <<
": saved file time " << m_file_time ) ;
77 if( t != m_file_time )
79 G_LOG_S(
"GAuth::Secrets: re-reading secrets file: " << m_path ) ;
86 void GAuth::SecretsFile::read(
const G::Path & path )
88 std::auto_ptr<std::ifstream> file ;
91 file <<=
new std::ifstream( path.
str().c_str() ) ;
95 std::ostringstream ss ;
96 ss <<
"reading \"" << path <<
"\" for " << m_debug_name <<
" secrets" ;
97 throw Secrets::OpenError( ss.str() ) ;
99 m_file_time = readFileTime( path ) ;
103 unsigned int count = read( *file.get() ) ;
105 G_DEBUG(
"GAuth::SecretsFile::read: processed " << (count-1) <<
" records" ) ;
114 unsigned int GAuth::SecretsFile::read( std::istream & file )
116 unsigned int count = 0U ;
117 for(
unsigned int line_number = 1U ; file.good() ; ++line_number )
124 if( !line.empty() && line.at(0U) !=
'#' )
128 if( word_array.size() > 4U )
130 G_WARNING(
"GAuth::SecretsFile::read: ignoring extra fields on line "
131 << line_number <<
" of secrets file" ) ;
133 if( word_array.size() >= 4U )
135 bool processed = process( word_array[0U] , word_array[1U] , word_array[2U] , word_array[3U] ) ;
136 G_DEBUG(
"GAuth::SecretsFile::read: line #" << line_number << (processed?
" used":
" ignored") ) ;
142 G_WARNING(
"GAuth::SecretsFile::read: ignoring line "
143 << line_number <<
" of secrets file: too few fields" ) ;
150 bool GAuth::SecretsFile::process( std::string side , std::string mechanism , std::string
id , std::string secret )
156 if( mechanism ==
G::Str::upper(m_server_type) || mechanism ==
"CLIENT" )
157 std::swap( mechanism , side ) ;
162 std::string key = mechanism +
":" + id ;
163 std::string value = secret ;
164 m_map.insert( std::make_pair(key,value) ) ;
165 m_set.insert( mechanism ) ;
168 else if( side ==
"CLIENT" )
171 std::string key = mechanism +
" client" ;
172 std::string value =
id +
" " + secret ;
173 m_map.insert( std::make_pair(key,value) ) ;
189 Map::const_iterator p = m_map.find( mechanism+
" client" ) ;
191 if( p != m_map.end() && (*p).second.find(
" ") != std::string::npos )
193 G_DEBUG(
"GAuth::Secrets::id: " << m_debug_name <<
": \"" << mechanism <<
"\"" ) ;
200 Map::const_iterator p = m_map.find( mechanism+
" client" ) ;
201 if( p == m_map.end() || (*p).second.find(
" ") == std::string::npos )
202 return std::string() ;
211 if( p == m_map.end() )
212 return std::string() ;
219 return m_path.str() ;
224 return m_set.find(mechanism) != m_set.end() ;
std::string str() const
Returns the path string.
static EpochTime now()
Returns the current epoch time.
static std::string decode(const std::string &)
Decodes the given string.
static std::string encode(const std::string &)
Encodes the given string.
std::vector< std::string > StringArray
A std::vector of std::strings.
A class which acquires the process's special privileges on construction and releases them on destruct...
static void splitIntoTokens(const std::string &in, Strings &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
std::string secret(const std::string &mechanism) const
std::string id(const std::string &mechanism) const
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
bool contains(const std::string &mechanism) const
A implementation class used by GAuth::Secrets.
SecretsFile(const G::Path &path, bool auto_, const std::string &name, const std::string &type)
static std::string upper(const std::string &s)
Returns a copy of 's' in which all lowercase characters have been replaced by uppercase characters...
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
A Path object represents a file system path.
static time_type time(const Path &file)
Returns the file's timestamp.
static std::string ws()
A convenience function returning standard whitespace characters.
static void toUpper(std::string &s)
Replaces all lowercase characters in string 's' by uppercase characters.