Skip to content

Commit c48a4de

Browse files
authored
🔀 Merge pull request #195 from nevans/sasl/secrets-kwarg
✨ Add `secret` alias (for `password`, `oauth2_token`, etc) to relevant SASL mechanisms
2 parents ca7f3c3 + 1cb0c17 commit c48a4de

8 files changed

+46
-14
lines changed

‎lib/net/imap/sasl/cram_md5_authenticator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
class Net::IMAP::SASL::CramMD5Authenticator
1717
def initialize(user = nil, pass = nil,
1818
authcid: nil, username: nil,
19-
password: nil,
19+
password: nil, secret: nil,
2020
warn_deprecation: true,
2121
**)
2222
if warn_deprecation
2323
warn "WARNING: CRAM-MD5 mechanism is deprecated." # TODO: recommend SCRAM
2424
end
2525
require "digest/md5"
2626
@user = authcid || username || user
27-
@password = password || pass
27+
@password = password || secret || pass
2828
@done = false
2929
end
3030

‎lib/net/imap/sasl/digest_md5_authenticator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ class Net::IMAP::SASL::DigestMD5Authenticator
6969
# Any other keyword arguments are silently ignored.
7070
def initialize(user = nil, pass = nil, authz = nil,
7171
username: nil, password: nil, authzid: nil,
72-
authcid: nil,
72+
authcid: nil, secret: nil,
7373
warn_deprecation: true, **)
7474
username = authcid || username || user or
7575
raise ArgumentError, "missing username (authcid)"
76-
password ||= pass or raise ArgumentError, "missing password"
76+
password ||= secret || pass or raise ArgumentError, "missing password"
7777
authzid ||= authz
7878
if warn_deprecation
7979
warn "WARNING: DIGEST-MD5 SASL mechanism was deprecated by RFC6331."

‎lib/net/imap/sasl/login_authenticator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class Net::IMAP::SASL::LoginAuthenticator
2525

2626
def initialize(user = nil, pass = nil,
2727
authcid: nil, username: nil,
28-
password: nil,
28+
password: nil, secret: nil,
2929
warn_deprecation: true,
3030
**)
3131
if warn_deprecation
3232
warn "WARNING: LOGIN SASL mechanism is deprecated. Use PLAIN instead."
3333
end
3434
@user = authcid || username || user
35-
@password = password || pass
35+
@password = password || secret || pass
3636
@state = STATE_USER
3737
end
3838

‎lib/net/imap/sasl/oauthbearer_authenticator.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class OAuthBearerAuthenticator < OAuthAuthenticator
139139

140140
# An OAuth 2.0 bearer token. See {RFC-6750}[https://www.rfc-editor.org/rfc/rfc6750]
141141
attr_reader :oauth2_token
142+
alias secret oauth2_token
142143

143144
# :call-seq:
144145
# new(oauth2_token, **options) -> authenticator
@@ -173,10 +174,12 @@ class OAuthBearerAuthenticator < OAuthAuthenticator
173174
# noting that <b><em>application protocols are allowed to
174175
# require</em></b> #authzid (<em>or other parameters, such as</em> #host
175176
# _or_ #port) <b><em>as are specific server implementations</em></b>.
176-
def initialize(arg1 = nil, arg2 = nil, oauth2_token: nil, **args, &blk)
177+
def initialize(arg1 = nil, arg2 = nil,
178+
oauth2_token: nil, secret: nil,
179+
**args, &blk)
177180
username, oauth2_token_arg = arg2.nil? ? [nil, arg1] : [arg1, arg2]
178181
super(username: username, **args, &blk)
179-
@oauth2_token = oauth2_token || oauth2_token_arg or
182+
@oauth2_token = oauth2_token || secret || oauth2_token_arg or
180183
raise ArgumentError, "missing oauth2_token"
181184
end
182185

‎lib/net/imap/sasl/plain_authenticator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Net::IMAP::SASL::PlainAuthenticator
2626

2727
# A password or passphrase that matches the #username.
2828
attr_reader :password
29+
alias secret password
2930

3031
# Authorization identity: an identity to act as or on behalf of. The identity
3132
# form is application protocol specific. If not provided or left blank, the
@@ -64,11 +65,11 @@ class Net::IMAP::SASL::PlainAuthenticator
6465
#
6566
# Any other keyword parameters are quietly ignored.
6667
def initialize(user = nil, pass = nil,
67-
authcid: nil,
68+
authcid: nil, secret: nil,
6869
username: nil, password: nil, authzid: nil, **)
6970
username ||= authcid || user or
7071
raise ArgumentError, "missing username (authcid)"
71-
password ||= pass or raise ArgumentError, "missing password"
72+
password ||= secret || pass or raise ArgumentError, "missing password"
7273
raise ArgumentError, "username contains NULL" if username.include?(NULL)
7374
raise ArgumentError, "password contains NULL" if password.include?(NULL)
7475
raise ArgumentError, "authzid contains NULL" if authzid&.include?(NULL)

