Skip to content

Commit 7a6f2f1

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 3e72a95 commit 7a6f2f1

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/net/smtp.rb

Lines changed: 23 additions & 8 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', user: 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', user = 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 an optional hash of keyword arguments for #authenticate.
526+
#
524527
# See the discussion of Net::SMTP@SMTP+Authentication in the overview notes.
525528
#
526529
# === Errors
@@ -538,6 +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,
544+
auth: nil,
541545
tls: false, starttls: :auto,
542546
tls_verify: true, tls_hostname: nil, ssl_context_params: nil,
543547
&block)
@@ -546,7 +550,8 @@ def SMTP.start(address, port = nil, *args, helo: nil,
546550
user ||= args[1]
547551
secret ||= password || args[2]
548552
authtype ||= args[3]
549-
new(address, port, tls: tls, starttls: starttls, tls_verify: tls_verify, tls_hostname: tls_hostname, ssl_context_params: ssl_context_params).start(helo: helo, user: user, 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, user: user, secret: secret, authtype: authtype, auth: auth, &block)
550555
end
551556

552557
# +true+ if the \SMTP session has been started.
@@ -558,6 +563,7 @@ def started?
558563
# :call-seq:
559564
# start(helo: 'localhost', user: nil, secret: nil, authtype: nil) { |smtp| ... }
560565
# start(helo = 'localhost', user = nil, secret = nil, authtype = nil) { |smtp| ... }
566+
# start(helo = 'localhost', auth: {type: nil, **auth_kwargs}) { |smtp| ... }
561567
#
562568
# Opens a TCP connection and starts the SMTP session.
563569
#
@@ -578,6 +584,8 @@ def started?
578584
# These will be sent to #authenticate as positional arguments-the exact
579585
# semantics are dependent on the +authtype+.
580586
#
587+
# +auth+ is an optional hash of keyword arguments for #authenticate.
588+
#
581589
# See the discussion of Net::SMTP@SMTP+Authentication in the overview notes.
582590
#
583591
# See also: Net::SMTP.start
@@ -619,12 +627,15 @@ def started?
619627
# * Net::ReadTimeout
620628
# * IOError
621629
#
622-
def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil)
630+
def start(*args, helo: nil,
631+
user: nil, secret: nil, password: nil,
632+
authtype: nil, auth: nil)
623633
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..4)" if args.size > 4
624634
helo ||= args[0] || 'localhost'
625635
user ||= args[1]
626636
secret ||= password || args[2]
627637
authtype ||= args[3]
638+
auth ||= {}
628639
if defined?(OpenSSL::VERSION)
629640
ssl_context_params = @ssl_context_params || {}
630641
unless ssl_context_params.has_key?(:verify_mode)
@@ -639,13 +650,13 @@ def start(*args, helo: nil, user: nil, secret: nil, password: nil, authtype: nil
639650
end
640651
if block_given?
641652
begin
642-
do_start helo, user, secret, authtype
653+
do_start helo, user, secret, authtype, **auth
643654
return yield(self)
644655
ensure
645656
do_finish
646657
end
647658
else
648-
do_start helo, user, secret, authtype
659+
do_start helo, user, secret, authtype, **auth
649660
return self
650661
end
651662
end
@@ -663,10 +674,10 @@ def tcp_socket(address, port)
663674
TCPSocket.open address, port
664675
end
665676

666-
def do_start(helo_domain, user, secret, authtype)
677+
def do_start(helo_domain, user, secret, authtype, **auth)
667678
raise IOError, 'SMTP session already started' if @started
668-
if user || secret || authtype
669-
check_auth_args authtype, user, secret
679+
if user || secret || authtype || auth.any?
680+
check_auth_args(authtype, user, secret, **auth)
670681
end
671682
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
672683
tcp_socket(@address, @port)
@@ -684,7 +695,11 @@ def do_start(helo_domain, user, secret, authtype)
684695
# helo response may be different after STARTTLS
685696
do_helo helo_domain
686697
end
687-
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
688703
@started = true
689704
ensure
690705
unless @started

0 commit comments

Comments
 (0)