36 const char * login_challenge_1 =
"Username:" ;
37 const char * login_challenge_2 =
"Password:" ;
50 Response response(
const std::string & mechanism ,
const std::string & challenge )
const ;
52 std::string preferred()
const ;
53 std::string id()
const ;
54 std::string info()
const ;
55 static bool match(
const G::StringArray & mechanisms ,
const std::string & ) ;
60 mutable std::string m_info ;
61 mutable std::string m_id ;
75 std::string GAuth::SaslClientImp::preferred(
const G::StringArray & server_mechanisms )
const
79 return std::string() ;
88 for( G::StringArray::iterator p = our_list.begin() ; p != our_list.end() ; )
90 if( m_secrets.clientSecret((*p).substr(5U)).valid() )
93 p = our_list.erase( p ) ;
95 if( m_secrets.clientSecret(
"oauth").valid() )
97 our_list.push_back(
"XOAUTH2" ) ;
99 if( m_secrets.clientSecret(
"plain").valid() )
101 our_list.push_back( PLAIN ) ;
102 our_list.push_back( LOGIN ) ;
106 m_mechanisms.clear() ;
107 for( G::StringArray::iterator p = our_list.begin() ; p != our_list.end() ; ++p )
109 if( match(server_mechanisms,*p) )
111 m_mechanisms.push_back( *p ) ;
115 G_DEBUG(
"GAuth::SaslClientImp::preferred: server mechanisms: [" <<
G::Str::join(
",",server_mechanisms) <<
"]" ) ;
116 G_DEBUG(
"GAuth::SaslClientImp::preferred: our mechanisms: [" <<
G::Str::join(
",",our_list) <<
"]" ) ;
117 G_DEBUG(
"GAuth::SaslClientImp::preferred: usable mechanisms: [" <<
G::Str::join(
",",m_mechanisms) <<
"]" ) ;
119 return m_mechanisms.empty() ? std::string() : m_mechanisms.at(0U) ;
122 bool GAuth::SaslClientImp::next()
124 if( !m_mechanisms.empty() )
125 m_mechanisms.erase( m_mechanisms.begin() ) ;
126 return !m_mechanisms.empty() ;
129 std::string GAuth::SaslClientImp::preferred()
const
131 return m_mechanisms.empty() ? std::string() : m_mechanisms.at(0U) ;
135 const std::string & challenge )
const
140 rsp.sensitive = true ;
143 if( mechanism.find(
"CRAM-") == 0U )
145 std::string hash_type = mechanism.substr( 5U ) ;
146 secret = m_secrets.clientSecret( hash_type ) ;
147 if( !secret.valid() )
148 secret = m_secrets.clientSecret(
"plain" ) ;
149 rsp.data =
Cram::response( hash_type ,
true , secret , challenge , secret.id() ) ;
150 rsp.error = rsp.data.empty() ;
153 else if( mechanism ==
"APOP" )
155 secret = m_secrets.clientSecret(
"MD5" ) ;
156 rsp.data =
Cram::response(
"MD5" ,
false , secret , challenge , secret.id() ) ;
157 rsp.error = rsp.data.empty() ;
160 else if( mechanism == PLAIN )
162 secret = m_secrets.clientSecret(
"plain" ) ;
163 const std::string sep( 1U ,
'\0' ) ;
164 rsp.data = sep + secret.id() + sep + secret.key() ;
165 rsp.error = rsp.data.empty() ;
168 else if( mechanism == LOGIN && challenge == login_challenge_1 )
170 secret = m_secrets.clientSecret(
"plain" ) ;
171 rsp.data = secret.id() ;
172 rsp.error = rsp.data.empty() ;
174 rsp.sensitive = false ;
176 else if( mechanism == LOGIN && challenge == login_challenge_2 )
178 secret = m_secrets.clientSecret(
"plain" ) ;
179 rsp.data = secret.key() ;
180 rsp.error = rsp.data.empty() ;
183 else if( mechanism ==
"XOAUTH2" && challenge.empty() )
185 secret = m_secrets.clientSecret(
"oauth" ) ;
186 rsp.data = secret.key() ;
187 rsp.error = rsp.data.empty() ;
189 rsp.sensitive = true ;
191 else if( mechanism ==
"XOAUTH2" )
193 secret = m_secrets.clientSecret(
"oauth" ) ;
197 rsp.sensitive = false ;
202 std::ostringstream ss ;
203 ss <<
"using mechanism [" <<
G::Str::lower(mechanism) <<
"] and " << secret.info() ;
211 std::string GAuth::SaslClientImp::id()
const
216 std::string GAuth::SaslClientImp::info()
const
221 bool GAuth::SaslClientImp::active()
const
223 return m_secrets.valid() ;
226 bool GAuth::SaslClientImp::match(
const G::StringArray & mechanisms ,
const std::string & mechanism )
228 return std::find(mechanisms.begin(),mechanisms.end(),mechanism) != mechanisms.end() ;
245 return m_imp->active() ;
250 return m_imp->response( mechanism , challenge ) ;
255 return m_imp->preferred( server_mechanisms ) ;
260 return m_imp->next() ;
265 return m_imp->preferred() ;
275 return m_imp->info() ;
An interface to an underlying TLS library.
std::string info() const
Returns logging and diagnostic information, valid after the last response().
An interface used by GAuth::SaslClient to obtain a client id and its authentication secret...
SaslClient(const SaslClientSecrets &secrets)
Constructor. The secrets reference is kept.
std::vector< std::string > StringArray
A std::vector of std::strings.
bool active() const
Returns true if the constructor's secrets object is valid.
Response response(const std::string &mechanism, const std::string &challenge) const
Returns a response to the given challenge.
std::string id() const
Returns the authentication id, valid after the last response().
A private pimple-pattern implementation class used by GAuth::SaslClient.
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 std::string response(const std::string &hash_type, bool hmac, const Secret &secret, const std::string &challenge, const std::string &response_prefix)
Constructs a response to a challenge comprising the response-prefix, space, and digest-or-hmac of key...
static Secret none()
Factory function that returns a secret that is not valid() and has an empty id(). ...
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
bool next()
Moves to the next preferred mechanism.
std::string preferred() const
Returns the name of the current preferred mechanism after next() returns true.
static G::StringArray hashTypes(const std::string &prefix=std::string(), bool require_state=false)
Returns a list of supported hash types, such as "MD5" and "SHA1", ordered with the strongest first...
Result structure returned from GAuth::SaslClient::response.