Skip to content

Commit

Permalink
Delegate checking auth args to the authenticator
Browse files Browse the repository at this point in the history
Each authenticator has different parameters, so argument validation must
be delegated to the authenticator classes.
  • Loading branch information
nevans committed Oct 14, 2023
1 parent f044f30 commit 36d91ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
25 changes: 8 additions & 17 deletions lib/net/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,7 @@ def tcp_socket(address, port)

def do_start(helo_domain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
if user or secret
check_auth_method(authtype || DEFAULT_AUTH_TYPE)
check_auth_args user, secret
end
check_auth_args authtype, user, secret
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
tcp_socket(@address, @port)
end
Expand Down Expand Up @@ -832,29 +829,23 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
DEFAULT_AUTH_TYPE = :plain

def authenticate(user, secret, authtype = DEFAULT_AUTH_TYPE)
check_auth_method authtype
check_auth_args user, secret
check_auth_args authtype, user, secret
authenticator = Authenticator.auth_class(authtype).new(self)
authenticator.auth(user, secret)
end

private

def check_auth_method(type)
unless Authenticator.auth_class(type)
def check_auth_args(type, *args, **kwargs)
return unless type || args.any? || kwargs.any?
type ||= DEFAULT_AUTH_TYPE
if (klass = Authenticator.auth_class(type))
klass.check_args(*args, **kwargs)
else
raise ArgumentError, "wrong authentication type #{type}"
end
end

def check_auth_args(user, secret, authtype = DEFAULT_AUTH_TYPE)
unless user
raise ArgumentError, 'SMTP-AUTH requested but missing user name'
end
unless secret
raise ArgumentError, 'SMTP-AUTH requested but missing secret phrase'
end
end

#
# SMTP command dispatcher
#
Expand Down
9 changes: 9 additions & 0 deletions lib/net/smtp/authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def self.auth_class(type)
Authenticator.auth_classes[type.intern]
end

def self.check_args(user_arg = nil, secret_arg = nil, *, **)
unless user_arg
raise ArgumentError, 'SMTP-AUTH requested but missing user name'
end
unless secret_arg
raise ArgumentError, 'SMTP-AUTH requested but missing secret phrase'
end
end

attr_reader :smtp

def initialize(smtp)
Expand Down

0 comments on commit 36d91ca

Please sign in to comment.