|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "net/imap" |
| 4 | +require "test/unit" |
| 5 | +require_relative "fake_server" |
| 6 | + |
| 7 | +class IMAPAuthenticateTest < Net::IMAP::TestCase |
| 8 | + include Net::IMAP::FakeServer::TestHelper |
| 9 | + |
| 10 | + test("#authenticate sends an initial response " \ |
| 11 | + "when supported by both the mechanism and the server") do |
| 12 | + with_fake_server( |
| 13 | + preauth: false, cleartext_auth: true, sasl_ir: true |
| 14 | + ) do |server, imap| |
| 15 | + imap.authenticate("PLAIN", "test_user", "test-password") |
| 16 | + cmd = server.commands.pop |
| 17 | + assert_equal "AUTHENTICATE", cmd.name |
| 18 | + assert_equal(["PLAIN", ["\x00test_user\x00test-password"].pack("m0")], |
| 19 | + cmd.args) |
| 20 | + assert_empty server.commands |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + test("#authenticate sends '=' as the initial reponse " \ |
| 25 | + "when the initial response is an empty string") do |
| 26 | + with_fake_server( |
| 27 | + preauth: false, cleartext_auth: true, |
| 28 | + sasl_ir: true, sasl_mechanisms: %i[EXTERNAL], |
| 29 | + ) do |server, imap| |
| 30 | + server.on "AUTHENTICATE" do |cmd| |
| 31 | + server.state.authenticate(server.config.user) |
| 32 | + cmd.done_ok |
| 33 | + end |
| 34 | + imap.authenticate("EXTERNAL") |
| 35 | + cmd = server.commands.pop |
| 36 | + assert_equal "AUTHENTICATE", cmd.name |
| 37 | + assert_equal %w[EXTERNAL =], cmd.args |
| 38 | + assert_empty server.commands rescue pp server.commands.pop |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + test("#authenticate never sends an initial response " \ |
| 43 | + "when the server doesn't explicitly support the mechanism") do |
| 44 | + with_fake_server( |
| 45 | + preauth: false, cleartext_auth: true, |
| 46 | + sasl_ir: true, sasl_mechanisms: %i[SCRAM-SHA-1 SCRAM-SHA-256], |
| 47 | + ) do |server, imap| |
| 48 | + imap.authenticate("PLAIN", "test_user", "test-password") |
| 49 | + cmd, cont = 2.times.map { server.commands.pop } |
| 50 | + assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args] |
| 51 | + assert_equal(["\x00test_user\x00test-password"].pack("m0"), |
| 52 | + cont[:continuation].strip) |
| 53 | + assert_empty server.commands |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + test("#authenticate never sends an initial response " \ |
| 58 | + "when the server isn't capable") do |
| 59 | + with_fake_server( |
| 60 | + preauth: false, cleartext_auth: true, sasl_ir: false |
| 61 | + ) do |server, imap| |
| 62 | + imap.authenticate("PLAIN", "test_user", "test-password") |
| 63 | + cmd, cont = 2.times.map { server.commands.pop } |
| 64 | + assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args] |
| 65 | + assert_equal(["\x00test_user\x00test-password"].pack("m0"), |
| 66 | + cont[:continuation].strip) |
| 67 | + assert_empty server.commands |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + test("#authenticate never sends an initial response " \ |
| 72 | + "when sasl_ir: false") do |
| 73 | + [true, false].each do |server_support| |
| 74 | + with_fake_server( |
| 75 | + preauth: false, cleartext_auth: true, sasl_ir: server_support |
| 76 | + ) do |server, imap| |
| 77 | + imap.authenticate("PLAIN", "test_user", "test-password", sasl_ir: false) |
| 78 | + cmd, cont = 2.times.map { server.commands.pop } |
| 79 | + assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args] |
| 80 | + assert_equal(["\x00test_user\x00test-password"].pack("m0"), |
| 81 | + cont[:continuation].strip) |
| 82 | + assert_empty server.commands |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + test("#authenticate never sends an initial response " \ |
| 88 | + "when config.sasl_ir: false") do |
| 89 | + [true, false].each do |server_support| |
| 90 | + with_fake_server( |
| 91 | + preauth: false, cleartext_auth: true, sasl_ir: server_support |
| 92 | + ) do |server, imap| |
| 93 | + imap.config.sasl_ir = false |
| 94 | + imap.authenticate("PLAIN", "test_user", "test-password") |
| 95 | + cmd, cont = 2.times.map { server.commands.pop } |
| 96 | + assert_equal %w[AUTHENTICATE PLAIN], [cmd.name, *cmd.args] |
| 97 | + assert_equal(["\x00test_user\x00test-password"].pack("m0"), |
| 98 | + cont[:continuation].strip) |
| 99 | + assert_empty server.commands |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + test("#authenticate never sends an initial response " \ |
| 105 | + "when the mechanism does not support client-first") do |
| 106 | + with_fake_server( |
| 107 | + preauth: false, cleartext_auth: true, |
| 108 | + sasl_ir: true, sasl_mechanisms: %i[DIGEST-MD5] |
| 109 | + ) do |server, imap| |
| 110 | + server.on "AUTHENTICATE" do |cmd| |
| 111 | + response_b64 = cmd.request_continuation( |
| 112 | + [ |
| 113 | + %w[ |
| 114 | + realm="somerealm" |
| 115 | + nonce="OA6MG9tEQGm2hh" |
| 116 | + qop="auth" |
| 117 | + charset=utf-8 |
| 118 | + algorithm=md5-sess |
| 119 | + ].join(",") |
| 120 | + ].pack("m0") |
| 121 | + ) |
| 122 | + state.commands << {continuation: response_b64} |
| 123 | + response_b64 = cmd.request_continuation(["rspauth="].pack("m0")) |
| 124 | + state.commands << {continuation: response_b64} |
| 125 | + server.state.authenticate(server.config.user) |
| 126 | + cmd.done_ok |
| 127 | + end |
| 128 | + imap.authenticate(:digest_md5, "test_user", "test-password", |
| 129 | + warn_deprecation: false) |
| 130 | + cmd, cont1, cont2 = 3.times.map { server.commands.pop } |
| 131 | + assert_equal %w[AUTHENTICATE DIGEST-MD5], [cmd.name, *cmd.args] |
| 132 | + assert_match(%r{\A[a-z0-9+/]+=*\z}i, cont1[:continuation].strip) |
| 133 | + assert_empty cont2[:continuation].strip |
| 134 | + assert_empty server.commands |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + test("#authenticate disconnects and raises SASL::AuthenticationFailed " \ |
| 139 | + "when the server succeeds prematurely") do |
| 140 | + with_fake_server( |
| 141 | + preauth: false, cleartext_auth: true, |
| 142 | + sasl_ir: true, sasl_mechanisms: %i[DIGEST-MD5] |
| 143 | + ) do |server, imap| |
| 144 | + server.on "AUTHENTICATE" do |cmd| |
| 145 | + response_b64 = cmd.request_continuation( |
| 146 | + [ |
| 147 | + %w[ |
| 148 | + realm="somerealm" |
| 149 | + nonce="OA6MG9tEQGm2hh" |
| 150 | + qop="auth" |
| 151 | + charset=utf-8 |
| 152 | + algorithm=md5-sess |
| 153 | + ].join(",") |
| 154 | + ].pack("m0") |
| 155 | + ) |
| 156 | + state.commands << {continuation: response_b64} |
| 157 | + server.state.authenticate(server.config.user) |
| 158 | + cmd.done_ok |
| 159 | + end |
| 160 | + assert_raise(Net::IMAP::SASL::AuthenticationIncomplete) do |
| 161 | + imap.authenticate("DIGEST-MD5", "test_user", "test-password", |
| 162 | + warn_deprecation: false) |
| 163 | + end |
| 164 | + assert imap.disconnected? |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | +end |
0 commit comments