Skip to content

Commit

Permalink
Do not require positional args for #authenticate
Browse files Browse the repository at this point in the history
This API is a little bit confusing, IMO.  But it does preserve backward
compatibility, while allowing authenticators that don't allow positional
parameters to work without crashing.  But, authenticators that require
only one parameter—or more than three—will still be inaccessible.
  • Loading branch information
nevans committed Oct 15, 2023
1 parent 9e53015 commit e4e64e5
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/net/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
DEFAULT_AUTH_TYPE = :plain

# call-seq:
# authenticate(type = DEFAULT_AUTH_TYPE, **, &)
# authenticate(username, secret, type = DEFAULT_AUTH_TYPE, **, &)
#
# Authenticates with the server, using the "AUTH" command.
Expand All @@ -873,10 +874,17 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
# All arguments—other than +type+—are forwarded to the authenticator.
# Different authenticators may interpret the +username+ and +secret+
# arguments differently.
def authenticate(user, secret, authtype = DEFAULT_AUTH_TYPE, **kwargs, &block)
check_auth_args authtype, user, secret, **kwargs
def authenticate(*args, **kwargs, &block)
case args.length
when 1, 3 then authtype = args.pop
when (4..)
raise ArgumentError, "wrong number of arguments " \
"(given %d, expected 0..3)" % [args.length]
end
authtype ||= DEFAULT_AUTH_TYPE
check_auth_args authtype, *args, **kwargs
authenticator = Authenticator.auth_class(authtype).new(self)
critical { authenticator.auth(user, secret, **kwargs, &block) }
critical { authenticator.auth(*args, **kwargs, &block) }
end

private
Expand Down

0 comments on commit e4e64e5

Please sign in to comment.