net-imap 0.4.9.1 → 0.5.6

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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/BSDL +22 -0
  3. data/COPYING +56 -0
  4. data/Gemfile +12 -1
  5. data/LICENSE.txt +3 -22
  6. data/README.md +10 -4
  7. data/docs/styles.css +75 -14
  8. data/lib/net/imap/authenticators.rb +2 -2
  9. data/lib/net/imap/command_data.rb +61 -48
  10. data/lib/net/imap/config/attr_accessors.rb +75 -0
  11. data/lib/net/imap/config/attr_inheritance.rb +90 -0
  12. data/lib/net/imap/config/attr_type_coercion.rb +61 -0
  13. data/lib/net/imap/config.rb +470 -0
  14. data/lib/net/imap/data_encoding.rb +4 -4
  15. data/lib/net/imap/data_lite.rb +226 -0
  16. data/lib/net/imap/deprecated_client_options.rb +9 -6
  17. data/lib/net/imap/errors.rb +7 -1
  18. data/lib/net/imap/esearch_result.rb +180 -0
  19. data/lib/net/imap/fetch_data.rb +126 -47
  20. data/lib/net/imap/flags.rb +1 -1
  21. data/lib/net/imap/response_data.rb +126 -239
  22. data/lib/net/imap/response_parser/parser_utils.rb +11 -6
  23. data/lib/net/imap/response_parser.rb +188 -34
  24. data/lib/net/imap/sasl/anonymous_authenticator.rb +3 -3
  25. data/lib/net/imap/sasl/authentication_exchange.rb +52 -20
  26. data/lib/net/imap/sasl/authenticators.rb +8 -4
  27. data/lib/net/imap/sasl/client_adapter.rb +77 -26
  28. data/lib/net/imap/sasl/cram_md5_authenticator.rb +4 -4
  29. data/lib/net/imap/sasl/digest_md5_authenticator.rb +218 -56
  30. data/lib/net/imap/sasl/external_authenticator.rb +3 -3
  31. data/lib/net/imap/sasl/gs2_header.rb +7 -7
  32. data/lib/net/imap/sasl/login_authenticator.rb +4 -3
  33. data/lib/net/imap/sasl/oauthbearer_authenticator.rb +6 -6
  34. data/lib/net/imap/sasl/plain_authenticator.rb +7 -7
  35. data/lib/net/imap/sasl/protocol_adapters.rb +60 -4
  36. data/lib/net/imap/sasl/scram_authenticator.rb +8 -8
  37. data/lib/net/imap/sasl.rb +8 -5
  38. data/lib/net/imap/sasl_adapter.rb +0 -1
  39. data/lib/net/imap/search_result.rb +4 -8
  40. data/lib/net/imap/sequence_set.rb +239 -88
  41. data/lib/net/imap/stringprep/nameprep.rb +1 -1
  42. data/lib/net/imap/stringprep/trace.rb +4 -4
  43. data/lib/net/imap/uidplus_data.rb +244 -0
  44. data/lib/net/imap/vanished_data.rb +56 -0
  45. data/lib/net/imap.rb +1012 -322
  46. data/net-imap.gemspec +4 -7
  47. data/rakelib/benchmarks.rake +1 -1
  48. data/rakelib/rfcs.rake +2 -0
  49. data/rakelib/string_prep_tables_generator.rb +2 -0
  50. data/sample/net-imap.rb +167 -0
  51. metadata +16 -40
  52. data/.github/dependabot.yml +0 -6
  53. data/.github/workflows/pages.yml +0 -46
  54. data/.github/workflows/test.yml +0 -31
  55. data/.gitignore +0 -12
