Skip to content

Commit a587fc7

Browse files
committed
Update AUTH=PLAIN to be a little closer to RFC4616
* Add authzid support * must not contain NULL chars * improve rdoc
1 parent 23f241b commit a587fc7

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/net/imap/authenticators/plain.rb

+18-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44
#
55
# See RFC4616[https://tools.ietf.org/html/rfc4616] for the specification.
66
class Net::IMAP::PlainAuthenticator
7+
78
def process(data)
8-
return "\0#{@user}\0#{@password}"
9+
return "#@authzid\0#@username\0#@password"
910
end
1011

12+
NULL = -"\0".b
13+
1114
private
1215

13-
def initialize(user, password)
14-
@user = user
16+
# +username+ is the authentication identity, the identity whose +password+ is
17+
# used. +username+ is referred to as +authcid+ by
18+
# RFC4616[https://tools.ietf.org/html/rfc4616].
19+
#
20+
# +authzid+ is the authorization identity (identity to act as). It can
21+
# usually be left blank. When +authzid+ is left blank (nil or empty string)
22+
# the server will derive an identity from the credentials and use that as the
23+
# authorization identity.
24+
def initialize(username, password, authzid: nil)
25+
raise ArgumentError, "username contains NULL" if username&.include?(NULL)
26+
raise ArgumentError, "password contains NULL" if password&.include?(NULL)
27+
raise ArgumentError, "authzid contains NULL" if authzid&.include?(NULL)
28+
@username = username
1529
@password = password
30+
@authzid = authzid
1631
end
1732

1833
Net::IMAP.add_authenticator "PLAIN", self
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
6+
class IMAPAuthenticatorsTest < Test::Unit::TestCase
7+
8+
PLAIN = Net::IMAP::PlainAuthenticator
9+
10+
def test_plain
11+
assert_equal("\0authc\0passwd",
12+
PLAIN.new("authc", "passwd").process(nil))
13+
assert_equal("authz\0user\0pass",
14+
PLAIN.new("user", "pass", authzid: "authz").process(nil))
15+
end
16+
17+
def test_plain_no_null_chars
18+
assert_raise(ArgumentError) { PLAIN.new("bad\0user", "pass") }
19+
assert_raise(ArgumentError) { PLAIN.new("user", "bad\0pass") }
20+
assert_raise(ArgumentError) { PLAIN.new("u", "p", authzid: "bad\0authz") }
21+
end
22+
23+
end

0 commit comments

Comments
 (0)