From 9c47fafec3be408ba32316145be004a991aa9048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Rosick=C3=BD?= Date: Sun, 27 Nov 2022 13:19:26 +0100 Subject: [PATCH 1/2] xoauth2 --- NEWS.md | 5 +++++ lib/net/smtp.rb | 30 ++++++++++++++++++++++++++++++ test/net/smtp/test_smtp.rb | 17 +++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/NEWS.md b/NEWS.md index 0df3239..42ab9df 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,9 @@ # NEWS +## Unreleased + +### Improvements + +* Add XOAUTH2 authenticator ## Version 0.3.3 (2022-10-29) diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index f354708..dd7219e 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -857,6 +857,20 @@ def auth_login(user, secret) res end + def auth_xoauth2(user, oauth2_token) + check_auth_args user, oauth2_token + + auth_string = build_oauth2_string(user, oauth2_token) + res = send_xoauth2(base64_encode(auth_string)) + + if res.continue? + res = get_final_status + end + + check_auth_response res + res + end + def auth_cram_md5(user, secret) check_auth_args user, secret res = critical { @@ -915,6 +929,22 @@ def cram_secret(secret, mask) buf end + def send_xoauth2(auth_token) + critical { + get_response("AUTH XOAUTH2 #{auth_token}") + } + end + + def build_oauth2_string(user, oauth2_token) + "user=%s\1auth=Bearer %s\1\1".encode("us-ascii") % [user, oauth2_token] + end + + def get_final_status + critical { + get_response("") + } + end + # # SMTP command dispatcher # diff --git a/test/net/smtp/test_smtp.rb b/test/net/smtp/test_smtp.rb index b418b3f..c2ff94d 100644 --- a/test/net/smtp/test_smtp.rb +++ b/test/net/smtp/test_smtp.rb @@ -151,6 +151,23 @@ def test_unsucessful_auth_plain assert_equal "535", err.response.status end + def test_auth_xoauth2 + sock = FakeSocket.new("235 2.7.0 Authentication successful\r\n") + smtp = Net::SMTP.new 'localhost', 25 + smtp.instance_variable_set :@socket, sock + assert smtp.auth_xoauth2("foo", "bar").success? + assert_equal "AUTH XOAUTH2 dXNlcj1mb28BYXV0aD1CZWFyZXIgYmFyAQE=\r\n", sock.write_io.string + end + + def test_unsucessful_auth_xoauth2 + sock = FakeSocket.new("535 5.7.8 BadCredentials\r\n") + smtp = Net::SMTP.new 'localhost', 25 + smtp.instance_variable_set :@socket, sock + err = assert_raise(Net::SMTPAuthenticationError) { smtp.auth_xoauth2("foo", "bar") } + assert_equal "535 5.7.8 BadCredentials\n", err.message + assert_equal "535", err.response.status + end + def test_auth_login sock = FakeSocket.new("334 VXNlcm5hbWU6\r\n334 UGFzc3dvcmQ6\r\n235 2.7.0 Authentication successful\r\n") smtp = Net::SMTP.new 'localhost', 25 From 78e2542a1023e9f060aa18b08cab7909fad49033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Rosick=C3=BD?= Date: Thu, 8 Dec 2022 16:55:30 +0100 Subject: [PATCH 2/2] simplify oauth2_string --- lib/net/smtp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index dd7219e..13ec53d 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -936,7 +936,7 @@ def send_xoauth2(auth_token) end def build_oauth2_string(user, oauth2_token) - "user=%s\1auth=Bearer %s\1\1".encode("us-ascii") % [user, oauth2_token] + format("user=%s\1auth=Bearer %s\1\1", user, oauth2_token) end def get_final_status