@@ -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
- # An abstract base class for implementing a SASL authentication exchange.
12
- # Different clients will each have their own adapter subclass, overridden
13
- # to match their needs.
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
- # Although the default implementations _may_ be sufficient, subclasses
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
- attr_reader :client, :command_proc
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 should run a command with the arguments sent to it, yield each
26
- # continuation payload, respond to the server with the result of each
27
- # yield, and return the result. Non-successful results *MUST* raise an
28
- # exception. Exceptions in the block *MUST* cause the command to fail.
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
- # Subclasses that override #run_command may use #command_proc for
31
- # other purposes.
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
- # Delegates to AuthenticationExchange.authenticate.
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
- # Do the protocol and server both support an initial response?
40
- def sasl_ir_capable?; client.sasl_ir_capable? end
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
- # Does the server advertise support for the mechanism?
43
- def auth_capable?(mechanism); client.auth_capable?(mechanism) end
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
- # Runs the authenticate command with +mechanism+ and +initial_response+.
46
- # When +initial_response+ is nil, an initial response must NOT be sent.
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, &block)
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, &block)
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
- # Drop the connection gracefully.
65
- def drop_connection; client.drop_connection end
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://tools.ietf.org/html/rfc2195]. See Net::IMAP#authenticate.
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://tools.ietf.org/html/draft-ietf-sasl-crammd5-to-historic-00.html]
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://tools.ietf.org/html/rfc8314] discourage the use
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." # TODO: recommend SCRAM
23
+ warn "WARNING: CRAM-MD5 mechanism is deprecated.", category: :deprecated
24
24
  end
25
25
  require "digest/md5"
26
26
  @user = authcid || username || user
@@ -1,25 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Net::IMAP authenticator for the "`DIGEST-MD5`" SASL mechanism type, specified
4
- # in RFC-2831[https://tools.ietf.org/html/rfc2831]. See Net::IMAP#authenticate.
3
+ # Net::IMAP authenticator for the +DIGEST-MD5+ SASL mechanism type, specified
4
+ # in RFC-2831[https://www.rfc-editor.org/rfc/rfc2831]. See Net::IMAP#authenticate.
5
5
  #
6
6
  # == Deprecated
7
7
  #
8
8
  # "+DIGEST-MD5+" has been deprecated by
9
- # RFC-6331[https://tools.ietf.org/html/rfc6331] and should not be relied on for
9
+ # RFC-6331[https://www.rfc-editor.org/rfc/rfc6331] and should not be relied on for
10
10
  # security. It is included for compatibility with existing servers.
11
11
  class Net::IMAP::SASL::DigestMD5Authenticator
12
+ DataFormatError = Net::IMAP::DataFormatError
13
+ ResponseParseError = Net::IMAP::ResponseParseError
14
+ private_constant :DataFormatError, :ResponseParseError
15
+
12
16
  STAGE_ONE = :stage_one
13
17
  STAGE_TWO = :stage_two
14
18
  STAGE_DONE = :stage_done
15
19
  private_constant :STAGE_ONE, :STAGE_TWO, :STAGE_DONE
16
20
 
21
+ # Directives which must not have multiples. The RFC states:
22
+ # >>>
23
+ # This directive may appear at most once; if multiple instances are present,
24
+ # the client should abort the authentication exchange.
25
+ NO_MULTIPLES = %w[nonce stale maxbuf charset algorithm].freeze
26
+
27
+ # Required directives which must occur exactly once. The RFC states: >>>
28
+ # This directive is required and MUST appear exactly once; if not present,
29
+ # or if multiple instances are present, the client should abort the
30
+ # authentication exchange.
31
+ REQUIRED = %w[nonce algorithm].freeze
32
+
33
+ # Directives which are composed of one or more comma delimited tokens
34
+ QUOTED_LISTABLE = %w[qop cipher].freeze
35
+
36
+ private_constant :NO_MULTIPLES, :REQUIRED, :QUOTED_LISTABLE
37
+
17
38
  # Authentication identity: the identity that matches the #password.
18
39
  #
19
- # RFC-2831[https://tools.ietf.org/html/rfc2831] uses the term +username+.
40
+ # RFC-2831[https://www.rfc-editor.org/rfc/rfc2831] uses the term +username+.
20
41
  # "Authentication identity" is the generic term used by
21
- # RFC-4422[https://tools.ietf.org/html/rfc4422].
22
- # RFC-4616[https://tools.ietf.org/html/rfc4616] and many later RFCs abbreviate
42
+ # RFC-4422[https://www.rfc-editor.org/rfc/rfc4422].
43
+ # RFC-4616[https://www.rfc-editor.org/rfc/rfc4616] and many later RFCs abbreviate
23
44
  # this to +authcid+.
