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

Delegate checking auth args to the authenticator #73

Merged
merged 1 commit into from
Nov 4, 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
24 changes: 7 additions & 17 deletions lib/net/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,8 @@ 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
if user || secret || authtype
check_auth_args authtype, user, secret
end
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
tcp_socket(@address, @port)
Expand Down Expand Up @@ -832,27 +831,18 @@ 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)
type ||= DEFAULT_AUTH_TYPE
klass = Authenticator.auth_class(type) or
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
klass.check_args(*args, **kwargs)
end

#
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 @@ -15,6 +15,15 @@ def self.auth_class(type)
Authenticator.auth_classes[type]
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