Skip to content

Commit

Permalink
♻️ Make SASL.add_authenticator case insensitive
Browse files Browse the repository at this point in the history
Also allow symbols for both add_authenticator and authenticator.
  • Loading branch information
nevans committed Sep 9, 2023
1 parent 9c8c90f commit 14139d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/net/imap/sasl/authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class Authenticators
def initialize(use_defaults: false)
@authenticators = {}
if use_defaults
add_authenticator "PLAIN", PlainAuthenticator
add_authenticator "XOAUTH2", XOAuth2Authenticator
add_authenticator "LOGIN", LoginAuthenticator # deprecated
add_authenticator "CRAM-MD5", CramMD5Authenticator # deprecated
add_authenticator "DIGEST-MD5", DigestMD5Authenticator # deprecated
add_authenticator "Plain", PlainAuthenticator
add_authenticator "XOAuth2", XOAuth2Authenticator
add_authenticator "Login", LoginAuthenticator # deprecated
add_authenticator "Cram-MD5", CramMD5Authenticator # deprecated
add_authenticator "Digest-MD5", DigestMD5Authenticator # deprecated
end
end

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

# :call-seq:
Expand All @@ -81,7 +82,7 @@ def add_authenticator(auth_type, authenticator)
# only. Protocol client users should see refer to their client's
# documentation, e.g. Net::IMAP#authenticate.
def authenticator(mechanism, ...)
auth = @authenticators.fetch(mechanism.upcase) do
auth = @authenticators.fetch(mechanism.upcase.to_sym) do
raise ArgumentError, 'unknown auth type - "%s"' % mechanism
end
auth.respond_to?(:new) ? auth.new(...) : auth.call(...)
Expand Down
18 changes: 18 additions & 0 deletions test/net/imap/test_imap_authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ def test_net_imap_authenticator_deprecated
end
end

test ".authenticator mechanism name is case insensitive" do
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
Net::IMAP::SASL.authenticator("PLAIN", "user", "pass"))
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
Net::IMAP::SASL.authenticator("plain", "user", "pass"))
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
Net::IMAP::SASL.authenticator("pLaIn", "user", "pass"))
end

test ".authenticator mechanism name can be a symbol" do
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
Net::IMAP::SASL.authenticator(:PLAIN, "user", "pass"))
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
Net::IMAP::SASL.authenticator(:plain, "user", "pass"))
assert_kind_of(Net::IMAP::SASL::PlainAuthenticator,
Net::IMAP::SASL.authenticator(:pLaIN, "user", "pass"))
end

# ----------------------
# PLAIN
# ----------------------
Expand Down

0 comments on commit 14139d4

Please sign in to comment.