24
45
  attr_reader :username
25
46
  alias authcid username
@@ -42,6 +63,59 @@ class Net::IMAP::SASL::DigestMD5Authenticator
42
63
  #
43
64
  attr_reader :authzid
44
65
 
66
+ # A namespace or collection of identities which contains +username+.
67
+ #
68
+ # Used by DIGEST-MD5, GSS-API, and NTLM. This is often a domain name that
69
+ # contains the name of the host performing the authentication.
70
+ #
71
+ # <em>Defaults to the last realm in the server-provided list of
72
+ # realms.</em>
73
+ attr_reader :realm
74
+
75
+ # Fully qualified canonical DNS host name for the requested service.
76
+ #
77
+ # <em>Defaults to #realm.</em>
78
+ attr_reader :host
79
+
80
+ # The service protocol, a
81
+ # {registered GSSAPI service name}[https://www.iana.org/assignments/gssapi-service-names/gssapi-service-names.xhtml],
82
+ # e.g. "imap", "ldap", or "xmpp".
83
+ #
84
+ # For Net::IMAP, the default is "imap" and should not be overridden. This
85
+ # must be set appropriately to use authenticators in other protocols.
86
+ #
87
+ # If an IANA-registered name isn't available, GSS-API
88
+ # (RFC-2743[https://www.rfc-editor.org/rfc/rfc2743]) allows the generic name
89
+ # "host".
90
+ attr_reader :service
91
+
92
+ # The generic server name when the server is replicated.
93
+ #
94
+ # +service_name+ will be ignored when it is +nil+ or identical to +host+.
95
+ #
96
+ # From RFC-2831[https://www.rfc-editor.org/rfc/rfc2831]:
97
+ # >>>
98
+ # The service is considered to be replicated if the client's
99
+ # service-location process involves resolution using standard DNS lookup
100
+ # operations, and if these operations involve DNS records (such as SRV, or
101
+ # MX) which resolve one DNS name into a set of other DNS names. In this
102
+ # case, the initial name used by the client is the "serv-name", and the
103
+ # final name is the "host" component.
104
+ attr_reader :service_name
105
+
106
+ # Parameters sent by the server are stored in this hash.
107
+ attr_reader :sparams
108
+
109
+ # The charset sent by the server. "UTF-8" (case insensitive) is the only
110
+ # allowed value. +nil+ should be interpreted as ISO 8859-1.
111
+ attr_reader :charset
112
+
113
+ # nonce sent by the server
114
+ attr_reader :nonce
115
+
116
+ # qop-options sent by the server
117
+ attr_reader :qop
118
+
45
119
  # :call-seq:
46
120
  # new(username, password, authzid = nil, **options) -> authenticator
47
121
  # new(username:, password:, authzid: nil, **options) -> authenticator
@@ -64,27 +138,59 @@ class Net::IMAP::SASL::DigestMD5Authenticator
64
138
  # When +authzid+ is not set, the server should derive the authorization
65
139
  # identity from the authentication identity.
66
140
  #
141
+ # * _optional_ #realm — A namespace for the #username, e.g. a domain.
142
+ # <em>Defaults to the last realm in the server-provided realms list.</em>
143
+ # * _optional_ #host — FQDN for requested service.
144
+ # <em>Defaults to</em> #realm.
145
+ # * _optional_ #service_name — The generic host name when the server is
146
+ # replicated.
147
+ # * _optional_ #service — the registered service protocol. E.g. "imap",
148
+ # "smtp", "ldap", "xmpp".
149
+ # <em>For Net::IMAP, this defaults to "imap".</em>
150
+ #
67
151
  # * _optional_ +warn_deprecation+ — Set to +false+ to silence the warning.
68
152
  #
69
153
  # Any other keyword arguments are silently ignored.
70
154
  def initialize(user = nil, pass = nil, authz = nil,
71
155
  username: nil, password: nil, authzid: nil,
72
156
  authcid: nil, secret: nil,
157
+ realm: nil, service: "imap", host: nil, service_name: nil,
73
158
  warn_deprecation: true, **)
74
159
  username = authcid || username || user or
75
160
  raise ArgumentError, "missing username (authcid)"
