Skip to content

♻️ Make SASL.authenticator case insensitive #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2023
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: 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