Skip to content

Commit

Permalink
Add auth keyword arg to start methods
Browse files Browse the repository at this point in the history
This adds a new `auth` keyword param to `Net::SMTP.start` and `#start`
that can be used to pass any arbitrary keyword parameters to
`#authenticate`.  The pre-existing `username`, `secret`, etc keyword
params will retain their existing behavior as positional arguments to
`#authenticate`.
  • Loading branch information
nevans committed Nov 7, 2023
1 parent 196552b commit f8e201f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lib/net/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ def debug_output=(arg)

#
# :call-seq:
# start(address, port = nil, helo: 'localhost', auth: nil, tls: false, starttls: :auto, tls_verify: true, tls_hostname: nil, ssl_context_params: nil) { |smtp| ... }
# start(address, port = nil, helo: 'localhost', username: nil, secret: nil, authtype: nil, tls: false, starttls: :auto, tls_verify: true, tls_hostname: nil, ssl_context_params: nil) { |smtp| ... }
# start(address, port = nil, helo = 'localhost', username = nil, secret = nil, authtype = nil) { |smtp| ... }
#
Expand Down Expand Up @@ -521,6 +522,8 @@ def debug_output=(arg)
# These will be sent to #authenticate as positional arguments—the exact
# semantics are dependent on the +authtype+.
#
# +auth+ is a hash of arbitrary keyword arguments for #authenticate.
#
# See the discussion of Net::SMTP@SMTP+Authentication in the overview notes.
#
# === Errors
Expand All @@ -538,7 +541,7 @@ def debug_output=(arg)
#
def SMTP.start(address, port = nil, *args, helo: nil,
user: nil, secret: nil, password: nil, authtype: nil,
username: nil,
username: nil, auth: nil,
tls: false, starttls: :auto,
tls_verify: true, tls_hostname: nil, ssl_context_params: nil,
&block)
Expand All @@ -547,7 +550,8 @@ def SMTP.start(address, port = nil, *args, helo: nil,
username ||= user || args[1]
secret ||= password || args[2]
authtype ||= args[3]
new(address, port, tls: tls, starttls: starttls, tls_verify: tls_verify, tls_hostname: tls_hostname, ssl_context_params: ssl_context_params).start(helo: helo, username: username, secret: secret, authtype: authtype, &block)
new(address, port, tls: tls, starttls: starttls, tls_verify: tls_verify, tls_hostname: tls_hostname, ssl_context_params: ssl_context_params)
.start(helo: helo, username: username, secret: secret, authtype: authtype, auth: auth, &block)
end

# +true+ if the \SMTP session has been started.
Expand All @@ -559,6 +563,7 @@ def started?
# :call-seq:
# start(helo: 'localhost', username: nil, secret: nil, authtype: nil) { |smtp| ... }
# start(helo = 'localhost', username = nil, secret = nil, authtype = nil) { |smtp| ... }
# start(helo = 'localhost', auth: {type: nil, **auth_kwargs}) { |smtp| ... }
#
# Opens a TCP connection and starts the SMTP session.
#
Expand All @@ -579,6 +584,8 @@ def started?
# These will be sent to #authenticate as positional arguments—the exact
# semantics are dependent on the +authtype+.
#
# +auth+ is an optional hash of keyword arguments for #authenticate.
#
# See the discussion of Net::SMTP@SMTP+Authentication in the overview notes.
#
# See also: Net::SMTP.start
Expand Down Expand Up @@ -620,13 +627,15 @@ def started?
# * Net::ReadTimeout
# * IOError
#
def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil,
username: nil)
def start(*args, helo: nil,
user: nil, username: nil, secret: nil, password: nil,
authtype: nil, auth: nil)
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..4)" if args.size > 4
helo ||= args[0] || 'localhost'
username ||= user || args[1]
secret ||= password || args[2]
authtype ||= args[3]
auth ||= {}
if defined?(OpenSSL::VERSION)
ssl_context_params = @ssl_context_params || {}
unless ssl_context_params.has_key?(:verify_mode)
Expand All @@ -641,13 +650,13 @@ def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil
end
if block_given?
begin
do_start helo, username, secret, authtype
do_start helo, username, secret, authtype, **auth
return yield(self)
ensure
do_finish
end
else
do_start helo, username, secret, authtype
do_start helo, username, secret, authtype, **auth
return self
end
end
Expand All @@ -665,10 +674,10 @@ def tcp_socket(address, port)
TCPSocket.open address, port
end

def do_start(helo_domain, user, secret, authtype)
def do_start(helo_domain, user, secret, authtype, **auth)
raise IOError, 'SMTP session already started' if @started
if user || secret || authtype
check_auth_args authtype, user, secret
if user || secret || authtype || auth.any?
check_auth_args(authtype, user, secret, **auth)
end
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
tcp_socket(@address, @port)
Expand All @@ -686,7 +695,11 @@ def do_start(helo_domain, user, secret, authtype)
# helo response may be different after STARTTLS
do_helo helo_domain
end
authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
if user or secret
authenticate(user, secret, authtype, **auth)
elsif authtype or auth.any?
authenticate(authtype, **auth)
end
@started = true
ensure
unless @started
Expand Down
9 changes: 9 additions & 0 deletions test/net/smtp/test_smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,15 @@ def test_start_auth_plain
port = fake_server_start(auth: 'plain')
Net::SMTP.start('localhost', port, user: 'account', password: 'password', authtype: :plain){}

port = fake_server_start(auth: 'plain')
Net::SMTP.start('localhost', port, authtype: "PLAIN",
auth: {username: 'account', password: 'password'}){}

port = fake_server_start(auth: 'plain')
Net::SMTP.start('localhost', port, auth: {username: 'account',
password: 'password',
type: :plain}){}

port = fake_server_start(auth: 'plain')
assert_raise Net::SMTPAuthenticationError do
Net::SMTP.start('localhost', port, user: 'account', password: 'invalid', authtype: :plain){}
Expand Down

0 comments on commit f8e201f

Please sign in to comment.