76
161
  password ||= secret || pass or raise ArgumentError, "missing password"
77
162
  authzid ||= authz
78
163
  if warn_deprecation
79
- warn "WARNING: DIGEST-MD5 SASL mechanism was deprecated by RFC6331."
80
- # TODO: recommend SCRAM instead.
164
+ warn("WARNING: DIGEST-MD5 SASL mechanism was deprecated by RFC6331.",
165
+ category: :deprecated)
81
166
  end
167
+
82
168
  require "digest/md5"
169
+ require "securerandom"
83
170
  require "strscan"
84
171
  @username, @password, @authzid = username, password, authzid
172
+ @realm = realm
173
+ @host = host
174
+ @service = service
175
+ @service_name = service_name
85
176
  @nc, @stage = {}, STAGE_ONE
86
177
  end
87
178
 
179
+ # From RFC-2831[https://www.rfc-editor.org/rfc/rfc2831]:
180
+ # >>>
181
+ # Indicates the principal name of the service with which the client wishes
182
+ # to connect, formed from the serv-type, host, and serv-name. For
183
+ # example, the FTP service on "ftp.example.com" would have a "digest-uri"
184
+ # value of "ftp/ftp.example.com"; the SMTP server from the example above
185
+ # would have a "digest-uri" value of "smtp/mail3.example.com/example.com".
186
+ def digest_uri
187
+ if service_name && service_name != host
188
+ "#{service}/#{host}/#{service_name}"
189
+ else
190
+ "#{service}/#{host}"
191
+ end
192
+ end
193
+
88
194
  def initial_response?; false end
89
195
 
90
196
  # Responds to server challenge in two stages.
@@ -92,78 +198,134 @@ class Net::IMAP::SASL::DigestMD5Authenticator
92
198
  case @stage
93
199
  when STAGE_ONE
94
200
  @stage = STAGE_TWO
95
- sparams = {}
96
- c = StringScanner.new(challenge)
97
- while c.scan(/(?:\s*,)?\s*(\w+)=("(?:[^\\"]|\\.)*"|[^,]+)\s*/)
98
- k, v = c[1], c[2]
99
- if v =~ /^"(.*)"$/
100
- v = $1
101
- if v =~ /,/
102
- v = v.split(',')
103
- end
104
- end
105
- sparams[k] = v
106
- end
201
+ @sparams = parse_challenge(challenge)
202
+ @qop = sparams.key?("qop") ? ["auth"] : sparams["qop"].flatten
203
+ @nonce = sparams["nonce"] &.first
204
+ @charset = sparams["charset"]&.first
205
+ @realm ||= sparams["realm"] &.last
206
+ @host ||= realm
107
207
 
108
- raise Net::IMAP::DataFormatError, "Bad Challenge: '#{challenge}'" unless c.eos? and sparams['qop']
109
- raise Net::IMAP::Error, "Server does not support auth (qop = #{sparams['qop'].join(',')})" unless sparams['qop'].include?("auth")
208
+ if !qop.include?("auth")
209
+ raise DataFormatError, "Server does not support auth (qop = %p)" % [
210
+ sparams["qop"]
211
+ ]
212
+ elsif (emptykey = REQUIRED.find { sparams[_1].empty? })
213
+ raise DataFormatError, "Server didn't send %s (%p)" % [emptykey, challenge]
214
+ elsif (multikey = NO_MULTIPLES.find { sparams[_1].length > 1 })
215
+ raise DataFormatError, "Server sent multiple %s (%p)" % [multikey, challenge]
216
+ end
110
217
 
111
218
  response = {
112
- :nonce => sparams['nonce'],
113
- :username => @username,
114
- :realm => sparams['realm'],
115
- :cnonce => Digest::MD5.hexdigest("%.15f:%.15f:%d" % [Time.now.to_f, rand, Process.pid.to_s]),
116
- :'digest-uri' => 'imap/' + sparams['realm'],
117
- :qop => 'auth',
118
- :maxbuf => 65535,
119
- :nc => "%08d" % nc(sparams['nonce']),
120
- :charset => sparams['charset'],
219
+ nonce: nonce,
220
+ username: username,
221
+ realm: realm,
222
+ cnonce: SecureRandom.base64(32),
223
+ "digest-uri": digest_uri,
224
+ qop: "auth",
225
+ maxbuf: 65535,
226
+ nc: "%08d" % nc(nonce),
227
+ charset: charset,
121
228
  }
