From 00b56e2234ba16b4ad4df500ed546cd64ecc701b Mon Sep 17 00:00:00 2001 From: nick evans Date: Thu, 28 Sep 2023 09:51:43 -0400 Subject: [PATCH] Remove obsolete Authenticator implementations The versions from `net-imap` are more full-featured. E.g: they support `authzid` and print deprecation warnings for MD5 authenticators. The tests are updated to silence deprecation warnings, which are printed by the authenticators from `net-imap`. --- lib/net/smtp/auth_cram_md5.rb | 53 ----------------------------------- lib/net/smtp/auth_login.rb | 16 ----------- lib/net/smtp/auth_plain.rb | 14 --------- test/net/smtp/test_smtp.rb | 35 +++++++++++++++-------- 4 files changed, 24 insertions(+), 94 deletions(-) delete mode 100644 lib/net/smtp/auth_cram_md5.rb delete mode 100644 lib/net/smtp/auth_login.rb delete mode 100644 lib/net/smtp/auth_plain.rb diff --git a/lib/net/smtp/auth_cram_md5.rb b/lib/net/smtp/auth_cram_md5.rb deleted file mode 100644 index 50994ee..0000000 --- a/lib/net/smtp/auth_cram_md5.rb +++ /dev/null @@ -1,53 +0,0 @@ -unless defined? OpenSSL - begin - require 'digest/md5' - rescue LoadError - end -end - -class Net::SMTP - class AuthCramMD5 < Net::SMTP::Authenticator - auth_type :cram_md5 - - def auth(user_arg = nil, secret_arg = nil, - authcid: nil, username: nil, user: nil, - secret: nil, password: nil, - **) - user = req_param authcid, username, user, user_arg, "username (authcid)" - secret = req_param password, secret, secret_arg, "secret (password)" - challenge = continue('AUTH CRAM-MD5') - crammed = cram_md5_response(secret, challenge.unpack1('m')) - finish(base64_encode("#{user} #{crammed}")) - end - - IMASK = 0x36 - OMASK = 0x5c - - # CRAM-MD5: [RFC2195] - def cram_md5_response(secret, challenge) - tmp = digest_class::MD5.digest(cram_secret(secret, IMASK) + challenge) - digest_class::MD5.hexdigest(cram_secret(secret, OMASK) + tmp) - end - - CRAM_BUFSIZE = 64 - - def cram_secret(secret, mask) - secret = digest_class::MD5.digest(secret) if secret.size > CRAM_BUFSIZE - buf = secret.ljust(CRAM_BUFSIZE, "\0") - 0.upto(buf.size - 1) do |i| - buf[i] = (buf[i].ord ^ mask).chr - end - buf - end - - def digest_class - @digest_class ||= if defined?(OpenSSL::Digest) - OpenSSL::Digest - elsif defined?(::Digest) - ::Digest - else - raise '"openssl" or "digest" library is required' - end - end - end -end diff --git a/lib/net/smtp/auth_login.rb b/lib/net/smtp/auth_login.rb deleted file mode 100644 index 174ab09..0000000 --- a/lib/net/smtp/auth_login.rb +++ /dev/null @@ -1,16 +0,0 @@ -class Net::SMTP - class AuthLogin < Net::SMTP::Authenticator - auth_type :login - - def auth(user_arg = nil, secret_arg = nil, - authcid: nil, username: nil, user: nil, - secret: nil, password: nil, - **) - user = req_param authcid, username, user, user_arg, "username (authcid)" - secret = req_param password, secret, secret_arg, "secret (password)" - continue('AUTH LOGIN') - continue(base64_encode(user)) - finish(base64_encode(secret)) - end - end -end diff --git a/lib/net/smtp/auth_plain.rb b/lib/net/smtp/auth_plain.rb deleted file mode 100644 index e778bad..0000000 --- a/lib/net/smtp/auth_plain.rb +++ /dev/null @@ -1,14 +0,0 @@ -class Net::SMTP - class AuthPlain < Net::SMTP::Authenticator - auth_type :plain - - def auth(user_arg = nil, secret_arg = nil, - authcid: nil, username: nil, user: nil, - secret: nil, password: nil, - **) - user = req_param authcid, username, user, user_arg, "username (authcid)" - secret = req_param password, secret, secret_arg, "secret (password)" - finish('AUTH PLAIN ' + base64_encode("\0#{user}\0#{secret}")) - end - end -end diff --git a/test/net/smtp/test_smtp.rb b/test/net/smtp/test_smtp.rb index d2fd19a..de181ff 100644 --- a/test/net/smtp/test_smtp.rb +++ b/test/net/smtp/test_smtp.rb @@ -138,23 +138,28 @@ def test_unsucessful_auth_plain def test_auth_cram_md5 server = FakeServer.start(auth: 'CRAM-MD5') smtp = Net::SMTP.start 'localhost', server.port - assert smtp.auth(:cram_md5, "account", password: "password").success? + assert smtp.auth(:cram_md5, "account", password: "password", + warn_deprecation: false).success? end def test_auth_login server = FakeServer.start(auth: 'login') smtp = Net::SMTP.start 'localhost', server.port - assert smtp.authenticate("account", "password", :login).success? + assert smtp.auth(:login, "account", "password", + warn_deprecation: false).success? server = FakeServer.start(auth: 'login') smtp = Net::SMTP.start 'localhost', server.port - assert smtp.auth("LOGIN", username: "account", secret: "password").success? + assert smtp.auth(username: "account", secret: "password", + type: :login, warn_deprecation: false).success? end def test_unsucessful_auth_login server = FakeServer.start(auth: 'login') smtp = Net::SMTP.start 'localhost', server.port - err = assert_raise(Net::SMTPAuthenticationError) { smtp.authenticate("foo", "bar", :login) } + err = assert_raise(Net::SMTPAuthenticationError) { + smtp.auth(:login, "foo", "bar", warn_deprecation: false) + } assert_equal "535 5.7.8 Error: authentication failed: authentication failure\n", err.message assert_equal "535", err.response.status end @@ -167,7 +172,9 @@ def server.auth(*) @sock.puts "235 2.7.0 Authentication successful\r\n" end smtp = Net::SMTP.start 'localhost', server.port - err = assert_raise(Net::SMTPUnknownError) { smtp.authenticate("account", "password", :login) } + err = assert_raise(Net::SMTPUnknownError) { + smtp.auth(:login, "account", "password", warn_deprecation: false) + } assert_equal "235 2.7.0 Authentication successful\n", err.message assert_equal "235", err.response.status end @@ -502,16 +509,19 @@ def test_start_auth_plain def test_start_auth_login port = fake_server_start(auth: 'LOGIN') - Net::SMTP.start('localhost', port, user: 'account', password: 'password', authtype: :login){} + Net::SMTP.start('localhost', port, user: 'account', password: 'password', + authtype: :login, auth: {warn_deprecation: false}){} port = fake_server_start(auth: 'LOGIN') assert_raise Net::SMTPAuthenticationError do - Net::SMTP.start('localhost', port, user: 'account', password: 'invalid', authtype: :login){} + Net::SMTP.start('localhost', port, user: 'account', password: 'invalid', + authtype: :login, auth: {warn_deprecation: false}){} end port = fake_server_start(auth: 'PLAIN') assert_raise Net::SMTPAuthenticationError do - Net::SMTP.start('localhost', port, user: 'account', password: 'password', authtype: :login){} + Net::SMTP.start('localhost', port, user: 'account', password: 'password', + authtype: :login, auth: {warn_deprecation: false}){} end end @@ -519,16 +529,19 @@ def test_start_auth_cram_md5 omit "openssl or digest library not loaded" unless defined? OpenSSL or defined? Digest port = fake_server_start(auth: 'CRAM-MD5') - Net::SMTP.start('localhost', port, user: 'account', password: 'password', authtype: "CRAM-MD5"){} + Net::SMTP.start('localhost', port, user: 'account', password: 'password', + authtype: "CRAM-MD5", auth: {warn_deprecation: false}){} port = fake_server_start(auth: 'CRAM-MD5') assert_raise Net::SMTPAuthenticationError do - Net::SMTP.start('localhost', port, user: 'account', password: 'invalid', authtype: :cram_md5){} + Net::SMTP.start('localhost', port, user: 'account', password: 'invalid', + authtype: :cram_md5, auth: {warn_deprecation: false}){} end port = fake_server_start(auth: 'PLAIN') assert_raise Net::SMTPAuthenticationError do - Net::SMTP.start('localhost', port, user: 'account', password: 'password', authtype: :cram_md5){} + Net::SMTP.start('localhost', port, user: 'account', password: 'password', + authtype: :cram_md5, auth: {warn_deprecation: false}){} end end