32 std::string_view context ) :
33 m_hash_function(
G::Str::lower(hash_function)) ,
34 m_context(
G::sv_to_string(context))
36 std::string reason =
check(
id ,
secret , m_hash_function ) ;
38 throw context.empty() ? Error(reason) : Error(m_context,reason) ;
44GAuth::Secret::Secret()
46 G_ASSERT( !valid() ) ;
51 if(
id.first.empty() )
54 if( secret.first.empty() )
55 return "empty secret" ;
57 if( !validEncodingType(
id) )
58 return "invalid encoding type for id" ;
60 if( !validEncodingType(secret) )
61 return "invalid encoding type for secret" ;
63 if( !validEncoding(
id) )
64 return "invalid " + G::sv_to_string(
id.second) +
" encoding of id" ;
66 if( !validEncoding(secret) )
67 return "invalid " + G::sv_to_string(
id.second) +
" encoding of secret" ;
69 if( encoding(secret) == Encoding::dotted && !
G::Str::imatch(hash_function,
"md5"_sv) )
70 return "invalid use of dotted format" ;
82 return !m_secret.empty() ;
87 if( !valid() )
throw Error() ;
93 return !m_hash_function.empty() ;
98 if( !valid() )
throw Error() ;
104 if( !valid() )
throw Error() ;
105 return m_hash_function ;
110 std::ostringstream ss ;
111 ss << (valid()?(masked()?maskHashFunction():std::string(
"plaintext")):std::string(
"missing")) <<
" secret" ;
112 std::string id_ = ( id_in.empty() && valid() ) ? m_id : id_in ;
117 if( !m_context.empty() )
119 ss <<
" from " << m_context ;
128 s.find_first_not_of(
"0123456789."_sv) == std::string::npos &&
132std::string GAuth::Secret::undotted( std::string_view s )
137 G::Md5::big_t n = 0U ;
138 std::string_view d = decimal() ;
139 for(
const char & c : d )
142 n += (G::Md5::big_t(c)-
'0') ;
144 for(
int j = 0 ; j < 4 ; j++ )
146 unsigned char uc = ( n & 0xffU ) ;
148 result.push_back(
static_cast<char>(uc) ) ;
154bool GAuth::Secret::validEncodingType( Value value )
157 value.second.empty() ||
163GAuth::Secret::Encoding GAuth::Secret::encoding( Value value )
165 if( value.second.empty() )
166 return Encoding::raw ;
168 return Encoding::xtext ;
170 return Encoding::dotted ;
172 return Encoding::base64 ;
175bool GAuth::Secret::validEncoding( Value value )
177 if( encoding(value) == Encoding::raw )
179 else if( encoding(value) == Encoding::xtext )
181 else if( encoding(value) == Encoding::dotted )
182 return isDotted( value.first ) ;
189 if( encoding(value) == Encoding::raw )
190 return G::sv_to_string( value.first ) ;
191 else if( encoding(value) == Encoding::xtext )
193 else if( encoding(value) == Encoding::dotted )
194 return undotted( value.first ) ;
Encapsulates a userid/shared-secret/hash-function tuple from the secrets file.
std::string secret() const
Returns the secret shared key. Throws if not valid().
bool masked() const
Returns true if a non-empty hash function was passed to the ctor.
static Secret none()
Factory function that returns a secret that is not valid().
Secret(Value id, Value secret, std::string_view masking_hash_function={}, std::string_view context={})
Constructor used by the SecretsFile class.
std::string maskHashFunction() const
Returns the masking function name as passed to the ctor, such as "md5", or the empty string if not ma...
bool valid() const
Returns true if the secret is valid.
static bool isDotted(std::string_view)
Returns true if the given secret string looks like it is in the old dotted format rather than base64.
std::string info(const std::string &id={}) const
Returns information for logging, excluding anything sensitive.
static std::string decode(Value)
Decodes a value.
std::string id() const
Returns the associated identity. Throws if not valid().
static std::string check(Value id, Value secret, std::string_view masking_hash_function)
Does a non-throwing check of the constructor parameters, returning an error message or the empty stri...
static std::string decode(std::string_view, bool throw_on_invalid=false, bool strict=true)
Decodes the given string.
static bool valid(std::string_view, bool strict=true)
Returns true if the string is a valid base64 encoding, possibly allowing for embedded newlines,...
static bool imatch(char, char) noexcept
Returns true if the two characters are the same, ignoring seven-bit case.
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 ...
A zero-copy string field iterator where the field separators are short fixed strings.
std::size_t count() const noexcept
Returns the number of fields.
static std::string decode(std::string_view)
Decodes the given string.
static bool valid(std::string_view, bool strict=false)
Returns true if a valid encoding, or empty.