122
229
 
123
230
  response[:authzid] = @authzid unless @authzid.nil?
124
231
 
125
- # now, the real thing
126
- a0 = Digest::MD5.digest( [ response.values_at(:username, :realm), @password ].join(':') )
127
-
128
- a1 = [ a0, response.values_at(:nonce,:cnonce) ].join(':')
129
- a1 << ':' + response[:authzid] unless response[:authzid].nil?
130
-
131
- a2 = "AUTHENTICATE:" + response[:'digest-uri']
132
- a2 << ":00000000000000000000000000000000" if response[:qop] and response[:qop] =~ /^auth-(?:conf|int)$/
133
-
134
- response[:response] = Digest::MD5.hexdigest(
135
- [
136
- Digest::MD5.hexdigest(a1),
137
- response.values_at(:nonce, :nc, :cnonce, :qop),
138
- Digest::MD5.hexdigest(a2)
139
- ].join(':')
140
- )
141
-
142
- return response.keys.map {|key| qdval(key.to_s, response[key]) }.join(',')
232
+ response[:response] = response_value(response)
233
+ format_response(response)
143
234
  when STAGE_TWO
144
235
  @stage = STAGE_DONE
145
- # if at the second stage, return an empty string
146
- if challenge =~ /rspauth=/
147
- return ''
148
- else
149
- raise ResponseParseError, challenge
150
- end
236
+ raise ResponseParseError, challenge unless challenge =~ /rspauth=/
237
+ "" # if at the second stage, return an empty string
151
238
  else
152
239
  raise ResponseParseError, challenge
153
240
  end
241
+ rescue => error
242
+ @stage = error
243
+ raise
154
244
  end
155
245
 
156
246
  def done?; @stage == STAGE_DONE end
157
247
 
158
248
  private
159
249
 
