|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Net |
| 4 | + class IMAP < Protocol |
| 5 | + module SASL |
| 6 | + |
| 7 | + # Authenticator for the "+LOGIN+" SASL mechanism. See Net::IMAP#authenticate. |
| 8 | + # |
| 9 | + # +LOGIN+ authentication sends the password in cleartext. |
| 10 | + # RFC3501[https://tools.ietf.org/html/rfc3501] encourages servers to disable |
| 11 | + # cleartext authentication until after TLS has been negotiated. |
| 12 | + # RFC8314[https://tools.ietf.org/html/rfc8314] recommends TLS version 1.2 or |
| 13 | + # greater be used for all traffic, and deprecate cleartext access ASAP. +LOGIN+ |
| 14 | + # can be secured by TLS encryption. |
| 15 | + # |
| 16 | + # == Deprecated |
| 17 | + # |
| 18 | + # The {SASL mechanisms |
| 19 | + # registry}[https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml] |
| 20 | + # marks "LOGIN" as obsoleted in favor of "PLAIN". It is included here for |
| 21 | + # compatibility with existing servers. See |
| 22 | + # {draft-murchison-sasl-login}[https://www.iana.org/go/draft-murchison-sasl-login] |
| 23 | + # for both specification and deprecation. |
| 24 | + class LoginAuthenticator |
| 25 | + |
| 26 | + STATE_USER = :USER |
| 27 | + STATE_PASSWORD = :PASSWORD |
| 28 | + STATE_DONE = :DONE |
| 29 | + private_constant :STATE_USER, :STATE_PASSWORD, :STATE_DONE |
| 30 | + |
| 31 | + def initialize(user, password, warn_deprecation: true, **_ignored) |
| 32 | + if warn_deprecation |
| 33 | + warn "WARNING: LOGIN SASL mechanism is deprecated. Use PLAIN instead." |
| 34 | + end |
| 35 | + @user = user |
| 36 | + @password = password |
| 37 | + @state = STATE_USER |
| 38 | + end |
| 39 | + |
| 40 | + def process(data) |
| 41 | + case @state |
| 42 | + when STATE_USER |
| 43 | + @state = STATE_PASSWORD |
| 44 | + @user |
| 45 | + when STATE_PASSWORD |
| 46 | + @state = STATE_DONE |
| 47 | + @password |
| 48 | + when STATE_DONE |
| 49 | + raise ResponseParseError, data |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + def done?; @state == STATE_DONE end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + LoginAuthenticator = SASL::LoginAuthenticator # :nodoc: |
| 58 | + deprecate_constant :LoginAuthenticator |
| 59 | + |
| 60 | + end |
| 61 | +end |
0 commit comments