‎lib/net/imap/sasl/scram_authenticator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ class ScramAuthenticator
8080
def initialize(username_arg = nil, password_arg = nil,
8181
authcid: nil, username: nil,
8282
authzid: nil,
83-
password: nil,
83+
password: nil, secret: nil,
8484
min_iterations: 4096, # see both RFC5802 and RFC7677
8585
cnonce: nil, # must only be set in tests
8686
**options)
8787
@username = username || username_arg || authcid or
8888
raise ArgumentError, "missing username (authcid)"
89-
@password = password || password_arg or
89+
@password = password || secret || password_arg or
9090
raise ArgumentError, "missing password"
9191
@authzid = authzid
9292

@@ -109,6 +109,7 @@ def initialize(username_arg = nil, password_arg = nil,
109109

110110
# A password or passphrase that matches the #username.
111111
attr_reader :password
112+
alias secret password
112113

113114
# Authorization identity: an identity to act as or on behalf of. The
114115
# identity form is application protocol specific. If not provided or

‎lib/net/imap/sasl/xoauth2_authenticator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Net::IMAP::SASL::XOAuth2Authenticator
4242
# An OAuth2 access token which has been authorized with the appropriate OAuth2
4343
# scopes to use the service for #username.
4444
attr_reader :oauth2_token
45+
alias secret oauth2_token
4546

4647
# :call-seq:
4748
# new(username, oauth2_token, **) -> authenticator
@@ -68,10 +69,10 @@ class Net::IMAP::SASL::XOAuth2Authenticator
6869
#
6970
# Any other keyword parameters are quietly ignored.
7071
def initialize(user = nil, token = nil, username: nil, oauth2_token: nil,
71-
authzid: nil, **)
72+
authzid: nil, secret: nil, **)
7273
@username = authzid || username || user or
7374
raise ArgumentError, "missing username (authzid)"
74-
@oauth2_token = oauth2_token || token or
75+
@oauth2_token = oauth2_token || secret || token or
7576
raise ArgumentError, "missing oauth2_token"
7677
@done = false
7778
end

‎test/net/imap/test_imap_authenticators.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def test_plain_kw_params
5757
"zid\0cid\0p",
5858
plain(authcid: "cid", password: "p", authzid: "zid").process(nil)
5959
)
60+
assert_equal(
61+
"zid\0cid\0p",
62+
plain(username: "cid", secret: "p", authzid: "zid").process(nil)
63+
)
6064
end
6165

6266
def test_plain_username_kw_sets_both_authcid_and_authzid
@@ -96,6 +100,15 @@ def test_oauthbearer_response
96100
oauthbearer("mF_9.B5f-4.1JqM", authzid: "user@example.com",
97101
host: "server.example.com", port: 587).process(nil)
98102
)
103+
assert_equal(
104+
"n,a=user@example.com,\1host=server.example.com\1port=587\1" \
105+
"auth=Bearer sssssssss\1\1",
106+
oauthbearer(secret: "sssssssss", username: "user@example.com",
107+
host: "server.example.com", port: 587).process(nil)
108+
)
109+
assert_equal(
110+
"n,a=user,\1auth=Bearer tok\1\1", oauthbearer("user", "tok").process(nil)
111+
)
99112
end
100113

101114
# ----------------------
@@ -153,6 +166,15 @@ def test_scram_sha1_authenticator
153166
assert authenticator.done?
154167
end
155168

169+
def test_scram_kwargs
170+
authenticator = scram_sha1(authcid: "user", password: "pass")
171+
assert_equal "user", authenticator.authcid
172+
assert_equal "pass", authenticator.password
173+
authenticator = scram_sha1(username: "user", secret: "pass")
174+
assert_equal "user", authenticator.authcid
175+
assert_equal "pass", authenticator.password
176+
end
177+
156178
def test_scram_sha256_authenticator
157179
authenticator = scram_sha256("user", "pencil",
158180
cnonce: "rOprNGfwEbeRWgbNEkqO")
@@ -210,6 +232,10 @@ def test_xoauth2_kwargs
210232
"user=user\1auth=Bearer kwarg\1\1",
211233
xoauth2(username: "user", oauth2_token: "kwarg").process(nil)
212234
)
235+
assert_equal(
236+
"user=user\1auth=Bearer kwarg\1\1",
237+
xoauth2(authzid: "user", secret: "kwarg").process(nil)
238+
)
213239
end
214240

215241
def test_xoauth2_supports_initial_response

0 commit comments

Comments
 (0)