Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/net/smtp/auth_xoauth2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Net::SMTP
class AuthXoauth2 < Net::SMTP::Authenticator
auth_type :xoauth2

def auth(user, secret)
token = xoauth2_string(user, secret)

finish("AUTH XOAUTH2 #{base64_encode(token)}")
end

private

def xoauth2_string(user, secret)
"user=#{user}\1auth=Bearer #{secret}\1\1"
end
end
end
17 changes: 17 additions & 0 deletions test/net/smtp/test_smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ def server.auth(*)
assert_equal "235", err.response.status
end

def test_auth_xoauth2
server = FakeServer.start(auth: 'xoauth2')
smtp = Net::SMTP.start 'localhost', server.port
assert smtp.authenticate("account", "token", :xoauth2).success?
assert_equal "AUTH XOAUTH2 dXNlcj1hY2NvdW50AWF1dGg9QmVhcmVyIHRva2VuAQE=\r\n", server.commands.last
end

def test_unsucessful_auth_xoauth2
server = FakeServer.start(auth: 'xoauth2')
smtp = Net::SMTP.start 'localhost', server.port
err = assert_raise(Net::SMTPAuthenticationError) { smtp.authenticate("account", "password", :xoauth2) }
assert_equal "535 5.7.8 Error: authentication failed: authentication failure\n", err.message
assert_equal "535", err.response.status
end

def test_send_message
port = fake_server_start
smtp = Net::SMTP.start 'localhost', port
Expand Down Expand Up @@ -712,6 +727,8 @@ def auth(*args)
@sock.puts "334 PDEyMzQ1Njc4OTAuMTIzNDVAc2VydmVybmFtZT4=\r\n"
r = @sock.gets&.chomp
r == 'YWNjb3VudCAyYzBjMTgxZjkxOGU2ZGM5Mjg3Zjk3N2E1ODhiMzg1YQ=='
when 'XOAUTH2'
arg == 'dXNlcj1hY2NvdW50AWF1dGg9QmVhcmVyIHRva2VuAQE='
end
if result
@sock.puts "235 2.7.0 Authentication successful\r\n"
Expand Down