Skip to content

Commit c02ce79

Browse files
committed
Delegate checking auth args to the authenticator
Each authenticator has different parameters, so argument validation must be delegated to the authenticator classes.
1 parent c41e299 commit c02ce79

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

lib/net/smtp.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,8 @@ def tcp_socket(address, port)
633633

634634
def do_start(helo_domain, user, secret, authtype)
635635
raise IOError, 'SMTP session already started' if @started
636-
if user or secret
637-
check_auth_method(authtype || DEFAULT_AUTH_TYPE)
638-
check_auth_args user, secret
636+
if user || secret || authtype
637+
check_auth_args authtype, user, secret
639638
end
640639
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
641640
tcp_socket(@address, @port)
@@ -832,27 +831,18 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
832831
DEFAULT_AUTH_TYPE = :plain
833832

834833
def authenticate(user, secret, authtype = DEFAULT_AUTH_TYPE)
835-
check_auth_method authtype
836-
check_auth_args user, secret
834+
check_auth_args authtype, user, secret
837835
authenticator = Authenticator.auth_class(authtype).new(self)
838836
authenticator.auth(user, secret)
839837
end
840838

841839
private
842840

843-
def check_auth_method(type)
844-
unless Authenticator.auth_class(type)
841+
def check_auth_args(type, *args, **kwargs)
842+
type ||= DEFAULT_AUTH_TYPE
843+
klass = Authenticator.auth_class(type) or
845844
raise ArgumentError, "wrong authentication type #{type}"
846-
end
847-
end
848-
849-
def check_auth_args(user, secret, authtype = DEFAULT_AUTH_TYPE)
850-
unless user
851-
raise ArgumentError, 'SMTP-AUTH requested but missing user name'
852-
end
853-
unless secret
854-
raise ArgumentError, 'SMTP-AUTH requested but missing secret phrase'
855-
end
845+
klass.check_args(*args, **kwargs)
856846
end
857847

858848
#

lib/net/smtp/authenticator.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def self.auth_class(type)
1515
Authenticator.auth_classes[type]
1616
end
1717

18+
def self.check_args(user_arg = nil, secret_arg = nil, *, **)
19+
unless user_arg
20+
raise ArgumentError, 'SMTP-AUTH requested but missing user name'
21+
end
22+
unless secret_arg
23+
raise ArgumentError, 'SMTP-AUTH requested but missing secret phrase'
24+
end
25+
end
26+
1827
attr_reader :smtp
1928

2029
def initialize(smtp)

0 commit comments

Comments
 (0)