Skip to content
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

♻️ Extract SASL::Authenticators#normalize_name #309

Merged
merged 1 commit into from
Jul 14, 2024
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
2 changes: 1 addition & 1 deletion lib/net/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ def starttls(**options)
def authenticate(mechanism, *creds,
sasl_ir: config.sasl_ir,
**props, &callback)
mechanism = mechanism.to_s.tr("_", "-").upcase
mechanism = SASL::Authenticators.normalize_name(mechanism)
authenticator = SASL.authenticator(mechanism, *creds, **props, &callback)
cmdargs = ["AUTHENTICATE", mechanism]
if sasl_ir && capable?("SASL-IR") && auth_capable?(mechanism) &&
Expand Down
2 changes: 1 addition & 1 deletion lib/net/imap/sasl/authentication_exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def self.build(client, mechanism, *args, sasl_ir: true, **kwargs, &block)

def initialize(client, mechanism, authenticator, sasl_ir: true)
@client = client
@mechanism = -mechanism.to_s.upcase.tr(?_, ?-)
@mechanism = Authenticators.normalize_name(mechanism)
@authenticator = authenticator
@sasl_ir = sasl_ir
@processed = false
Expand Down
12 changes: 8 additions & 4 deletions lib/net/imap/sasl/authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module Net::IMAP::SASL
# ScramSHA1Authenticator for examples.
class Authenticators

# Normalize the mechanism name as an uppercase string, with underscores
# converted to dashes.
def self.normalize_name(mechanism) -(mechanism.to_s.upcase.tr(?_, ?-)) end

# Create a new Authenticators registry.
#
# This class is usually not instantiated directly. Use SASL.authenticators
Expand Down Expand Up @@ -65,7 +69,6 @@ def names; @authenticators.keys end
# lazily loaded from <tt>Net::IMAP::SASL::#{name}Authenticator</tt> (case is
# preserved and non-alphanumeric characters are removed..
def add_authenticator(name, authenticator = nil)
key = -name.to_s.upcase.tr(?_, ?-)
authenticator ||= begin
class_name = "#{name.gsub(/[^a-zA-Z0-9]/, "")}Authenticator".to_sym
auth_class = nil
Expand All @@ -74,17 +77,18 @@ def add_authenticator(name, authenticator = nil)
auth_class.new(*creds, **props, &block)
}
end
key = Authenticators.normalize_name(name)
@authenticators[key] = authenticator
end

# Removes the authenticator registered for +name+
def remove_authenticator(name)
key = -name.to_s.upcase.tr(?_, ?-)
key = Authenticators.normalize_name(name)
@authenticators.delete(key)
end

def mechanism?(name)
key = -name.to_s.upcase.tr(?_, ?-)
key = Authenticators.normalize_name(name)
@authenticators.key?(key)
end

Expand All @@ -105,7 +109,7 @@ def mechanism?(name)
# only. Protocol client users should see refer to their client's
# documentation, e.g. Net::IMAP#authenticate.
def authenticator(mechanism, ...)
key = -mechanism.to_s.upcase.tr(?_, ?-)
key = Authenticators.normalize_name(mechanism)
auth = @authenticators.fetch(key) do
raise ArgumentError, 'unknown auth type - "%s"' % key
end
Expand Down
7 changes: 7 additions & 0 deletions test/net/imap/test_imap_authenticators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

class IMAPAuthenticatorsTest < Test::Unit::TestCase

test "SASL::Authenticators.normalize_name" do
authenticators = Net::IMAP::SASL::Authenticators
assert_equal "FOO-BAR-BAZ", authenticators.normalize_name(:foo_bar_baz)
assert_equal "SCRAM-SHA1-PLUS", authenticators.normalize_name(:scram_sha1_plus)
assert_equal "PLAIN", authenticators.normalize_name("pLAin")
end

def test_net_imap_authenticator_deprecated
assert_warn(/Net::IMAP\.authenticator .+deprecated./) do
Net::IMAP.authenticator("PLAIN", "user", "pass")
Expand Down