Skip to content

Commit

Permalink
Add keyword parameters to authenticators
Browse files Browse the repository at this point in the history
Username can be set by args[0], authcid, username, or user.
Secret can be set by args[1], secret, or password.

Since all of the existing authenticators have the same API, it is
sufficient to update `check_args` in the base class.
  • Loading branch information
nevans committed Nov 7, 2023
1 parent 1b43c0d commit c933c3e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/net/smtp/auth_cram_md5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ class Net::SMTP
class AuthCramMD5 < Net::SMTP::Authenticator
auth_type :cram_md5

def auth(user, secret)
def auth(user_arg = nil, secret_arg = nil,
authcid: nil, username: nil, user: nil,
secret: nil, password: nil,
**)
user = req_param authcid, username, user, user_arg, "username (authcid)"
secret = req_param password, secret, secret_arg, "secret (password)"
challenge = continue('AUTH CRAM-MD5')
crammed = cram_md5_response(secret, challenge.unpack1('m'))
finish(base64_encode("#{user} #{crammed}"))
Expand Down
7 changes: 6 additions & 1 deletion lib/net/smtp/auth_login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ class Net::SMTP
class AuthLogin < Net::SMTP::Authenticator
auth_type :login

def auth(user, secret)
def auth(user_arg = nil, secret_arg = nil,
authcid: nil, username: nil, user: nil,
secret: nil, password: nil,
**)
user = req_param authcid, username, user, user_arg, "username (authcid)"
secret = req_param password, secret, secret_arg, "secret (password)"
continue('AUTH LOGIN')
continue(base64_encode(user))
finish(base64_encode(secret))
Expand Down
7 changes: 6 additions & 1 deletion lib/net/smtp/auth_plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ class Net::SMTP
class AuthPlain < Net::SMTP::Authenticator
auth_type :plain

def auth(user, secret)
def auth(user_arg = nil, secret_arg = nil,
authcid: nil, username: nil, user: nil,
secret: nil, password: nil,
**)
user = req_param authcid, username, user, user_arg, "username (authcid)"
secret = req_param password, secret, secret_arg, "secret (password)"
finish('AUTH PLAIN ' + base64_encode("\0#{user}\0#{secret}"))
end
end
Expand Down
15 changes: 12 additions & 3 deletions lib/net/smtp/authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ def self.auth_class(type)
Authenticator.auth_classes[type]
end

def self.check_args(user_arg = nil, secret_arg = nil, *, **)
unless user_arg
def self.check_args(user_arg = nil, secret_arg = nil, *,
authcid: nil, username: nil, user: nil,
secret: nil, password: nil,
**)
unless authcid || username || user || user_arg
raise ArgumentError, 'SMTP-AUTH requested but missing user name'
end
unless secret_arg
unless password || secret || secret_arg
raise ArgumentError, 'SMTP-AUTH requested but missing secret phrase'
end
end
Expand Down Expand Up @@ -52,6 +55,12 @@ def base64_encode(str)
# expects "str" may not become too long
[str].pack('m0')
end

def req_param(*args, name)
args.compact.first or
raise ArgumentError, "SMTP-AUTH requested but missing #{name}"
end

end
end
end
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 c933c3e

Please sign in to comment.