Skip to content

Commit 65e5cd6

Browse files
committed
♻️ Make SASL.authenticator case insensitive
Also allow symbols for both add_authenticator and authenticator.
1 parent cb54c37 commit 65e5cd6

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/net/imap/sasl/authenticators.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class Authenticators
3333
def initialize(use_defaults: false)
3434
@authenticators = {}
3535
if use_defaults
36-
add_authenticator "PLAIN", PlainAuthenticator
37-
add_authenticator "XOAUTH2", XOAuth2Authenticator
38-
add_authenticator "LOGIN", LoginAuthenticator # deprecated
39-
add_authenticator "CRAM-MD5", CramMD5Authenticator # deprecated
40-
add_authenticator "DIGEST-MD5", DigestMD5Authenticator # deprecated
36+
add_authenticator "Plain", PlainAuthenticator
37+
add_authenticator "XOAuth2", XOAuth2Authenticator
38+
add_authenticator "Login", LoginAuthenticator # deprecated
39+
add_authenticator "Cram-MD5", CramMD5Authenticator # deprecated
40+
add_authenticator "Digest-MD5", DigestMD5Authenticator # deprecated
4141
end
4242
end
4343

@@ -60,8 +60,9 @@ def names; @authenticators.keys end
6060
# When only a single argument is given, the authenticator class will be
6161
# lazily loaded from <tt>Net::IMAP::SASL::#{name}Authenticator</tt> (case is
6262
# preserved and non-alphanumeric characters are removed..
63-
def add_authenticator(auth_type, authenticator)
64-
@authenticators[auth_type] = authenticator
63+
def add_authenticator(name, authenticator)
64+
key = name.upcase.to_sym
65+
@authenticators[key] = authenticator
6566
end
6667

6768
# :call-seq:
@@ -81,7 +82,7 @@ def add_authenticator(auth_type, authenticator)
8182
# only. Protocol client users should see refer to their client's
8283
# documentation, e.g. Net::IMAP#authenticate.
8384
def authenticator(mechanism, ...)
84-
auth = @authenticators.fetch(mechanism.upcase) do
85+
auth = @authenticators.fetch(mechanism.upcase.to_sym) do
8586
raise ArgumentError, 'unknown auth type - "%s"' % mechanism
8687
end
8788
auth.respond_to?(:new) ? auth.new(...) : auth.call(...)

test/net/imap/test_imap_authenticators.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ def test_net_imap_authenticator_deprecated
1111
end
1212
end
1313

14+
test ".authenticator mechanism name is case insensitive" do
15+
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
16+
Net::IMAP::SASL.authenticator("PLAIN", "user", "pass"))
17+
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
18+
Net::IMAP::SASL.authenticator("plain", "user", "pass"))
19+
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
20+
Net::IMAP::SASL.authenticator("pLaIn", "user", "pass"))
21+
end
22+
23+
test ".authenticator mechanism name can be a symbol" do
24+
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
25+
Net::IMAP::SASL.authenticator(:PLAIN, "user", "pass"))
26+
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
27+
Net::IMAP::SASL.authenticator(:plain, "user", "pass"))
28+
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
29+
Net::IMAP::SASL.authenticator(:pLaIN, "user", "pass"))
30+
end
31+
1432
# ----------------------
1533
# PLAIN
1634
# ----------------------

0 commit comments

Comments
 (0)