250
+ LWS = /[\r\n \t]*/n # less strict than RFC, more strict than '\s'
251
+ TOKEN = /[^\x00-\x20\x7f()<>@,;:\\"\/\[\]?={}]+/n
252
+ QUOTED_STR = /"(?: [\t\x20-\x7e&&[^"]] | \\[\x00-\x7f] )*"/nx
253
+ LIST_DELIM = /(?:#{LWS} , )+ #{LWS}/nx
254
+ AUTH_PARAM = /
255
+ (#{TOKEN}) #{LWS} = #{LWS} (#{QUOTED_STR} | #{TOKEN}) #{LIST_DELIM}?
256
+ /nx
257
+ private_constant :LWS, :TOKEN, :QUOTED_STR, :LIST_DELIM, :AUTH_PARAM
258
+
259
+ def parse_challenge(challenge)
260
+ sparams = Hash.new {|h, k| h[k] = [] }
261
+ c = StringScanner.new(challenge)
262
+ c.skip LIST_DELIM
263
+ while c.scan AUTH_PARAM
264
+ k, v = c[1], c[2]
265
+ k = k.downcase
266
+ if v =~ /\A"(.*)"\z/mn
267
+ v = $1.gsub(/\\(.)/mn, '\1')
268
+ v = split_quoted_list(v, challenge) if QUOTED_LISTABLE.include? k
269
+ end
270
+ sparams[k] << v
271
+ end
272
+ if !c.eos?
273
+ raise DataFormatError, "Unparsable challenge: %p" % [challenge]
274
+ elsif sparams.empty?
275
+ raise DataFormatError, "Empty challenge: %p" % [challenge]
276
+ end
277
+ sparams
278
+ end
279
+
280
+ def split_quoted_list(value, challenge)
281
+ value.split(LIST_DELIM).reject(&:empty?).tap do
282
+ _1.any? or raise DataFormatError, "Bad Challenge: %p" % [challenge]
283
+ end
284
+ end
285
+
160
286
  def nc(nonce)
161
287
  if @nc.has_key? nonce
162
288
  @nc[nonce] = @nc[nonce] + 1
163
289
  else
164
290
  @nc[nonce] = 1
165
291
  end
166
- return @nc[nonce]
292
+ end
293
+
294
+ def response_value(response)
295
+ a1 = compute_a1(response)
296
+ a2 = compute_a2(response)
297
+ Digest::MD5.hexdigest(
298
+ [
299
+ Digest::MD5.hexdigest(a1),
300
+ response.values_at(:nonce, :nc, :cnonce, :qop),
301
+ Digest::MD5.hexdigest(a2)
302
+ ].join(":")
303
+ )
304
+ end
305
+
306
+ def compute_a0(response)
307
+ Digest::MD5.digest(
308
+ [ response.values_at(:username, :realm), password ].join(":")
309
+ )
310
+ end
311
+
312
+ def compute_a1(response)
313
+ a0 = compute_a0(response)
314
+ a1 = [ a0, response.values_at(:nonce, :cnonce) ].join(":")
315
+ a1 << ":#{response[:authzid]}" unless response[:authzid].nil?
316
+ a1
317
+ end
318
+
319
+ def compute_a2(response)
320
+ a2 = "AUTHENTICATE:#{response[:"digest-uri"]}"
321
+ if response[:qop] and response[:qop] =~ /^auth-(?:conf|int)$/
322
+ a2 << ":00000000000000000000000000000000"
323
+ end
324
+ a2
325
+ end
326
+
327
+ def format_response(response)
328
+ response.map {|k, v| qdval(k.to_s, v) }.join(",")
167
329
  end
168
330
 
169
331
  # some responses need quoting
@@ -5,11 +5,11 @@ module Net
5
5
  module SASL
6
6
 
7
7
  # Authenticator for the "+EXTERNAL+" SASL mechanism, as specified by
8
- # RFC-4422[https://tools.ietf.org/html/rfc4422]. See
8
+ # RFC-4422[https://www.rfc-editor.org/rfc/rfc4422]. See
9
9
  # Net::IMAP#authenticate.
10
10
  #
11
11
  # The EXTERNAL mechanism requests that the server use client credentials
12
- # established external to SASL, for example by TLS certificate or IPsec.
12
+ # established external to SASL, for example by TLS certificate or IPSec.
13
13
  class ExternalAuthenticator
14
14
 
15
15
  # Authorization identity: an identity to act as or on behalf of. The
@@ -33,7 +33,7 @@ module Net
33
33
  # new(username = nil, **) -> authenticator
34
34
  #
35
35
  # Creates an Authenticator for the "+EXTERNAL+" SASL mechanism, as
36
- # specified in RFC-4422[https://tools.ietf.org/html/rfc4422]. To use
36
+ # specified in RFC-4422[https://www.rfc-editor.org/rfc/rfc4422]. To use
37
37
  # this, see Net::IMAP#authenticate or your client's authentication
38
38
  # method.
39
39
  #
@@ -5,15 +5,15 @@ module Net
5
5
  module SASL
6
6
 
7
7
  # Originally defined for the GS2 mechanism family in
8
- # RFC5801[https://tools.ietf.org/html/rfc5801],
8
+ # RFC5801[https://www.rfc-editor.org/rfc/rfc5801],
9
9
  # several different mechanisms start with a GS2 header:
10
- # * +GS2-*+ --- RFC5801[https://tools.ietf.org/html/rfc5801]
11
- # * +SCRAM-*+ --- RFC5802[https://tools.ietf.org/html/rfc5802]
10
+ # * +GS2-*+ --- RFC5801[https://www.rfc-editor.org/rfc/rfc5801]
11
+ # * +SCRAM-*+ --- RFC5802[https://www.rfc-editor.org/rfc/rfc5802]
12
12
  # (ScramAuthenticator)
13
- # * +SAML20+ --- RFC6595[https://tools.ietf.org/html/rfc6595]
14
- # * +OPENID20+ --- RFC6616[https://tools.ietf.org/html/rfc6616]
15
- # * +OAUTH10A+ --- RFC7628[https://tools.ietf.org/html/rfc7628]
16
- # * +OAUTHBEARER+ --- RFC7628[https://tools.ietf.org/html/rfc7628]
13
+ # * +SAML20+ --- RFC6595[https://www.rfc-editor.org/rfc/rfc6595]
14
+ # * +OPENID20+ --- RFC6616[https://www.rfc-editor.org/rfc/rfc6616]
15
+ # * +OAUTH10A+ --- RFC7628[https://www.rfc-editor.org/rfc/rfc7628]
16
+ # * +OAUTHBEARER+ --- RFC7628[https://www.rfc-editor.org/rfc/rfc7628]
17
17
  # (OAuthBearerAuthenticator)
18
18
  #
19
19
  # Classes that include this module must implement +#authzid+.
@@ -3,9 +3,9 @@
3
3
  # Authenticator for the "+LOGIN+" SASL mechanism. See Net::IMAP#authenticate.
4
4
  #
5
5
  # +LOGIN+ authentication sends the password in cleartext.
6
- # RFC3501[https://tools.ietf.org/html/rfc3501] encourages servers to disable
6
+ # RFC3501[https://www.rfc-editor.org/rfc/rfc3501] encourages servers to disable
7
7
  # cleartext authentication until after TLS has been negotiated.
8
- # RFC8314[https://tools.ietf.org/html/rfc8314] recommends TLS version 1.2 or
8
+ # RFC8314[https://www.rfc-editor.org/rfc/rfc8314] recommends TLS version 1.2 or
9
9
  # greater be used for all traffic, and deprecate cleartext access ASAP. +LOGIN+
10
10
  # can be secured by TLS encryption.
11
11
  #
@@ -29,7 +29,8 @@ class Net::IMAP::SASL::LoginAuthenticator
29
29
  warn_deprecation: true,
30
30
  **)
31
31
  if warn_deprecation
32
- warn "WARNING: LOGIN SASL mechanism is deprecated. Use PLAIN instead."
32
+ warn "WARNING: LOGIN SASL mechanism is deprecated. Use PLAIN instead.",
33
+ category: :deprecated
33
34
  end
34
35
  @user = authcid || username || user
35
36
  @password = password || secret || pass
@@ -7,7 +7,7 @@ module Net
7
7
  module SASL
8
8
 
9
9
  # Abstract base class for the SASL mechanisms defined in
10
- # RFC7628[https://tools.ietf.org/html/rfc7628]:
10
+ # RFC7628[https://www.rfc-editor.org/rfc/rfc7628]:
11
11
  # * OAUTHBEARER[rdoc-ref:OAuthBearerAuthenticator]
12
12
  # (OAuthBearerAuthenticator)
13
13
  # * OAUTH10A
@@ -52,7 +52,7 @@ module Net
52
52
  # this may hold information about the failure reason, as JSON.
53
53
  attr_reader :last_server_response
54
54
 
55
- # Creates an RFC7628[https://tools.ietf.org/html/rfc7628] OAuth
55
+ # Creates an RFC7628[https://www.rfc-editor.org/rfc/rfc7628] OAuth
56
56
  # authenticator.
57
57
  #
58
58
  # ==== Parameters
@@ -126,12 +126,12 @@ module Net
126
126
  end
127
127
 
128
128
  # Authenticator for the "+OAUTHBEARER+" SASL mechanism, specified in
129
- # RFC7628[https://tools.ietf.org/html/rfc7628]. Authenticates using OAuth
130
- # 2.0 bearer tokens, as described in
131
- # RFC6750[https://tools.ietf.org/html/rfc6750]. Use via
129
+ # RFC7628[https://www.rfc-editor.org/rfc/rfc7628]. Authenticates using
130
+ # OAuth 2.0 bearer tokens, as described in
131
+ # RFC6750[https://www.rfc-editor.org/rfc/rfc6750]. Use via
132
132
  # Net::IMAP#authenticate.
133
133
  #
134
- # RFC6750[https://tools.ietf.org/html/rfc6750] requires Transport Layer
134
+ # RFC6750[https://www.rfc-editor.org/rfc/rfc6750] requires Transport Layer
135
135
  # Security (TLS) to secure the protocol interaction between the client and
136
136
  # the resource server. TLS _MUST_ be used for +OAUTHBEARER+ to protect
137
137
  # the bearer token.