36 const char * login_challenge_1 =
"Username:" ;
37 const char * login_challenge_2 =
"Password:" ;
47 std::string mechanisms(
const std::string & )
const ;
48 bool init(
const std::string & mechanism ) ;
49 std::string mechanism()
const ;
50 std::string initialChallenge()
const ;
51 std::string apply(
const std::string & response ,
bool & done ) ;
53 bool trustedCore(
const std::string & ,
const GNet::Address & )
const ;
55 std::string id()
const ;
56 bool authenticated()
const ;
63 std::string m_mechanism ;
64 std::string m_challenge ;
65 bool m_authenticated ;
67 std::string m_trustee ;
68 bool m_advertise_login ;
69 bool m_advertise_plain ;
70 bool m_advertise_cram_md5 ;
75 GAuth::SaslServerBasicImp::SaslServerBasicImp(
const SaslServerSecrets & secrets ,
bool allow_apop ) :
76 m_allow_apop(allow_apop) ,
79 m_authenticated(false)
82 m_mechanisms.push_back(
"PLAIN" ) ;
83 m_mechanisms.push_back(
"LOGIN" ) ;
85 m_mechanisms.push_back(
"XOAUTH2" ) ;
88 std::string GAuth::SaslServerBasicImp::mechanisms(
const std::string & sep )
const
93 bool GAuth::SaslServerBasicImp::init(
const std::string & mechanism_in )
95 m_first_apply = true ;
96 m_authenticated = false ;
100 m_mechanism.erase() ;
103 if( m_allow_apop && mechanism ==
"APOP" )
105 m_mechanism = mechanism ;
109 else if( std::find(m_mechanisms.begin(),m_mechanisms.end(),mechanism) == m_mechanisms.end() )
111 G_DEBUG(
"GAuth::SaslServerBasicImp::init: requested mechanism [" << mechanism <<
"] is not in our list" ) ;
114 else if( mechanism.find(
"CRAM-") == 0U )
116 m_mechanism = mechanism ;
122 m_mechanism = mechanism ;
127 std::string GAuth::SaslServerBasicImp::initialChallenge()
const
130 if( m_mechanism ==
"PLAIN" )
131 return std::string() ;
132 else if( m_mechanism ==
"XOAUTH2" )
133 return std::string() ;
134 else if( m_mechanism ==
"LOGIN" )
135 return login_challenge_1 ;
140 std::string GAuth::SaslServerBasicImp::apply(
const std::string & response ,
bool & done )
142 G_DEBUG(
"GAuth::SaslServerBasic::apply: response: \"" <<
G::Str::printable(response) <<
"\"" ) ;
144 bool first_apply = m_first_apply ;
145 m_first_apply = false ;
150 std::string next_challenge ;
152 if( m_mechanism.find(
"CRAM-") == 0U || m_mechanism ==
"APOP" )
158 std::string hash_type = m_mechanism ==
"APOP" ?
"MD5" : m_mechanism.substr(5U) ;
159 secret = m_secrets.serverSecret( hash_type ,
id ) ;
160 if( !secret.valid() )
161 secret = m_secrets.serverSecret(
"plain" ,
id ) ;
163 if( !secret.valid() )
165 m_authenticated = false ;
171 m_mechanism ==
"APOP" ?
172 Cram::validate(
"MD5" ,
false , secret , m_challenge , response ) :
173 Cram::validate( m_mechanism.substr(5U) , true , secret , m_challenge , response ) ;
177 else if( m_mechanism ==
"PLAIN" )
180 std::string sep( 1U ,
'\0' ) ;
181 std::string id_pwd_in =
G::Str::tail( response , sep ) ;
184 secret = m_secrets.serverSecret(
"plain" ,
id ) ;
186 m_authenticated = secret.valid() && !
id.empty() && !pwd_in.empty() && pwd_in == secret.key() ;
190 else if( m_mechanism ==
"XOAUTH2" && first_apply )
195 m_authenticated = true ;
200 next_challenge =
"just testing" ;
204 else if( m_mechanism ==
"XOAUTH2" )
206 m_authenticated = false ;
209 else if( first_apply )
212 G_ASSERT( m_mechanism ==
"LOGIN" ) ;
213 id = m_id = response ;
215 next_challenge = login_challenge_2 ;
219 G_ASSERT( m_mechanism ==
"LOGIN" ) ;
221 secret = m_secrets.serverSecret(
"plain" , m_id ) ;
222 m_authenticated = secret.valid() && !response.empty() && response == secret.key() ;
228 std::ostringstream ss ;
230 << (m_authenticated?
"successful":
"failed") <<
" authentication of remote client using mechanism ["
231 <<
G::Str::lower(m_mechanism) <<
"] and " << secret.info(
id) ;
232 if( m_authenticated )
233 G_LOG(
"GAuth::SaslServerBasicImp::apply: " << ss.str() ) ;
235 G_WARNING(
"GAuth::SaslServerBasicImp::apply: " << ss.str() ) ;
238 return next_challenge ;
241 bool GAuth::SaslServerBasicImp::trusted(
const GNet::Address & address )
const
243 G_DEBUG(
"GAuth::SaslServerBasicImp::trusted: \"" << address.
hostPartString() <<
"\"" ) ;
245 for( G::StringArray::iterator p = wc.begin() ; p != wc.end() ; ++p )
247 if( trustedCore(*p,address) )
253 bool GAuth::SaslServerBasicImp::trustedCore(
const std::string & address_wildcard ,
const GNet::Address & address )
const
255 G_DEBUG(
"GAuth::SaslServerBasicImp::trustedCore: \"" << address_wildcard <<
"\", \"" << address.
hostPartString() <<
"\"" ) ;
256 std::pair<std::string,std::string> pair = m_secrets.serverTrust( address_wildcard ) ;
257 std::string & trustee = pair.first ;
258 if( !trustee.empty() )
260 G_LOG(
"GAuth::SaslServer::trusted: trusting [" << address.
hostPartString() <<
"]: "
261 <<
"matched [" << address_wildcard <<
"] " << pair.second ) ;
262 const_cast<SaslServerBasicImp*
>(
this)->m_trustee = trustee ;
271 bool GAuth::SaslServerBasicImp::active()
const
273 return m_secrets.valid() ;
276 std::string GAuth::SaslServerBasicImp::mechanism()
const
281 std::string GAuth::SaslServerBasicImp::id()
const
283 return m_authenticated ? m_id : m_trustee ;
286 bool GAuth::SaslServerBasicImp::authenticated()
const
288 return m_authenticated ;
305 return m_imp->mechanisms( std::string(1U,c) ) ;
310 return m_imp->mechanism() ;
315 return m_imp->trusted( address ) ;
320 return m_imp->active() ;
325 return m_imp->init( mechanism ) ;
331 return m !=
"PLAIN" && m !=
"LOGIN" ;
336 return m_imp->initialChallenge() ;
341 return m_imp->apply( response , done ) ;
346 return m_imp->authenticated() ;
virtual std::string mechanism() const override
Override from GAuth::SaslServer.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
virtual std::string mechanisms(char sep= ' ') const override
Override from GAuth::SaslServer.
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.
virtual ~SaslServerBasic()
Destructor.
virtual bool mustChallenge() const override
Override from GAuth::SaslServer.
virtual std::string apply(const std::string &response, bool &done) override
Override from GAuth::SaslServer.
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.
The GNet::Address class encapsulates a TCP/UDP transport address.
std::vector< std::string > StringArray
A std::vector of std::strings.
virtual bool active() const override
Override from GAuth::SaslServer.
virtual bool requiresEncryption() const override
Override from GAuth::SaslServer.
static bool validate(const std::string &hash_type, bool hmac, const Secret &secret, const std::string &challenge, const std::string &response)
Validates the response with respect to the original challenge.
An interface used by GAuth::SaslServer to obtain authentication secrets.
static std::string challenge()
Returns a challenge string that incorporates a random number and the current time.
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 id(const std::string &response)
Returns the leading id part of the response.
virtual bool authenticated() const override
Override from GAuth::SaslServer.
G::StringArray wildcards() const
Returns an ordered list of wildcard strings that match this address.
static bool enabled()
Returns true if test features are enabled.
std::string hostPartString() const
Returns a string which represents the network address for debugging and diagnostics purposes...
A private pimple-pattern implementation class used by GAuth::SaslServerBasic.
static std::string upper(const std::string &s)
Returns a copy of 's' in which all Latin-1 lowercase characters have been replaced by uppercase chara...
virtual bool trusted(const GNet::Address &) const override
Override from GAuth::SaslServer.
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.
virtual bool init(const std::string &mechanism) override
Override from GAuth::SaslServer.
virtual std::string initialChallenge() const override
Override from GAuth::SaslServer.
SaslServerBasic(const SaslServerSecrets &, bool allow_apop)
Constructor.
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...
virtual std::string id() const override
Override from GAuth::SaslServer.