39 const std::string & server_type ) :
42 m_debug_name(debug_name) ,
43 m_server_type(server_type) ,
45 m_check_time(
G::DateTime::now())
47 m_server_type = m_server_type.empty() ? std::string(
"server") : m_server_type ;
48 G_DEBUG(
"GAuth::Secrets: " << m_debug_name <<
": \"" << path <<
"\"" ) ;
49 m_valid = ! path.
str().empty() ;
63 void GAuth::SecretsFile::reread()
const
68 void GAuth::SecretsFile::reread(
int )
73 G_DEBUG(
"GAuth::SecretsFile::reread: file time checked at " << m_check_time <<
": now " << now ) ;
74 if( now != m_check_time )
78 G_DEBUG(
"GAuth::SecretsFile::reread: current file time " << t <<
": saved file time " << m_file_time ) ;
79 if( t != m_file_time )
81 G_LOG_S(
"GAuth::Secrets: re-reading secrets file: " << m_path ) ;
88 void GAuth::SecretsFile::read(
const G::Path & path )
91 unique_ptr<std::ifstream> file ;
94 file.reset(
new std::ifstream( path.
str().c_str() ) ) ;
98 std::ostringstream ss ;
99 ss <<
"reading \"" << path <<
"\" for " << m_debug_name <<
" secrets" ;
100 throw Secrets::OpenError( ss.str() ) ;
102 m_file_time = readFileTime( path ) ;
108 read( *file.get() ) ;
111 G_DEBUG(
"GAuth::SecretsFile::read: processed " << m_map.size() <<
" records" ) ;
112 if( m_warnings.size() )
114 std::string prefix = path.
basename() ;
115 G_WARNING(
"GAuth::SecretsFile::read: problems reading " << m_debug_name
116 <<
" secrets file [" << path.
str() <<
"]..." ) ;
117 for( Warnings::iterator p = m_warnings.begin() ; p != m_warnings.end() ; ++p )
119 G_WARNING(
"GAuth::SecretsFile::read: " << prefix <<
"(" << (*p).first <<
"): " << (*p).second ) ;
130 void GAuth::SecretsFile::read( std::istream & file )
132 for(
unsigned int line_number = 1U ; file.good() ; ++line_number )
139 if( !line.empty() && line.at(0U) !=
'#' )
143 if( word_array.size() >= 4U )
146 process( line_number , word_array[0U] , word_array[1U] , word_array[2U] , word_array[3U] ) ;
150 warning( line_number ,
"line ignored: too few fields" ,
"" ) ;
156 void GAuth::SecretsFile::process(
unsigned int line_number , std::string side ,
157 std::string encoding_type , std::string
id , std::string secret )
159 G_ASSERT( !side.empty() && !encoding_type.empty() && !
id.empty() && !secret.empty() ) ;
164 if( encoding_type ==
G::Str::lower(m_server_type) || encoding_type ==
"client" )
165 std::swap( encoding_type , side ) ;
170 std::string key = serverKey( encoding_type ,
id ) ;
171 Value value( secret , line_number ) ;
172 bool inserted = m_map.insert(std::make_pair(key,value)).second ;
174 m_types.insert( canonical(encoding_type) ) ;
176 warning( line_number ,
"line ignored: duplicate key" , key ) ;
178 else if( side ==
"client" )
181 std::string key = clientKey( encoding_type ) ;
182 Value value(
id +
" " + secret , line_number ) ;
183 bool inserted = m_map.insert(std::make_pair(key,value)).second ;
185 warning( line_number ,
"line ignored: duplicate key" , key ) ;
189 G_DEBUG(
"GAuth::SecretsFile::process: ignoring line number " << line_number <<
": not 'client' or '" << m_server_type <<
"'" ) ;
193 void GAuth::SecretsFile::warning(
unsigned int line_number ,
const std::string & message ,
const std::string & more )
195 m_warnings.push_back( Warnings::value_type(line_number,message+(more.empty()?std::string():
": ")+more) ) ;
198 std::string GAuth::SecretsFile::canonical(
const std::string & encoding_type )
201 if( type ==
"cram-md5" ) type =
"md5" ;
202 if( type ==
"apop" ) type =
"md5" ;
203 if( type ==
"login" ) type =
"plain" ;
207 std::string GAuth::SecretsFile::serverKey( std::string encoding_type ,
const std::string &
id )
214 return "server " + canonical(encoding_type) +
" " + id ;
217 std::string GAuth::SecretsFile::clientKey( std::string encoding_type )
222 return "client " + canonical(encoding_type) ;
229 Map::const_iterator p = m_map.find( clientKey(encoding_type) ) ;
230 if( p == m_map.end() )
237 std::string str_secret =
G::Str::tail( (*p).second.s ,
" " ) ;
238 return Secret( str_secret , canonical(encoding_type) ,
id , context((*p).second.n) ) ;
249 Map::const_iterator p = m_map.find( serverKey(encoding_type,
G::Xtext::encode(
id)) ) ;
250 if( p == m_map.end() )
256 return Secret( (*p).second.s , canonical(encoding_type) ,
id , context((*p).second.n) ) ;
262 std::pair<std::string,std::string> result ;
263 std::string encoding_type =
"none" ;
264 std::string
id = address_range ;
265 Map::const_iterator p = m_map.find( serverKey(encoding_type,
G::Xtext::encode(
id)) ) ;
266 if( p != m_map.end() )
268 result.first = (*p).second.s ;
269 result.second = context( (*p).second.n ) ;
276 return m_path.str() ;
281 return m_types.find(canonical(encoding_type)) != m_types.end() ;
284 std::string GAuth::SecretsFile::context(
unsigned int line_number )
std::string str() const
Returns the path string.
A subsecond-resolution timestamp based on a time_t.
std::pair< std::string, std::string > serverTrust(const std::string &address_range) const
Returns a non-empty trustee name if the server trusts clients in the given address range...
SecretsFile(const G::Path &path, bool auto_reread, const std::string &debug_name, const std::string &server_type=std::string())
Constructor to read "client" and "" records from the named file.
static std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static std::string decode(const std::string &)
Decodes the given string.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
static std::string head(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
bool valid() const
Returns true if the file path was supplied in the ctor.
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, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
~SecretsFile()
Destructor.
static void trim(std::string &s, const std::string &ws)
Trims both ends of s, taking off any of the 'ws' characters.
static EpochTime now()
Returns the current epoch time.
Secret serverSecret(const std::string &encoding_type, const std::string &id) const
Returns the server secret for the given id and encoding-type.
static std::string lower(const std::string &s)
Returns a copy of 's' in which all Latin-1 uppercase characters have been replaced by lowercase chara...
static void toLower(std::string &s)
Replaces all Latin-1 uppercase characters in string 's' by lowercase characters.
A class to read authentication secrets from file, used by GAuth::Secrets.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
std::string path() const
Returns the file path, as supplied to the ctor.
Secret clientSecret(const std::string &encoding_type) const
Returns the client id and secret for the given encoding-type.
bool contains(const std::string &server_encoding_type) const
Returns true if the given server encoding-type is represented.
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
static Secret none()
Factory function that returns a secret that is not valid() and has an empty id(). ...
static EpochTime time(const Path &file)
Returns the file's timestamp.
Encapsulates a shared secret from the secrets file plus the associated userid.
A Path object represents a file system path.
static std::string ws()
Returns a string of standard whitespace characters.