Skip to content

Commit d264b33

Browse files
committed
🚚 Move and rename SASL authenticators
The original constants for the `PLAIN` and `XOAUTH2` authenticators are now deprecated aliases. The constants for deprecated mechanisms were simply moved, with no regard for backward compatibility.
1 parent 4520aa8 commit d264b33

10 files changed

+41
-24
lines changed

lib/net/imap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ def starttls(options = {}, verify = true)
10151015
# +PLAIN+:: See PlainAuthenticator.
10161016
# Login using clear-text username and password.
10171017
#
1018-
# +XOAUTH2+:: See XOauth2Authenticator.
1018+
# +XOAUTH2+:: See XOAuth2Authenticator.
10191019
# Login using a username and OAuth2 access token.
10201020
# Non-standard and obsoleted by +OAUTHBEARER+, but widely
10211021
# supported.

lib/net/imap/authenticators.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Net::IMAP::Authenticators
1212
# The authenticator session must respond to +#process+, receiving the server's
1313
# challenge and returning the client's response.
1414
#
15-
# See PlainAuthenticator, XOauth2Authenticator, and DigestMD5Authenticator for
15+
# See PlainAuthenticator, XOAuth2Authenticator, and DigestMD5Authenticator for
1616
# examples.
1717
def add_authenticator(auth_type, authenticator)
1818
authenticators[auth_type] = authenticator
@@ -54,11 +54,20 @@ def authenticators
5454

5555
end
5656

57-
Net::IMAP.extend Net::IMAP::Authenticators
57+
class Net::IMAP
58+
extend Authenticators
59+
add_authenticator "PLAIN", SASL::PlainAuthenticator
60+
add_authenticator "XOAUTH2", SASL::XOAuth2Authenticator
5861

59-
require_relative "authenticators/plain"
62+
add_authenticator "CRAM-MD5", SASL::CramMD5Authenticator # deprecated
63+
add_authenticator "LOGIN", SASL::LoginAuthenticator # deprecated
64+
add_authenticator "DIGEST-MD5", SASL::DigestMD5Authenticator # deprecated
65+
end
66+
67+
class Net::IMAP
68+
PlainAuthenticator = SASL::PlainAuthenticator # :nodoc:
69+
deprecate_constant :PlainAuthenticator
6070

61-
require_relative "authenticators/login"
62-
require_relative "authenticators/cram_md5"
63-
require_relative "authenticators/digest_md5"
64-
require_relative "authenticators/xoauth2"
71+
XOauth2Authenticator = SASL::XOAuth2Authenticator # :nodoc:
72+
deprecate_constant :XOauth2Authenticator
73+
end

lib/net/imap/sasl.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ module SASL
3232
autoload :ProhibitedCodepoint, sasl_stringprep_rb
3333
autoload :BidiStringError, sasl_stringprep_rb
3434

35+
sasl_dir = File.expand_path("sasl", __dir__)
36+
autoload :PlainAuthenticator, "#{sasl_dir}/plain_authenticator"
37+
autoload :XOAuth2Authenticator, "#{sasl_dir}/xoauth2_authenticator"
38+
39+
autoload :CramMD5Authenticator, "#{sasl_dir}/cram_md5_authenticator"
40+
autoload :DigestMD5Authenticator, "#{sasl_dir}/digest_md5_authenticator"
41+
autoload :LoginAuthenticator, "#{sasl_dir}/login_authenticator"
42+
3543
module_function
3644

3745
# See Net::IMAP::StringPrep::SASLprep#saslprep.

lib/net/imap/authenticators/cram_md5.rb renamed to lib/net/imap/sasl/cram_md5_authenticator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# Additionally, RFC8314[https://tools.ietf.org/html/rfc8314] discourage the use
1414
# of cleartext and recommends TLS version 1.2 or greater be used for all
1515
# traffic. With TLS +CRAM-MD5+ is okay, but so is +PLAIN+
16-
class Net::IMAP::CramMD5Authenticator
16+
class Net::IMAP::SASL::CramMD5Authenticator
1717
def process(challenge)
1818
digest = hmac_md5(challenge, @password)
1919
return @user + " " + digest
@@ -47,5 +47,4 @@ def hmac_md5(text, key)
4747
return Digest::MD5.hexdigest(k_opad + digest)
4848
end
4949

50-
Net::IMAP.add_authenticator "CRAM-MD5", self
5150
end

lib/net/imap/authenticators/digest_md5.rb renamed to lib/net/imap/sasl/digest_md5_authenticator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# "+DIGEST-MD5+" has been deprecated by
99
# {RFC6331}[https://tools.ietf.org/html/rfc6331] and should not be relied on for
1010
# security. It is included for compatibility with existing servers.
11-
class Net::IMAP::DigestMD5Authenticator
11+
class Net::IMAP::SASL::DigestMD5Authenticator
1212
def process(challenge)
1313
case @stage
1414
when STAGE_ONE
@@ -111,5 +111,4 @@ def qdval(k, v)
111111
end
112112
end
113113

114-
Net::IMAP.add_authenticator "DIGEST-MD5", self
115114
end

