From 802278180097d0f3fdb4c6d16133536c41057081 Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Mon, 14 Dec 2015 22:29:11 +0100 Subject: [PATCH] SMTPClientSession: added support for XOAUTH2 authentication --- Net/include/Poco/Net/SMTPClientSession.h | 4 +++- Net/src/SMTPClientSession.cpp | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Net/include/Poco/Net/SMTPClientSession.h b/Net/include/Poco/Net/SMTPClientSession.h index 7258ad632..556103218 100644 --- a/Net/include/Poco/Net/SMTPClientSession.h +++ b/Net/include/Poco/Net/SMTPClientSession.h @@ -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: diff --git a/Net/src/SMTPClientSession.cpp b/Net/src/SMTPClientSession.cpp index 16febfde2..8399ac7de 100644 --- a/Net/src/SMTPClientSession.cpp +++ b/Net/src/SMTPClientSession.cpp @@ -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");