Skip to content

Commit 8f42562

Browse files
committed
♻️ Extract SASL::Authenticators#normalize_name [🚧 tests]
This was used in several different places, so it's better to DRY up the implementation.
1 parent 623402a commit 8f42562

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

lib/net/imap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ def starttls(**options)
13421342
def authenticate(mechanism, *creds,
13431343
sasl_ir: config.sasl_ir,
13441344
**props, &callback)
1345-
mechanism = mechanism.to_s.tr("_", "-").upcase
1345+
mechanism = SASL::Authenticators.normalize_name(mechanism)
13461346
authenticator = SASL.authenticator(mechanism, *creds, **props, &callback)
13471347
cmdargs = ["AUTHENTICATE", mechanism]
13481348
if sasl_ir && capable?("SASL-IR") && auth_capable?(mechanism) &&

lib/net/imap/sasl/authentication_exchange.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def self.build(client, mechanism, *args, sasl_ir: true, **kwargs, &block)
8181

8282
def initialize(client, mechanism, authenticator, sasl_ir: true)
8383
@client = client
84-
@mechanism = -mechanism.to_s.upcase.tr(?_, ?-)
84+
@mechanism = Authenticators.normalize_name(mechanism)
8585
@authenticator = authenticator
8686
@sasl_ir = sasl_ir
8787
@processed = false

lib/net/imap/sasl/authenticators.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ module Net::IMAP::SASL
2121
# ScramSHA1Authenticator for examples.
2222
class Authenticators
2323

24+
# Normalize the mechanism name as an uppercase string, with underscores
25+
# converted to dashes.
26+
def self.normalize_name(mechanism) -mechanism.to_s.upcase.tr(?_, ?-) end
27+
2428
# Create a new Authenticators registry.
2529
#
2630
# This class is usually not instantiated directly. Use SASL.authenticators
@@ -65,7 +69,7 @@ def names; @authenticators.keys end
6569
# lazily loaded from <tt>Net::IMAP::SASL::#{name}Authenticator</tt> (case is
6670
# preserved and non-alphanumeric characters are removed..
6771
def add_authenticator(name, authenticator = nil)
68-
key = -name.to_s.upcase.tr(?_, ?-)
72+
key = Authenticators.normalize_name(name)
6973
authenticator ||= begin
7074
class_name = "#{name.gsub(/[^a-zA-Z0-9]/, "")}Authenticator".to_sym
7175
auth_class = nil
@@ -79,12 +83,12 @@ def add_authenticator(name, authenticator = nil)
7983

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

8690
def mechanism?(name)
87-
key = -name.to_s.upcase.tr(?_, ?-)
91+
key = Authenticators.normalize_name(name)
8892
@authenticators.key?(key)
8993
end
9094

@@ -105,7 +109,7 @@ def mechanism?(name)
105109
# only. Protocol client users should see refer to their client's
106110
# documentation, e.g. Net::IMAP#authenticate.
107111
def authenticator(mechanism, ...)
108-
key = -mechanism.to_s.upcase.tr(?_, ?-)
112+
key = Authenticators.normalize_name(mechanism)
109113
auth = @authenticators.fetch(key) do
110114
raise ArgumentError, 'unknown auth type - "%s"' % key
111115
end

0 commit comments

Comments
 (0)