Skip to content

Commit f8e201f

Browse files
committed
Add auth keyword arg to start methods
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`.
1 parent 196552b commit f8e201f

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/net/smtp.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ def debug_output=(arg)
459459

460460
#
461461
# :call-seq:
462+
# start(address, port = nil, helo: 'localhost', auth: nil, tls: false, starttls: :auto, tls_verify: true, tls_hostname: nil, ssl_context_params: nil) { |smtp| ... }
462463
# 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| ... }
463464
# start(address, port = nil, helo = 'localhost', username = nil, secret = nil, authtype = nil) { |smtp| ... }
464465
#
@@ -521,6 +522,8 @@ def debug_output=(arg)
521522
# These will be sent to #authenticate as positional arguments—the exact
522523
# semantics are dependent on the +authtype+.
523524
#
525+
# +auth+ is a hash of arbitrary keyword arguments for #authenticate.
526+
#
524527
# See the discussion of Net::SMTP@SMTP+Authentication in the overview notes.
525528
#
526529
# === Errors
@@ -538,7 +541,7 @@ def debug_output=(arg)
538541
#
539542
def SMTP.start(address, port = nil, *args, helo: nil,
540543
user: nil, secret: nil, password: nil, authtype: nil,
541-
username: nil,
544+
username: nil, auth: nil,
542545
tls: false, starttls: :auto,
543546
tls_verify: true, tls_hostname: nil, ssl_context_params: nil,
544547
&block)
@@ -547,7 +550,8 @@ def SMTP.start(address, port = nil, *args, helo: nil,
547550
username ||= user || args[1]
548551
secret ||= password || args[2]
549552
authtype ||= args[3]
550-
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)
553+
new(address, port, tls: tls, starttls: starttls, tls_verify: tls_verify, tls_hostname: tls_hostname, ssl_context_params: ssl_context_params)
554+
.start(helo: helo, username: username, secret: secret, authtype: authtype, auth: auth, &block)
551555
end
552556

553557
# +true+ if the \SMTP session has been started.
@@ -559,6 +563,7 @@ def started?
559563
# :call-seq:
560564
# start(helo: 'localhost', username: nil, secret: nil, authtype: nil) { |smtp| ... }
561565
# start(helo = 'localhost', username = nil, secret = nil, authtype = nil) { |smtp| ... }
566+
# start(helo = 'localhost', auth: {type: nil, **auth_kwargs}) { |smtp| ... }
562567
#
563568
# Opens a TCP connection and starts the SMTP session.
564569
#
@@ -579,6 +584,8 @@ def started?
579584
# These will be sent to #authenticate as positional arguments—the exact
580585
# semantics are dependent on the +authtype+.
581586
#
587+
# +auth+ is an optional hash of keyword arguments for #authenticate.
588+
#
582589
# See the discussion of Net::SMTP@SMTP+Authentication in the overview notes.
583590
#
584591
# See also: Net::SMTP.start
@@ -620,13 +627,15 @@ def started?
620627
# * Net::ReadTimeout
621628
# * IOError
622629
#
623-
def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil,
624-
username: nil)
630+
def start(*args, helo: nil,
631+
user: nil, username: nil, secret: nil, password: nil,
632+
authtype: nil, auth: nil)
625633
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..4)" if args.size > 4
626634
helo ||= args[0] || 'localhost'
627635
username ||= user || args[1]
628636
secret ||= password || args[2]
629637
authtype ||= args[3]
638+
auth ||= {}
630639
if defined?(OpenSSL::VERSION)
631640
ssl_context_params = @ssl_context_params || {}
632641
unless ssl_context_params.has_key?(:verify_mode)
@@ -641,13 +650,13 @@ def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil
641650
end
642651
if block_given?
643652
begin
644-
do_start helo, username, secret, authtype
653+
do_start helo, username, secret, authtype, **auth
645654
return yield(self)
646655
ensure
647656
do_finish
648657
end
649658
else
650-
do_start helo, username, secret, authtype
659+
do_start helo, username, secret, authtype, **auth
651660
return self
652661
end
653662
end
@@ -665,10 +674,10 @@ def tcp_socket(address, port)
665674
TCPSocket.open address, port
666675
end
667676

668-
def do_start(helo_domain, user, secret, authtype)
677+
def do_start(helo_domain, user, secret, authtype, **auth)
669678
raise IOError, 'SMTP session already started' if @started
670-
if user || secret || authtype
671-
check_auth_args authtype, user, secret
679+
if user || secret || authtype || auth.any?
680+
check_auth_args(authtype, user, secret, **auth)
672681
end
673682
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
674683
tcp_socket(@address, @port)
@@ -686,7 +695,11 @@ def do_start(helo_domain, user, secret, authtype)
686695
# helo response may be different after STARTTLS
687696
do_helo helo_domain
688697
end
689-
authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
698+
if user or secret
699+
authenticate(user, secret, authtype, **auth)
700+
elsif authtype or auth.any?
701+
authenticate(authtype, **auth)
702+
end
690703
@started = true
691704
ensure
692705
unless @started

test/net/smtp/test_smtp.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,15 @@ def test_start_auth_plain
455455
port = fake_server_start(auth: 'plain')
456456
Net::SMTP.start('localhost', port, user: 'account', password: 'password', authtype: :plain){}
457457

458+
port = fake_server_start(auth: 'plain')
459+
Net::SMTP.start('localhost', port, authtype: "PLAIN",
460+
auth: {username: 'account', password: 'password'}){}
461+
462+
port = fake_server_start(auth: 'plain')
463+
Net::SMTP.start('localhost', port, auth: {username: 'account',
464+
password: 'password',
465+
type: :plain}){}
466+
458467
port = fake_server_start(auth: 'plain')
459468
assert_raise Net::SMTPAuthenticationError do
460469
Net::SMTP.start('localhost', port, user: 'account', password: 'invalid', authtype: :plain){}

0 commit comments

Comments
 (0)