net-imap 0.4.24 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +10 -1
- data/README.md +10 -4
- data/docs/styles.css +75 -14
- data/lib/net/imap/authenticators.rb +2 -2
- data/lib/net/imap/command_data.rb +73 -78
- data/lib/net/imap/config/attr_type_coercion.rb +22 -10
- data/lib/net/imap/config/attr_version_defaults.rb +93 -0
- data/lib/net/imap/config.rb +70 -94
- data/lib/net/imap/connection_state.rb +48 -0
- data/lib/net/imap/data_encoding.rb +3 -3
- data/lib/net/imap/data_lite.rb +226 -0
- data/lib/net/imap/deprecated_client_options.rb +6 -3
- data/lib/net/imap/errors.rb +6 -0
- data/lib/net/imap/esearch_result.rb +219 -0
- data/lib/net/imap/fetch_data.rb +126 -47
- data/lib/net/imap/flags.rb +1 -1
- data/lib/net/imap/response_data.rb +120 -186
- data/lib/net/imap/response_parser/parser_utils.rb +5 -0
- data/lib/net/imap/response_parser.rb +155 -21
- data/lib/net/imap/response_reader.rb +9 -12
- data/lib/net/imap/sasl/anonymous_authenticator.rb +3 -3
- data/lib/net/imap/sasl/authentication_exchange.rb +52 -20
- data/lib/net/imap/sasl/authenticators.rb +8 -4
- data/lib/net/imap/sasl/client_adapter.rb +77 -26
- data/lib/net/imap/sasl/cram_md5_authenticator.rb +4 -4
- data/lib/net/imap/sasl/digest_md5_authenticator.rb +218 -56
- data/lib/net/imap/sasl/external_authenticator.rb +2 -2
- data/lib/net/imap/sasl/gs2_header.rb +7 -7
- data/lib/net/imap/sasl/login_authenticator.rb +4 -3
- data/lib/net/imap/sasl/oauthbearer_authenticator.rb +6 -6
- data/lib/net/imap/sasl/plain_authenticator.rb +7 -7
- data/lib/net/imap/sasl/protocol_adapters.rb +60 -4
- data/lib/net/imap/sasl/scram_authenticator.rb +10 -10
- data/lib/net/imap/sasl.rb +7 -4
- data/lib/net/imap/sasl_adapter.rb +0 -1
- data/lib/net/imap/search_result.rb +4 -5
- data/lib/net/imap/sequence_set.rb +529 -154
- data/lib/net/imap/stringprep/nameprep.rb +1 -1
- data/lib/net/imap/stringprep/trace.rb +4 -4
- data/lib/net/imap/uidplus_data.rb +2 -84
- data/lib/net/imap/vanished_data.rb +65 -0
- data/lib/net/imap.rb +996 -305
- data/net-imap.gemspec +1 -1
- data/rakelib/rfcs.rake +2 -0
- data/rakelib/string_prep_tables_generator.rb +6 -2
- metadata +7 -2
|
@@ -325,6 +325,24 @@ module Net
|
|
|
325
325
|
SEQUENCE_SET = /#{SEQUENCE_SET_ITEM}(?:,#{SEQUENCE_SET_ITEM})*/n
|
|
326
326
|
SEQUENCE_SET_STR = /\A#{SEQUENCE_SET}\z/n
|
|
327
327
|
|
|
328
|
+
# partial-range-first = nz-number ":" nz-number
|
|
329
|
+
# ;; Request to search from oldest (lowest UIDs) to
|
|
330
|
+
# ;; more recent messages.
|
|
331
|
+
# ;; A range 500:400 is the same as 400:500.
|
|
332
|
+
# ;; This is similar to <seq-range> from [RFC3501]
|
|
333
|
+
# ;; but cannot contain "*".
|
|
334
|
+
PARTIAL_RANGE_FIRST = /\A(#{NZ_NUMBER}):(#{NZ_NUMBER})\z/n
|
|
335
|
+
|
|
336
|
+
# partial-range-last = MINUS nz-number ":" MINUS nz-number
|
|
337
|
+
# ;; Request to search from newest (highest UIDs) to
|
|
338
|
+
# ;; oldest messages.
|
|
339
|
+
# ;; A range -500:-400 is the same as -400:-500.
|
|
340
|
+
PARTIAL_RANGE_LAST = /\A(-#{NZ_NUMBER}):(-#{NZ_NUMBER})\z/n
|
|
341
|
+
|
|
342
|
+
# partial-range = partial-range-first / partial-range-last
|
|
343
|
+
PARTIAL_RANGE = Regexp.union(PARTIAL_RANGE_FIRST,
|
|
344
|
+
PARTIAL_RANGE_LAST)
|
|
345
|
+
|
|
328
346
|
# RFC3501:
|
|
329
347
|
# literal = "{" number "}" CRLF *CHAR8
|
|
330
348
|
# ; Number represents the number of CHAR8s
|
|
@@ -720,7 +738,7 @@ module Net
|
|
|
720
738
|
when "EXISTS" then mailbox_data__exists # RFC3501, RFC9051
|
|
721
739
|
when "ESEARCH" then esearch_response # RFC4731, RFC9051, etc
|
|
722
740
|
when "VANISHED" then expunged_resp # RFC7162
|
|
723
|
-
when "UIDFETCH" then uidfetch_resp #
|
|
741
|
+
when "UIDFETCH" then uidfetch_resp # RFC9586
|
|
724
742
|
when "SEARCH" then mailbox_data__search # RFC3501 (obsolete)
|
|
725
743
|
when "CAPABILITY" then capability_data__untagged # RFC3501, RFC9051
|
|
726
744
|
when "FLAGS" then mailbox_data__flags # RFC3501, RFC9051
|
|
@@ -773,9 +791,6 @@ module Net
|
|
|
773
791
|
def response_data__ignored; response_data__unhandled(IgnoredResponse) end
|
|
774
792
|
alias response_data__noop response_data__ignored
|
|
775
793
|
|
|
776
|
-
alias esearch_response response_data__unhandled
|
|
777
|
-
alias expunged_resp response_data__unhandled
|
|
778
|
-
alias uidfetch_resp response_data__unhandled
|
|
779
794
|
alias listrights_data response_data__unhandled
|
|
780
795
|
alias myrights_data response_data__unhandled
|
|
781
796
|
alias metadata_resp response_data__unhandled
|
|
@@ -836,6 +851,14 @@ module Net
|
|
|
836
851
|
UntaggedResponse.new(name, data, @str)
|
|
837
852
|
end
|
|
838
853
|
|
|
854
|
+
# uidfetch-resp = uniqueid SP "UIDFETCH" SP msg-att
|
|
855
|
+
def uidfetch_resp
|
|
856
|
+
uid = uniqueid; SP!
|
|
857
|
+
name = label "UIDFETCH"; SP!
|
|
858
|
+
data = UIDFetchData.new(uid, msg_att(uid))
|
|
859
|
+
UntaggedResponse.new(name, data, @str)
|
|
860
|
+
end
|
|
861
|
+
|
|
839
862
|
def response_data__simple_numeric
|
|
840
863
|
data = nz_number; SP!
|
|
841
864
|
name = tagged_ext_label
|
|
@@ -846,6 +869,20 @@ module Net
|
|
|
846
869
|
alias mailbox_data__exists response_data__simple_numeric
|
|
847
870
|
alias mailbox_data__recent response_data__simple_numeric
|
|
848
871
|
|
|
872
|
+
# The name for this is confusing, because it *replaces* EXPUNGE
|
|
873
|
+
# >>>
|
|
874
|
+
# expunged-resp = "VANISHED" [SP "(EARLIER)"] SP known-uids
|
|
875
|
+
def expunged_resp
|
|
876
|
+
name = label "VANISHED"; SP!
|
|
877
|
+
earlier = if lpar? then label("EARLIER"); rpar; SP!; true else false end
|
|
878
|
+
uids = known_uids
|
|
879
|
+
data = VanishedData[uids, earlier]
|
|
880
|
+
UntaggedResponse.new name, data, @str
|
|
881
|
+
end
|
|
882
|
+
|
|
883
|
+
# TODO: replace with uid_set
|
|
884
|
+
alias known_uids sequence_set
|
|
885
|
+
|
|
849
886
|
# RFC3501 & RFC9051:
|
|
850
887
|
# msg-att = "(" (msg-att-dynamic / msg-att-static)
|
|
851
888
|
# *(SP (msg-att-dynamic / msg-att-static)) ")"
|
|
@@ -1321,31 +1358,19 @@ module Net
|
|
|
1321
1358
|
# header-fld-name = astring
|
|
1322
1359
|
#
|
|
1323
1360
|
# NOTE: Previously, Net::IMAP recreated the raw original source string.
|
|
1324
|
-
# Now, it
|
|
1325
|
-
#
|
|
1326
|
-
#
|
|
1327
|
-
# standard header field names are valid atoms:
|
|
1361
|
+
# Now, it returns the decoded astring value. Although this is technically
|
|
1362
|
+
# incompatible, it should almost never make a difference: all standard
|
|
1363
|
+
# header field names are valid atoms:
|
|
1328
1364
|
#
|
|
1329
1365
|
# https://www.iana.org/assignments/message-headers/message-headers.xhtml
|
|
1330
1366
|
#
|
|
1331
|
-
#
|
|
1332
|
-
# or more of the printable US-ASCII characters, except SP and colon. So
|
|
1333
|
-
# empty string isn't valid, and literals aren't needed and should not be
|
|
1334
|
-
# used. This is explicitly unchanged by [I18N-HDRS] (RFC6532).
|
|
1335
|
-
#
|
|
1336
|
-
# RFC5233:
|
|
1367
|
+
# See also RFC5233:
|
|
1337
1368
|
# optional-field = field-name ":" unstructured CRLF
|
|
1338
1369
|
# field-name = 1*ftext
|
|
1339
1370
|
# ftext = %d33-57 / ; Printable US-ASCII
|
|
1340
1371
|
# %d59-126 ; characters not including
|
|
1341
1372
|
# ; ":".
|
|
1342
|
-
|
|
1343
|
-
assert_no_lookahead
|
|
1344
|
-
start = @pos
|
|
1345
|
-
astring
|
|
1346
|
-
end_pos = @token ? @pos - 1 : @pos
|
|
1347
|
-
@str[start...end_pos]
|
|
1348
|
-
end
|
|
1373
|
+
alias header_fld_name astring
|
|
1349
1374
|
|
|
1350
1375
|
# mailbox-data = "FLAGS" SP flag-list / "LIST" SP mailbox-list /
|
|
1351
1376
|
# "LSUB" SP mailbox-list / "SEARCH" *(SP nz-number) /
|
|
@@ -1484,6 +1509,111 @@ module Net
|
|
|
1484
1509
|
end
|
|
1485
1510
|
alias sort_data mailbox_data__search
|
|
1486
1511
|
|
|
1512
|
+
# esearch-response = "ESEARCH" [search-correlator] [SP "UID"]
|
|
1513
|
+
# *(SP search-return-data)
|
|
1514
|
+
# ;; Note that SEARCH and ESEARCH responses
|
|
1515
|
+
# ;; SHOULD be mutually exclusive,
|
|
1516
|
+
# ;; i.e., only one of the response types
|
|
1517
|
+
# ;; should be
|
|
1518
|
+
# ;; returned as a result of a command.
|
|
1519
|
+
# esearch-response = "ESEARCH" [search-correlator] [SP "UID"]
|
|
1520
|
+
# *(SP search-return-data)
|
|
1521
|
+
# ; ESEARCH response replaces SEARCH response
|
|
1522
|
+
# ; from IMAP4rev1.
|
|
1523
|
+
# search-correlator = SP "(" "TAG" SP tag-string ")"
|
|
1524
|
+
def esearch_response
|
|
1525
|
+
name = label("ESEARCH")
|
|
1526
|
+
tag = search_correlator if peek_str?(" (")
|
|
1527
|
+
uid = peek_re?(/\G UID\b/i) && (SP!; label("UID"); true)
|
|
1528
|
+
data = []
|
|
1529
|
+
data << search_return_data while SP?
|
|
1530
|
+
esearch = ESearchResult.new(tag, uid, data)
|
|
1531
|
+
UntaggedResponse.new(name, esearch, @str)
|
|
1532
|
+
end
|
|
1533
|
+
|
|
1534
|
+
# From RFC4731 (ESEARCH):
|
|
1535
|
+
# search-return-data = "MIN" SP nz-number /
|
|
1536
|
+
# "MAX" SP nz-number /
|
|
1537
|
+
# "ALL" SP sequence-set /
|
|
1538
|
+
# "COUNT" SP number /
|
|
1539
|
+
# search-ret-data-ext
|
|
1540
|
+
# ; All return data items conform to
|
|
1541
|
+
# ; search-ret-data-ext syntax.
|
|
1542
|
+
# search-ret-data-ext = search-modifier-name SP search-return-value
|
|
1543
|
+
# search-modifier-name = tagged-ext-label
|
|
1544
|
+
# search-return-value = tagged-ext-val
|
|
1545
|
+
#
|
|
1546
|
+
# From RFC4731 (ESEARCH):
|
|
1547
|
+
# search-return-data =/ "MODSEQ" SP mod-sequence-value
|
|
1548
|
+
#
|
|
1549
|
+
# From RFC9394 (PARTIAL):
|
|
1550
|
+
# search-return-data =/ ret-data-partial
|
|
1551
|
+
#
|
|
1552
|
+
def search_return_data
|
|
1553
|
+
label = search_modifier_name; SP!
|
|
1554
|
+
value =
|
|
1555
|
+
case label
|
|
1556
|
+
when "MIN" then nz_number
|
|
1557
|
+
when "MAX" then nz_number
|
|
1558
|
+
when "ALL" then sequence_set
|
|
1559
|
+
when "COUNT" then number
|
|
1560
|
+
when "MODSEQ" then mod_sequence_value # RFC7162: CONDSTORE
|
|
1561
|
+
when "PARTIAL" then ret_data_partial__value # RFC9394: PARTIAL
|
|
1562
|
+
else search_return_value
|
|
1563
|
+
end
|
|
1564
|
+
[label, value]
|
|
1565
|
+
end
|
|
1566
|
+
|
|
1567
|
+
# From RFC5267 (CONTEXT=SEARCH, CONTEXT=SORT) and RFC9394 (PARTIAL):
|
|
1568
|
+
# ret-data-partial = "PARTIAL"
|
|
1569
|
+
# SP "(" partial-range SP partial-results ")"
|
|
1570
|
+
def ret_data_partial__value
|
|
1571
|
+
lpar
|
|
1572
|
+
range = partial_range; SP!
|
|
1573
|
+
results = partial_results
|
|
1574
|
+
rpar
|
|
1575
|
+
ESearchResult::PartialResult.new(range, results)
|
|
1576
|
+
end
|
|
1577
|
+
|
|
1578
|
+
# partial-range = partial-range-first / partial-range-last
|
|
1579
|
+
# tagged-ext-simple =/ partial-range-last
|
|
1580
|
+
def partial_range
|
|
1581
|
+
case (str = atom)
|
|
1582
|
+
when Patterns::PARTIAL_RANGE_FIRST, Patterns::PARTIAL_RANGE_LAST
|
|
1583
|
+
min, max = [Integer($1), Integer($2)].minmax
|
|
1584
|
+
min..max
|
|
1585
|
+
else
|
|
1586
|
+
parse_error("unexpected atom %p, expected partial-range", str)
|
|
1587
|
+
end
|
|
1588
|
+
end
|
|
1589
|
+
|
|
1590
|
+
# partial-results = sequence-set / "NIL"
|
|
1591
|
+
# ;; <sequence-set> from [RFC3501].
|
|
1592
|
+
# ;; NIL indicates that no results correspond to
|
|
1593
|
+
# ;; the requested range.
|
|
1594
|
+
def partial_results; NIL? ? nil : sequence_set end
|
|
1595
|
+
|
|
1596
|
+
# search-modifier-name = tagged-ext-label
|
|
1597
|
+
alias search_modifier_name tagged_ext_label
|
|
1598
|
+
|
|
1599
|
+
# search-return-value = tagged-ext-val
|
|
1600
|
+
# ; Data for the returned search option.
|
|
1601
|
+
# ; A single "nz-number"/"number"/"number64" value
|
|
1602
|
+
# ; can be returned as an atom (i.e., without
|
|
1603
|
+
# ; quoting). A sequence-set can be returned
|
|
1604
|
+
# ; as an atom as well.
|
|
1605
|
+
def search_return_value; ExtensionData.new(tagged_ext_val) end
|
|
1606
|
+
|
|
1607
|
+
# search-correlator = SP "(" "TAG" SP tag-string ")"
|
|
1608
|
+
def search_correlator
|
|
1609
|
+
SP!; lpar; label("TAG"); SP!; tag = tag_string; rpar
|
|
1610
|
+
tag
|
|
1611
|
+
end
|
|
1612
|
+
|
|
1613
|
+
# tag-string = astring
|
|
1614
|
+
# ; <tag> represented as <astring>
|
|
1615
|
+
alias tag_string astring
|
|
1616
|
+
|
|
1487
1617
|
# RFC5256: THREAD
|
|
1488
1618
|
# thread-data = "THREAD" [SP 1*thread-list]
|
|
1489
1619
|
def thread_data
|
|
@@ -1816,6 +1946,9 @@ module Net
|
|
|
1816
1946
|
#
|
|
1817
1947
|
# RFC8474: OBJECTID
|
|
1818
1948
|
# resp-text-code =/ "MAILBOXID" SP "(" objectid ")"
|
|
1949
|
+
#
|
|
1950
|
+
# RFC9586: UIDONLY
|
|
1951
|
+
# resp-text-code =/ "UIDREQUIRED"
|
|
1819
1952
|
def resp_text_code
|
|
1820
1953
|
name = resp_text_code__name
|
|
1821
1954
|
data =
|
|
@@ -1838,6 +1971,7 @@ module Net
|
|
|
1838
1971
|
when "HIGHESTMODSEQ" then SP!; mod_sequence_value # CONDSTORE
|
|
1839
1972
|
when "MODIFIED" then SP!; sequence_set # CONDSTORE
|
|
1840
1973
|
when "MAILBOXID" then SP!; parens__objectid # RFC8474: OBJECTID
|
|
1974
|
+
when "UIDREQUIRED" then # RFC9586: UIDONLY
|
|
1841
1975
|
else
|
|
1842
1976
|
SP? and text_chars_except_rbra
|
|
1843
1977
|
end
|
|
@@ -29,11 +29,10 @@ module Net
|
|
|
29
29
|
|
|
30
30
|
attr_reader :buff, :literal_size
|
|
31
31
|
|
|
32
|
-
def bytes_read
|
|
33
|
-
def empty
|
|
34
|
-
def done
|
|
35
|
-
def
|
|
36
|
-
def line_done?; buff.end_with?(CRLF) end
|
|
32
|
+
def bytes_read = buff.bytesize
|
|
33
|
+
def empty? = buff.empty?
|
|
34
|
+
def done? = line_done? && !literal_size
|
|
35
|
+
def line_done? = buff.end_with?(CRLF)
|
|
37
36
|
|
|
38
37
|
def get_literal_size(buff)
|
|
39
38
|
buff.end_with?("}\r\n") && buff.rindex(/\{(\d+)\}\r\n\z/n) && $1.to_i
|
|
@@ -59,10 +58,10 @@ module Net
|
|
|
59
58
|
[limit, max_response_remaining!].compact.min
|
|
60
59
|
end
|
|
61
60
|
|
|
62
|
-
def max_response_size
|
|
63
|
-
def max_response_remaining
|
|
64
|
-
def response_too_large
|
|
65
|
-
def min_response_size
|
|
61
|
+
def max_response_size = client.max_response_size
|
|
62
|
+
def max_response_remaining = max_response_size &.- bytes_read
|
|
63
|
+
def response_too_large? = max_response_size &.< min_response_size
|
|
64
|
+
def min_response_size = bytes_read + min_response_remaining
|
|
66
65
|
|
|
67
66
|
def min_response_remaining
|
|
68
67
|
empty? ? 3 : done? ? 0 : (literal_size || 0) + 2
|
|
@@ -71,9 +70,7 @@ module Net
|
|
|
71
70
|
def max_response_remaining!
|
|
72
71
|
return max_response_remaining unless response_too_large?
|
|
73
72
|
raise ResponseTooLargeError.new(
|
|
74
|
-
max_response_size
|
|
75
|
-
bytes_read: bytes_read,
|
|
76
|
-
literal_size: literal_size,
|
|
73
|
+
max_response_size:, bytes_read:, literal_size:,
|
|
77
74
|
)
|
|
78
75
|
end
|
|
79
76
|
|
|
@@ -5,7 +5,7 @@ module Net
|
|
|
5
5
|
module SASL
|
|
6
6
|
|
|
7
7
|
# Authenticator for the "+ANONYMOUS+" SASL mechanism, as specified by
|
|
8
|
-
# RFC-4505[https://
|
|
8
|
+
# RFC-4505[https://www.rfc-editor.org/rfc/rfc4505]. See
|
|
9
9
|
# Net::IMAP#authenticate.
|
|
10
10
|
class AnonymousAuthenticator
|
|
11
11
|
|
|
@@ -13,7 +13,7 @@ module Net
|
|
|
13
13
|
# characters in length.
|
|
14
14
|
#
|
|
15
15
|
# If it contains an "@" sign, the message must be a valid email address
|
|
16
|
-
# (+addr-spec+ from RFC-2822[https://
|
|
16
|
+
# (+addr-spec+ from RFC-2822[https://www.rfc-editor.org/rfc/rfc2822]).
|
|
17
17
|
# Email syntax is _not_ validated by AnonymousAuthenticator.
|
|
18
18
|
#
|
|
19
19
|
# Otherwise, it can be any UTF8 string which is permitted by the
|
|
@@ -25,7 +25,7 @@ module Net
|
|
|
25
25
|
# new(anonymous_message: "", **) -> authenticator
|
|
26
26
|
#
|
|
27
27
|
# Creates an Authenticator for the "+ANONYMOUS+" SASL mechanism, as
|
|
28
|
-
# specified in RFC-4505[https://
|
|
28
|
+
# specified in RFC-4505[https://www.rfc-editor.org/rfc/rfc4505]. To use
|
|
29
29
|
# this, see Net::IMAP#authenticate or your client's authentication
|
|
30
30
|
# method.
|
|
31
31
|
#
|
|
@@ -4,44 +4,76 @@ module Net
|
|
|
4
4
|
class IMAP
|
|
5
5
|
module SASL
|
|
6
6
|
|
|
7
|
-
#
|
|
7
|
+
# AuthenticationExchange is used internally by Net::IMAP#authenticate.
|
|
8
|
+
# But the API is still *experimental*, and may change.
|
|
8
9
|
#
|
|
9
10
|
# TODO: catch exceptions in #process and send #cancel_response.
|
|
10
11
|
# TODO: raise an error if the command succeeds after being canceled.
|
|
11
12
|
# TODO: use with more clients, to verify the API can accommodate them.
|
|
13
|
+
# TODO: pass ClientAdapter#service to SASL.authenticator
|
|
12
14
|
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
# ).authenticate
|
|
20
|
-
# end
|
|
21
|
-
#
|
|
22
|
-
# private
|
|
15
|
+
# An AuthenticationExchange represents a single attempt to authenticate
|
|
16
|
+
# a SASL client to a SASL server. It is created from a client adapter, a
|
|
17
|
+
# mechanism name, and a mechanism authenticator. When #authenticate is
|
|
18
|
+
# called, it will send the appropriate authenticate command to the server,
|
|
19
|
+
# returning the client response on success and raising an exception on
|
|
20
|
+
# failure.
|
|
23
21
|
#
|
|
24
|
-
#
|
|
22
|
+
# In most cases, the client will not need to use
|
|
23
|
+
# SASL::AuthenticationExchange directly at all. Instead, use
|
|
24
|
+
# SASL::ClientAdapter#authenticate. If customizations are needed, the
|
|
25
|
+
# custom client adapter is probably the best place for that code.
|
|
25
26
|
#
|
|
26
|
-
# Or delegate creation of the authenticator to ::build:
|
|
27
27
|
# def authenticate(...)
|
|
28
|
-
#
|
|
29
|
-
# .authenticate
|
|
28
|
+
# MyClient::SASLAdapter.new(self).authenticate(...)
|
|
30
29
|
# end
|
|
31
30
|
#
|
|
32
|
-
#
|
|
31
|
+
# SASL::ClientAdapter#authenticate delegates to ::authenticate, like so:
|
|
32
|
+
#
|
|
33
33
|
# def authenticate(...)
|
|
34
|
+
# sasl_adapter = MyClient::SASLAdapter.new(self)
|
|
34
35
|
# SASL::AuthenticationExchange.authenticate(sasl_adapter, ...)
|
|
35
36
|
# end
|
|
36
37
|
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
38
|
+
# ::authenticate simply delegates to ::build and #authenticate, like so:
|
|
39
|
+
#
|
|
40
|
+
# def authenticate(...)
|
|
41
|
+
# sasl_adapter = MyClient::SASLAdapter.new(self)
|
|
42
|
+
# SASL::AuthenticationExchange
|
|
43
|
+
# .build(sasl_adapter, ...)
|
|
44
|
+
# .authenticate
|
|
45
|
+
# end
|
|
46
|
+
#
|
|
47
|
+
# And ::build delegates to SASL.authenticator and ::new, like so:
|
|
48
|
+
#
|
|
49
|
+
# def authenticate(mechanism, ...)
|
|
50
|
+
# sasl_adapter = MyClient::SASLAdapter.new(self)
|
|
51
|
+
# authenticator = SASL.authenticator(mechanism, ...)
|
|
52
|
+
# SASL::AuthenticationExchange
|
|
53
|
+
# .new(sasl_adapter, mechanism, authenticator)
|
|
54
|
+
# .authenticate
|
|
55
|
+
# end
|
|
39
56
|
#
|
|
40
57
|
class AuthenticationExchange
|
|
41
58
|
# Convenience method for <tt>build(...).authenticate</tt>
|
|
59
|
+
#
|
|
60
|
+
# See also: SASL::ClientAdapter#authenticate
|
|
42
61
|
def self.authenticate(...) build(...).authenticate end
|
|
43
62
|
|
|
44
|
-
#
|
|
63
|
+
# Convenience method to combine the creation of a new authenticator and
|
|
64
|
+
# a new Authentication exchange.
|
|
65
|
+
#
|
|
66
|
+
# +client+ must be an instance of SASL::ClientAdapter.
|
|
67
|
+
#
|
|
68
|
+
# +mechanism+ must be a SASL mechanism name, as a string or symbol.
|
|
69
|
+
#
|
|
70
|
+
# +sasl_ir+ allows or disallows sending an "initial response", depending
|
|
71
|
+
# also on whether the server capabilities, mechanism authenticator, and
|
|
72
|
+
# client adapter all support it. Defaults to +true+.
|
|
73
|
+
#
|
|
74
|
+
# +mechanism+, +args+, +kwargs+, and +block+ are all forwarded to
|
|
75
|
+
# SASL.authenticator. Use the +registry+ kwarg to override the global
|
|
76
|
+
# SASL::Authenticators registry.
|
|
45
77
|
def self.build(client, mechanism, *args, sasl_ir: true, **kwargs, &block)
|
|
46
78
|
authenticator = SASL.authenticator(mechanism, *args, **kwargs, &block)
|
|
47
79
|
new(client, mechanism, authenticator, sasl_ir: sasl_ir)
|
|
@@ -51,7 +83,7 @@ module Net
|
|
|
51
83
|
|
|
52
84
|
def initialize(client, mechanism, authenticator, sasl_ir: true)
|
|
53
85
|
@client = client
|
|
54
|
-
@mechanism =
|
|
86
|
+
@mechanism = Authenticators.normalize_name(mechanism)
|
|
55
87
|
@authenticator = authenticator
|
|
56
88
|
@sasl_ir = sasl_ir
|
|
57
89
|
@processed = false
|
|
@@ -21,6 +21,10 @@ module Net::IMAP::SASL
|
|
|
21
21
|
# ScramSHA1Authenticator for examples.
|
|
22
22
|
class Authenticators
|
|
23
23
|
|
|
24
|
+
# Normalize the mechanism name as an uppercase string, with underscores
|
|
25
|
+
# converted to dashes.
|
|
26
|
+
def self.normalize_name(mechanism) -(mechanism.to_s.upcase.tr(?_, ?-)) end
|
|
27
|
+
|
|
24
28
|
# Create a new Authenticators registry.
|
|
25
29
|
#
|
|
26
30
|
# This class is usually not instantiated directly. Use SASL.authenticators
|
|
@@ -65,7 +69,6 @@ module Net::IMAP::SASL
|
|
|
65
69
|
# lazily loaded from <tt>Net::IMAP::SASL::#{name}Authenticator</tt> (case is
|
|
66
70
|
# preserved and non-alphanumeric characters are removed..
|
|
67
71
|
def add_authenticator(name, authenticator = nil)
|
|
68
|
-
key = -name.to_s.upcase.tr(?_, ?-)
|
|
69
72
|
authenticator ||= begin
|
|
70
73
|
class_name = "#{name.gsub(/[^a-zA-Z0-9]/, "")}Authenticator".to_sym
|
|
71
74
|
auth_class = nil
|
|
@@ -74,17 +77,18 @@ module Net::IMAP::SASL
|
|
|
74
77
|
auth_class.new(*creds, **props, &block)
|
|
75
78
|
}
|
|
76
79
|
end
|
|
80
|
+
key = Authenticators.normalize_name(name)
|
|
77
81
|
@authenticators[key] = authenticator
|
|
78
82
|
end
|
|
79
83
|
|
|
80
84
|
# Removes the authenticator registered for +name+
|
|
81
85
|
def remove_authenticator(name)
|
|
82
|
-
key =
|
|
86
|
+
key = Authenticators.normalize_name(name)
|
|
83
87
|
@authenticators.delete(key)
|
|
84
88
|
end
|
|
85
89
|
|
|
86
90
|
def mechanism?(name)
|
|
87
|
-
key =
|
|
91
|
+
key = Authenticators.normalize_name(name)
|
|
88
92
|
@authenticators.key?(key)
|
|
89
93
|
end
|
|
90
94
|
|
|
@@ -105,7 +109,7 @@ module Net::IMAP::SASL
|
|
|
105
109
|
# only. Protocol client users should see refer to their client's
|
|
106
110
|
# documentation, e.g. Net::IMAP#authenticate.
|
|
107
111
|
def authenticator(mechanism, ...)
|
|
108
|
-
key =
|
|
112
|
+
key = Authenticators.normalize_name(mechanism)
|
|
109
113
|
auth = @authenticators.fetch(key) do
|
|
110
114
|
raise ArgumentError, 'unknown auth type - "%s"' % key
|
|
111
115
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "forwardable"
|
|
4
|
+
|
|
3
5
|
module Net
|
|
4
6
|
class IMAP
|
|
5
7
|
module SASL
|
|
@@ -8,42 +10,76 @@ module Net
|
|
|
8
10
|
#
|
|
9
11
|
# TODO: use with more clients, to verify the API can accommodate them.
|
|
10
12
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# to match
|
|
13
|
+
# Represents the client to a SASL::AuthenticationExchange. By default,
|
|
14
|
+
# most methods simply delegate to #client. Clients should subclass
|
|
15
|
+
# SASL::ClientAdapter and override methods as needed to match the
|
|
16
|
+
# semantics of this API to their API.
|
|
14
17
|
#
|
|
15
|
-
#
|
|
16
|
-
# will probably need to override some methods. Additionally, subclasses
|
|
17
|
-
# may need to include a protocol adapter mixin, if the default
|
|
18
|
+
# Subclasses should also include a protocol adapter mixin when the default
|
|
18
19
|
# ProtocolAdapters::Generic isn't sufficient.
|
|
20
|
+
#
|
|
21
|
+
# === Protocol Requirements
|
|
22
|
+
#
|
|
23
|
+
# {RFC4422 §4}[https://www.rfc-editor.org/rfc/rfc4422.html#section-4]
|
|
24
|
+
# lists requirements for protocol specifications to offer SASL. Where
|
|
25
|
+
# possible, ClientAdapter delegates the handling of these requirements to
|
|
26
|
+
# SASL::ProtocolAdapters.
|
|
19
27
|
class ClientAdapter
|
|
28
|
+
extend Forwardable
|
|
29
|
+
|
|
20
30
|
include ProtocolAdapters::Generic
|
|
21
31
|
|
|
22
|
-
|
|
32
|
+
# The client that handles communication with the protocol server.
|
|
33
|
+
#
|
|
34
|
+
# Most ClientAdapter methods are simply delegated to #client by default.
|
|
35
|
+
attr_reader :client
|
|
23
36
|
|
|
24
37
|
# +command_proc+ can used to avoid exposing private methods on #client.
|
|
25
|
-
# It
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
38
|
+
# It's value is set by the block that is passed to ::new, and it is used
|
|
39
|
+
# by the default implementation of #run_command. Subclasses that
|
|
40
|
+
# override #run_command may use #command_proc for any other purpose they
|
|
41
|
+
# find useful.
|
|
29
42
|
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
43
|
+
# In the default implementation of #run_command, command_proc is called
|
|
44
|
+
# with the protocols authenticate +command+ name, the +mechanism+ name,
|
|
45
|
+
# an _optional_ +initial_response+ argument, and a +continuations+
|
|
46
|
+
# block. command_proc must run the protocol command with the arguments
|
|
47
|
+
# sent to it, _yield_ the payload of each continuation, respond to the
|
|
48
|
+
# continuation with the result of each _yield_, and _return_ the
|
|
49
|
+
# command's successful result. Non-successful results *MUST* raise
|
|
50
|
+
# an exception.
|
|
51
|
+
attr_reader :command_proc
|
|
52
|
+
|
|
53
|
+
# By default, this simply sets the #client and #command_proc attributes.
|
|
54
|
+
# Subclasses may override it, for example: to set the appropriate
|
|
55
|
+
# command_proc automatically.
|
|
32
56
|
def initialize(client, &command_proc)
|
|
33
57
|
@client, @command_proc = client, command_proc
|
|
34
58
|
end
|
|
35
59
|
|
|
36
|
-
#
|
|
60
|
+
# Attempt to authenticate #client to the server.
|
|
61
|
+
#
|
|
62
|
+
# By default, this simply delegates to
|
|
63
|
+
# AuthenticationExchange.authenticate.
|
|
37
64
|
def authenticate(...) AuthenticationExchange.authenticate(self, ...) end
|
|
38
65
|
|
|
39
|
-
|
|
40
|
-
|
|
66
|
+
##
|
|
67
|
+
# method: sasl_ir_capable?
|
|
68
|
+
# Do the protocol, server, and client all support an initial response?
|
|
69
|
+
def_delegator :client, :sasl_ir_capable?
|
|
41
70
|
|
|
42
|
-
|
|
43
|
-
|
|
71
|
+
##
|
|
72
|
+
# method: auth_capable?
|
|
73
|
+
# call-seq: auth_capable?(mechanism)
|
|
74
|
+
#
|
|
75
|
+
# Does the server advertise support for the +mechanism+?
|
|
76
|
+
def_delegator :client, :auth_capable?
|
|
44
77
|
|
|
45
|
-
#
|
|
46
|
-
#
|
|
78
|
+
# Calls command_proc with +command_name+ (see
|
|
79
|
+
# SASL::ProtocolAdapters::Generic#command_name),
|
|
80
|
+
# +mechanism+, +initial_response+, and a +continuations_handler+ block.
|
|
81
|
+
# The +initial_response+ is optional; when it's nil, it won't be sent to
|
|
82
|
+
# command_proc.
|
|
47
83
|
#
|
|
48
84
|
# Yields each continuation payload, responds to the server with the
|
|
49
85
|
# result of each yield, and returns the result. Non-successful results
|
|
@@ -51,21 +87,36 @@ module Net
|
|
|
51
87
|
# command to fail.
|
|
52
88
|
#
|
|
53
89
|
# Subclasses that override this may use #command_proc differently.
|
|
54
|
-
def run_command(mechanism, initial_response = nil, &
|
|
90
|
+
def run_command(mechanism, initial_response = nil, &continuations_handler)
|
|
55
91
|
command_proc or raise Error, "initialize with block or override"
|
|
56
92
|
args = [command_name, mechanism, initial_response].compact
|
|
57
|
-
command_proc.call(*args, &
|
|
93
|
+
command_proc.call(*args, &continuations_handler)
|
|
58
94
|
end
|
|
59
95
|
|
|
96
|
+
##
|
|
97
|
+
# method: host
|
|
98
|
+
# The hostname to which the client connected.
|
|
99
|
+
def_delegator :client, :host
|
|
100
|
+
|
|
101
|
+
##
|
|
102
|
+
# method: port
|
|
103
|
+
# The destination port to which the client connected.
|
|
104
|
+
def_delegator :client, :port
|
|
105
|
+
|
|
60
106
|
# Returns an array of server responses errors raised by run_command.
|
|
61
107
|
# Exceptions in this array won't drop the connection.
|
|
62
108
|
def response_errors; [] end
|
|
63
109
|
|
|
64
|
-
|
|
65
|
-
|
|
110
|
+
##
|
|
111
|
+
# method: drop_connection
|
|
112
|
+
# Drop the connection gracefully, sending a "LOGOUT" command as needed.
|
|
113
|
+
def_delegator :client, :drop_connection
|
|
114
|
+
|
|
115
|
+
##
|
|
116
|
+
# method: drop_connection!
|
|
117
|
+
# Drop the connection abruptly, closing the socket without logging out.
|
|
118
|
+
def_delegator :client, :drop_connection!
|
|
66
119
|
|
|
67
|
-
# Drop the connection abruptly.
|
|
68
|
-
def drop_connection!; client.drop_connection! end
|
|
69
120
|
end
|
|
70
121
|
end
|
|
71
122
|
end
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
# Authenticator for the "+CRAM-MD5+" SASL mechanism, specified in
|
|
4
|
-
# RFC2195[https://
|
|
4
|
+
# RFC2195[https://www.rfc-editor.org/rfc/rfc2195]. See Net::IMAP#authenticate.
|
|
5
5
|
#
|
|
6
6
|
# == Deprecated
|
|
7
7
|
#
|
|
8
8
|
# +CRAM-MD5+ is obsolete and insecure. It is included for compatibility with
|
|
9
9
|
# existing servers.
|
|
10
|
-
# {draft-ietf-sasl-crammd5-to-historic}[https://
|
|
10
|
+
# {draft-ietf-sasl-crammd5-to-historic}[https://www.rfc-editor.org/rfc/draft-ietf-sasl-crammd5-to-historic-00.html]
|
|
11
11
|
# recommends using +SCRAM-*+ or +PLAIN+ protected by TLS instead.
|
|
12
12
|
#
|
|
13
|
-
# Additionally, RFC8314[https://
|
|
13
|
+
# Additionally, RFC8314[https://www.rfc-editor.org/rfc/rfc8314] discourage the use
|
|
14
14
|
# of cleartext and recommends TLS version 1.2 or greater be used for all
|
|
15
15
|
# traffic. With TLS +CRAM-MD5+ is okay, but so is +PLAIN+
|
|
16
16
|
class Net::IMAP::SASL::CramMD5Authenticator
|
|
@@ -20,7 +20,7 @@ class Net::IMAP::SASL::CramMD5Authenticator
|
|
|
20
20
|
warn_deprecation: true,
|
|
21
21
|
**)
|
|
22
22
|
if warn_deprecation
|
|
23
|
-
warn "WARNING: CRAM-MD5 mechanism is deprecated."
|
|
23
|
+
warn "WARNING: CRAM-MD5 mechanism is deprecated.", category: :deprecated
|
|
24
24
|
end
|
|
25
25
|
require "digest/md5"
|
|
26
26
|
@user = authcid || username || user
|