32 G_ASSERT( !
valid() ) ;
38 G_ASSERT( !valid() ) ;
42 const std::string &
id ,
bool id_encoding_xtext ,
const std::string & context ) :
46 G_ASSERT( secret_encoding ==
G::Str::lower(secret_encoding) ) ;
47 std::string reason =
check( secret , secret_encoding ,
id , id_encoding_xtext ) ;
49 throw context.empty() ? Error(reason) : Error(context,reason) ;
51 if( secret_encoding ==
"plain" )
56 else if( secret_encoding ==
"md5" && isDotted(secret) )
58 m_key = undotted( secret ) ;
59 m_mask_type = secret_encoding ;
65 m_mask_type = secret_encoding ;
70 const std::string &
id ,
bool id_encoding_xtext )
73 return "empty secret" ;
75 return "invalid encoding type" ;
79 return "invalid xtext encoding of id" ;
81 return "invalid xtext encoding of secret" ;
82 if( secret_encoding ==
"md5" && !( isDotted(secret) ||
G::Base64::valid(secret) ) )
83 return "invalid encoding of md5 secret" ;
84 if( secret_encoding !=
"md5" && secret_encoding !=
"plain" && !
G::Base64::valid(secret) )
85 return "invalid base64 encoding of secret" ;
86 return std::string() ;
101 return !m_key.empty() ;
106 if( !valid() )
throw Error() ;
112 return !m_mask_type.empty() ;
117 if( !valid() )
throw Error() ;
123 if( !valid() )
throw Error() ;
129 std::ostringstream ss ;
130 ss << (valid()?(masked()?maskType():std::string(
"plaintext")):std::string(
"missing")) <<
" secret" ;
131 std::string id_ = ( id_in.empty() && valid() ) ? m_id : id_in ;
136 if( !m_context.empty() )
138 ss <<
" from " << m_context ;
143bool GAuth::Secret::isDotted(
const std::string & s )
147 s.find_first_not_of(
"0123456789.") == std::string::npos &&
151std::string GAuth::Secret::undotted(
const std::string & s )
154 decimals.resize( 8U ) ;
156 for( std::size_t i = 0U ; i < 8U ; i++ )
158 G::Md5::big_t n = 0U ;
159 for(
const char & c : decimals[i] )
164 for(
int j = 0 ; j < 4 ; j++ )
166 unsigned char uc = ( n & 0xffU ) ;
168 result.push_back(
static_cast<char>(uc) ) ;
Encapsulates a shared secret from the secrets file plus the associated userid.
bool masked() const
Returns true if key() is masked.
static Secret none()
Factory function that returns a secret that is not valid() and has an empty id().
bool valid() const
Returns true if the secret is valid.
Secret(const std::string &secret, const std::string &secret_encoding, const std::string &id, bool id_encoding_xtext, const std::string &context=std::string())
Constructor used by the SecretsFile class.
static std::string check(const std::string &secret, const std::string &secret_encoding, const std::string &id, bool id_encoding_xtext)
Does a non-throwing check of the constructor parameters, returning an error message or the empty stri...
std::string info(const std::string &id=std::string()) const
Returns information for logging, excluding anything sensitive.
std::string maskType() const
Returns the masking function name, such as "MD5", or the empty string if not masked().
std::string id() const
Returns the associated identity. Throws if not valid().
std::string key() const
Returns the key. Throws if not valid().
static std::string decode(const std::string &, bool throw_on_invalid=false, bool strict=true)
Decodes the given string.
static bool valid(const std::string &, bool strict=true)
Returns true if the string is a valid base64 encoding, possibly allowing for embedded newlines,...
static void splitIntoFields(const std::string &in, StringArray &out, string_view ws, char escape='\0', bool remove_escapes=true)
Splits the string into fields.
static bool isPrintableAscii(const std::string &s)
Returns true if every character is a 7-bit, non-control character (ie.
static std::string printable(const std::string &in, char escape='\\')
Returns a printable representation of the given input string, using chacter code ranges 0x20 to 0x7e ...
static std::string lower(const std::string &s)
Returns a copy of 's' in which all Latin-1 upper-case characters have been replaced by lower-case cha...
static std::string decode(const std::string &)
Decodes the given string.
static bool valid(const std::string &, bool strict=false)
Returns true if a valid encoding.
std::vector< std::string > StringArray
A std::vector of std::strings.