lib/net/imap/authenticators/login.rb renamed to lib/net/imap/sasl/login_authenticator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# compatibility with existing servers. See
1818
# {draft-murchison-sasl-login}[https://www.iana.org/go/draft-murchison-sasl-login]
1919
# for both specification and deprecation.
20-
class Net::IMAP::LoginAuthenticator
20+
class Net::IMAP::SASL::LoginAuthenticator
2121
def process(data)
2222
case @state
2323
when STATE_USER
@@ -42,5 +42,4 @@ def initialize(user, password, warn_deprecation: true, **_ignored)
4242
@state = STATE_USER
4343
end
4444

45-
Net::IMAP.add_authenticator "LOGIN", self
4645
end

lib/net/imap/authenticators/plain.rb renamed to lib/net/imap/sasl/plain_authenticator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# RFC8314[https://tools.ietf.org/html/rfc8314] recommends TLS version 1.2 or
1010
# greater be used for all traffic, and deprecate cleartext access ASAP. +PLAIN+
1111
# can be secured by TLS encryption.
12-
class Net::IMAP::PlainAuthenticator
12+
class Net::IMAP::SASL::PlainAuthenticator
1313

1414
def initial_response?; true end
1515

@@ -39,5 +39,4 @@ def initialize(username, password, authzid: nil)
3939
@authzid = authzid
4040
end
4141

42-
Net::IMAP.add_authenticator "PLAIN", self
4342
end

lib/net/imap/authenticators/xoauth2.rb renamed to lib/net/imap/sasl/xoauth2_authenticator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class Net::IMAP::XOauth2Authenticator
3+
class Net::IMAP::SASL::XOAuth2Authenticator
44

55
def initial_response?; true end
66

@@ -19,5 +19,4 @@ def build_oauth2_string(user, oauth2_token)
1919
format("user=%s\1auth=Bearer %s\1\1", user, oauth2_token)
2020
end
2121

22-
Net::IMAP.add_authenticator 'XOAUTH2', self
2322
end

test/net/imap/test_imap_authenticators.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class IMAPAuthenticatorsTest < Test::Unit::TestCase
1212
def plain(...) Net::IMAP.authenticator("PLAIN", ...) end
1313

1414
def test_plain_authenticator_matches_mechanism
15-
assert_kind_of(Net::IMAP::PlainAuthenticator, plain("user", "pass"))
15+
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator, plain("user", "pass"))
1616
end
1717

1818
def test_plain_supports_initial_response
@@ -39,7 +39,7 @@ def test_plain_no_null_chars
3939
def xoauth2(...) Net::IMAP.authenticator("XOAUTH2", ...) end
4040

4141
def test_xoauth2_authenticator_matches_mechanism
42-
assert_kind_of(Net::IMAP::XOauth2Authenticator, xoauth2("user", "pass"))
42+
assert_kind_of(Net::IMAP::SASL::XOAuth2Authenticator, xoauth2("user", "tok"))
4343
end
4444

4545
def test_xoauth2
@@ -65,7 +65,7 @@ def login(*args, warn_deprecation: false, **kwargs, &block)
6565
end
6666

6767
def test_login_authenticator_matches_mechanism
68-
assert_kind_of(Net::IMAP::LoginAuthenticator, login("n", "p"))
68+
assert_kind_of(Net::IMAP::SASL::LoginAuthenticator, login("n", "p"))
6969
end
7070

7171
def test_login_does_not_support_initial_response
@@ -95,7 +95,7 @@ def cram_md5(*args, warn_deprecation: false, **kwargs, &block)
9595
end
9696

9797
def test_cram_md5_authenticator_matches_mechanism
98-
assert_kind_of(Net::IMAP::CramMD5Authenticator, cram_md5("n", "p"))
98+
assert_kind_of(Net::IMAP::SASL::CramMD5Authenticator, cram_md5("n", "p"))
9999
end
100100

101101
def test_cram_md5_does_not_support_initial_response
@@ -125,7 +125,8 @@ def digest_md5(*args, warn_deprecation: false, **kwargs, &block)
125125
end
126126

127127
def test_digest_md5_authenticator_matches_mechanism
128-
assert_kind_of(Net::IMAP::DigestMD5Authenticator, digest_md5("n", "p", "z"))
128+
assert_kind_of(Net::IMAP::SASL::DigestMD5Authenticator,
129+
digest_md5("n", "p", "z"))
129130
end
130131

131132
def test_digest_md5_authenticator_deprecated

test/net/imap/test_regexps.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ class IMAPRegexpsTest < Test::Unit::TestCase
2525
RegexpCollector.new(
2626
Net::IMAP,
2727
exclude_map: {
28-
Net::IMAP => %i[BodyTypeAttachment BodyTypeExtension], # deprecated
28+
Net::IMAP => %i[
29+
BodyTypeAttachment BodyTypeExtension
30+
PlainAuthenticator
31+
XOauth2Authenticator
32+
], # deprecated
2933
},
3034
).to_h
3135
)

0 commit comments

Comments
 (0)