SMTPClientSession: added support for XOAUTH2 authentication

This commit is contained in:
Guenter Obiltschnig 2015-12-14 22:29:11 +01:00
parent 910e2f3f29
commit 899e5084e9
2 changed files with 25 additions and 1 deletions

View File

@ -52,7 +52,8 @@ public:
AUTH_CRAM_MD5,
AUTH_CRAM_SHA1,
AUTH_LOGIN,
AUTH_PLAIN
AUTH_PLAIN,
AUTH_XOAUTH2
};
explicit SMTPClientSession(const StreamSocket& socket);
@ -184,6 +185,7 @@ protected:
void loginUsingCRAM(const std::string& username, const std::string& method, Poco::DigestEngine& hmac);
void loginUsingLogin(const std::string& username, const std::string& password);
void loginUsingPlain(const std::string& username, const std::string& password);
void loginUsingXOAUTH2(const std::string& username, const std::string& password);
DialogSocket& socket();
private:

View File

@ -206,6 +206,7 @@ void SMTPClientSession::loginUsingLogin(const std::string& username, const std::
}
}
void SMTPClientSession::loginUsingPlain(const std::string& username, const std::string& password)
{
std::ostringstream credentialsBase64;
@ -219,6 +220,19 @@ void SMTPClientSession::loginUsingPlain(const std::string& username, const std::
}
void SMTPClientSession::loginUsingXOAUTH2(const std::string& username, const std::string& password)
{
std::ostringstream credentialsBase64;
Base64Encoder credentialsEncoder(credentialsBase64);
credentialsEncoder << "user=" << username << "\001auth=Bearer " << password << "\001\001";
credentialsEncoder.close();
std::string response;
int status = sendCommand("AUTH XOAUTH2", credentialsBase64.str(), response);
if (!isPositiveCompletion(status)) throw SMTPException("Login using XOAUTH2 failed", response, status);
}
void SMTPClientSession::login(LoginMethod loginMethod, const std::string& username, const std::string& password)
{
login(Environment::nodeName(), loginMethod, username, password);
@ -262,6 +276,14 @@ void SMTPClientSession::login(const std::string& hostname, LoginMethod loginMeth
}
else throw SMTPException("The mail service does not support PLAIN authentication", response);
}
else if (loginMethod == AUTH_XOAUTH2)
{
if (response.find("XOAUTH2", 0) != std::string::npos)
{
loginUsingXOAUTH2(username, password);
}
else throw SMTPException("The mail service does not support XOAUTH2 authentication", response);
}
else if (loginMethod != AUTH_NONE)
{
throw SMTPException("The autentication method is not supported");