Skip to content

Commit 059698f

Browse files
committed
🔒 Add SASL EXTERNAL mechanism
1 parent ad5cbfc commit 059698f

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

lib/net/imap.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,12 @@ def starttls(options = {}, verify = true)
10021002
# Each mechanism has different properties and requirements. Please consult
10031003
# the documentation for the specific mechanisms you are using:
10041004
#
1005+
# +EXTERNAL+::
1006+
# See ExternalAuthenticator[Net::IMAP::SASL::ExternalAuthenticator].
1007+
#
1008+
# Login using already established credentials, such as a TLS certificate
1009+
# or IPsec.
1010+
#
10051011
# +PLAIN+::
10061012
# See PlainAuthenticator[Net::IMAP::SASL::PlainAuthenticator].
10071013
#

lib/net/imap/sasl.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class IMAP
2727
# Each mechanism has different properties and requirements. Please consult
2828
# the documentation for the specific mechanisms you are using:
2929
#
30+
# +EXTERNAL+::
31+
# See ExternalAuthenticator[Net::IMAP::SASL::ExternalAuthenticator].
32+
#
33+
# Login using already established credentials, such as a TLS certificate
34+
# or IPsec.
35+
#
3036
# +PLAIN+::
3137
# See PlainAuthenticator[Net::IMAP::SASL::PlainAuthenticator].
3238
#
@@ -70,6 +76,7 @@ module SASL
7076
sasl_dir = File.expand_path("sasl", __dir__)
7177
autoload :Authenticators, "#{sasl_dir}/authenticators"
7278

79+
autoload :ExternalAuthenticator, "#{sasl_dir}/external_authenticator"
7380
autoload :PlainAuthenticator, "#{sasl_dir}/plain_authenticator"
7481
autoload :XOAuth2Authenticator, "#{sasl_dir}/xoauth2_authenticator"
7582

lib/net/imap/sasl/authenticators.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Authenticators
3333
def initialize(use_defaults: false)
3434
@authenticators = {}
3535
if use_defaults
36+
add_authenticator "External"
3637
add_authenticator "Plain"
3738
add_authenticator "XOAuth2"
3839
add_authenticator "Login" # deprecated
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
module Net
4+
class IMAP < Protocol
5+
module SASL
6+
7+
# Authenticator for the "+EXTERNAL+" SASL mechanism, as specified by
8+
# RFC-4422[https://tools.ietf.org/html/rfc4422]. See
9+
# Net::IMAP#authenticate.
10+
#
11+
# The EXTERNAL mechanism requests that the server use client credentials
12+
# established external to SASL, for example by TLS certificate or IPsec.
13+
#
14+
class ExternalAuthenticator
15+
16+
# :call-seq:
17+
# initial_response? -> true
18+
#
19+
# +EXTERNAL+ can send an initial client response.
20+
def initial_response?; true end
21+
22+
##
23+
# :call-seq:
24+
# new -> authenticator
25+
# new(authzid, **) -> authenticator
26+
# new(authzid:, **) -> authenticator
27+
#
28+
# Creates an Authenticator for the "+EXTERNAL+" SASL mechanism, as
29+
# specified in RFC-4422[https://tools.ietf.org/html/rfc4422]. To use
30+
# this, see Net::IMAP#authenticate or your client's authentication
31+
# method.
32+
#
33+
# ==== Configuration parameters
34+
# Only one parameter, which is optional:
35+
#
36+
# * #authzid -- the identity to act as. Leave blank to use the identity
37+
# associated with the client's credentials.
38+
#
39+
# May be sent as a positional argument or as a keyword argument.
40+
#
41+
def initialize(authzid_arg = nil, authzid: nil)
42+
@authzid = authzid || authzid_arg
43+
[authzid, authzid_arg].compact.count <= 1 or
44+
raise ArgumentError, "conflicting values for authzid"
45+
if @authzid
46+
raise ArgumentError, "contains NULL" if @authzid.include? NULL
47+
@authzid = @authzid.encode "UTF-8"
48+
@authzid.valid_encoding? or raise ArgumentError, "not valid UTF-8"
49+
end
50+
end
51+
52+
attr_reader :authzid
53+
54+
def process(_)
55+
return "" if authzid.nil?
56+
if /\u0000/u.match?(authzid) # also validates UTF8 encoding
57+
raise DataFormatError, "authzid contains NULL"
58+
end
59+
authzid.encode "UTF-8"
60+
end
61+
62+
def authzid=(value)
63+
raise ArgumentError, "contains NULL" if value.include? NULL
64+
value = value.encode "UTF-8"
65+
value.valid_encoding? or raise ArgumentError, "not valid UTF-8"
66+
super(value)
67+
end
68+
69+
private
70+
71+
NULL = "\0"
72+
private_constant :NULL
73+
74+
end
75+
end
76+
end
77+
end

test/net/imap/test_imap_authenticators.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,37 @@ def test_xoauth2_supports_initial_response
7878
assert Net::IMAP::SASL.initial_response?(xoauth2("foo", "bar"))
7979
end
8080

81+
# ----------------------
82+
# EXTERNAL
83+
# ----------------------
84+
85+
def external(*args, **kwargs, &block)
86+
Net::IMAP::SASL.authenticator("EXTERNAL", *args, **kwargs, &block)
87+
end
88+
89+
def test_external_matches_mechanism
90+
assert_kind_of(Net::IMAP::SASL::ExternalAuthenticator, external)
91+
end
92+
93+
def test_external_response
94+
assert_equal("", external.process(nil))
95+
assert_equal("hello world", external("hello world").process(nil))
96+
assert_equal("kwargs",
97+
external(authzid: "kwargs").process(nil))
98+
end
99+
100+
def test_external_utf8
101+
assert_equal("", external.process(nil))
102+
assert_equal("🏴󠁧󠁢󠁥󠁮󠁧󠁿 England", external("🏴󠁧󠁢󠁥󠁮󠁧󠁿 England").process(nil))
103+
assert_equal("kwargs",
104+
external(authzid: "kwargs").process(nil))
105+
end
106+
107+
def test_external_invalid
108+
assert_raise(ArgumentError) { external("bad\0contains NULL") }
109+
assert_raise(ArgumentError) { external("invalid utf8\x80") }
110+
end
111+
81112
# ----------------------
82113
# LOGIN (obsolete)
83114
# ----------------------

0 commit comments

Comments
 (0)