Esempio n. 1
0
        public void TestGreetingOkWithNoImapCapability()
        {
            server.EnqueueResponse("* OK [CAPABILITY AUTH=PLAIN] ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port)) {
            Assert.IsTrue(!session.ServerCapabilities.Has(ImapCapability.Imap4Rev1));

            session.HandlesIncapableAsException = true;

            session.Login(credential);
              }
        }
Esempio n. 2
0
        private static ImapCommandResult AuthenticateAsAnonymous(ImapSession session,
                                                             string username,
                                                             bool canFallback)
        {
            /*
               * http://tools.ietf.org/html/rfc5092
               * 3.2. IMAP User Name and Authentication Mechanism
               *
               *    If no user name and no authentication mechanism are supplied, the
               *    client MUST authenticate as anonymous to the server.  If the server
               *    advertises AUTH=ANONYMOUS IMAP capability, the client MUST use the
               *    AUTHENTICATE command with ANONYMOUS [ANONYMOUS] SASL mechanism.  If
               *    SASL ANONYMOUS is not available, the (case-insensitive) user name
               *    "anonymous" is used with the "LOGIN" command and the Internet email
               *    address of the end user accessing the resource is supplied as the
               *    password.  The latter option is given in order to provide for
               *    interoperability with deployed servers.
               *
               *    Note that, as described in RFC 3501, the "LOGIN" command MUST NOT be
               *    used when the IMAP server advertises the LOGINDISABLED capability.
               */
              ImapCommandResult result = null;

              if (string.IsNullOrEmpty(username))
            username = "******";

              if (session.ServerCapabilities.IsCapable(ImapAuthenticationMechanism.Anonymous))
            // try AUTHENTICATE ANONYUMOUS
            result = session.Authenticate(new NetworkCredential(username, string.Empty),
                                      null,
                                      ImapAuthenticationMechanism.Anonymous,
                                      true);

              if ((result == null || (result.Failed && canFallback)) &&
              !session.ServerCapabilities.Has(ImapCapability.LoginDisabled))
            // try anonymous LOGIN
            result = session.Login(new NetworkCredential("anonymous", username),
                               null,
                               true);

              return result;
        }
Esempio n. 3
0
        private static ImapCommandResult AuthenticateWithAppropriateMechanism(ImapSession session,
                                                                          bool allowPlainTextMechanism,
                                                                          ICredentialsByHost credentials,
                                                                          string username,
                                                                          IEnumerable<string> usingSaslMechanisms)
        {
            ImapCommandResult result = null;

              foreach (var mechanism in usingSaslMechanisms) {
            if (!allowPlainTextMechanism && SaslClientMechanism.IsMechanismPlainText(mechanism))
              // disallow plain text mechanism
              continue;

            if (string.Equals(mechanism, SaslMechanisms.Anonymous, StringComparison.OrdinalIgnoreCase))
              // disallow 'ANONYMOUS' mechanism
              continue;

            var authMechanism = ImapAuthenticationMechanism.GetKnownOrCreate(mechanism);

            if (session.ServerCapabilities.IsCapable(authMechanism)) {
              result = session.Authenticate(credentials, username, authMechanism, true);

              if (result.Succeeded)
            break;
            }
              }

              if ((result == null || result.Failed) &&
              allowPlainTextMechanism &&
              !session.ServerCapabilities.Has(ImapCapability.LoginDisabled))
            result = session.Login(credentials, username, true);

              return result;
        }
        private void LoginCredentialPropertyNull(ICredentialsByHost credential, string expectedArgs)
        {
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ImapSimulatedServer ready\r\n");
              server.EnqueueResponse("0000 NO failed\r\n");

              using (var session = new ImapSession(host, port)) {
            Assert.IsFalse((bool)session.Login(credential));

            Assert.AreEqual(string.Format("0000 LOGIN {0}\r\n", expectedArgs),
                        server.DequeueRequest());
              }
        }
        private void LoginSelectAppropriateCredential(bool specifyUsername)
        {
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port)) {
            session.HandlesIncapableAsException = true;

            var credentials = new CredentialCache();

            credentials.Add("imap.example.net", 143, "LOGIN", new NetworkCredential("user", "pass1"));
            credentials.Add(server.Host, server.Port, "LOGIN", new NetworkCredential("user", "pass2"));
            credentials.Add(server.Host, server.Port, "PLAIN", new NetworkCredential("user", "pass3"));
            credentials.Add(server.Host, server.Port, string.Empty, new NetworkCredential("user", "pass4"));

            server.EnqueueResponse("0000 OK authenticated\r\n");

            if (specifyUsername)
              Assert.IsTrue((bool)session.Login(credentials, "user"));
            else
              Assert.IsTrue((bool)session.Login(credentials));

            Assert.AreEqual(string.Format("0000 LOGIN user pass4\r\n"),
                        server.DequeueRequest());

            Assert.AreEqual(new Uri(string.Format("imap://user@{0}:{1}/", host, port)), session.Authority);
            Assert.AreEqual(ImapSessionState.Authenticated, session.State);
              }
        }
        public void TestLoginReissueCapabilityResponseCodeAdvertised()
        {
            server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port)) {
            server.EnqueueResponse("0000 OK [CAPABILITY IMAP4rev1] authenticated\r\n");

            Assert.IsTrue((bool)session.Login(credential, true));

            StringAssert.StartsWith("0000 LOGIN ", server.DequeueRequest());

            Assert.AreEqual(1, session.ServerCapabilities.Count);
            Assert.IsTrue(session.ServerCapabilities.Has(ImapCapability.Imap4Rev1));
              }
        }
        public void TestLoginSelectAppropriateCredentialNotFound()
        {
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port)) {
            session.HandlesIncapableAsException = true;

            var credentials = new CredentialCache();

            credentials.Add("imap.example.net", 143, "LOGIN", new NetworkCredential("user", "pass1"));
            credentials.Add(server.Host, server.Port, "LOGIN", new NetworkCredential("user", "pass2"));
            credentials.Add(server.Host, server.Port, "PLAIN", new NetworkCredential("user", "pass3"));
            credentials.Add(server.Host, server.Port, string.Empty, new NetworkCredential("user", "pass4"));

            var result = session.Login(credentials, "xxxx");

            Assert.IsFalse((bool)result);
            Assert.AreEqual(ImapCommandResultCode.RequestError, result.Code);
            Assert.AreEqual(ImapSessionState.NotAuthenticated, session.State);
              }
        }
        public void TestLoginMailboxReferralAsError()
        {
            server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port, false)) {
            server.EnqueueResponse("0000 OK [REFERRAL IMAP://MATTHEW@SERVER2/] Specified user's personal mailboxes located on Server2, but public mailboxes are available.\r\n");

            Assert.IsTrue((bool)session.Login(credential));
              }
        }
        public void TestLoginMailboxReferralAsException()
        {
            server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port, true)) {
            server.EnqueueResponse("0000 OK [REFERRAL IMAP://MATTHEW@SERVER2/] Specified user's personal mailboxes located on Server2, but public mailboxes are available.\r\n");

            try {
              session.Login(credential);

              Assert.Fail("logged in without exception");
            }
            catch (ImapLoginReferralException ex) {
              Assert.IsTrue(ex.Message.Contains("Specified user's personal mailboxes located on Server2, but public mailboxes are available."));
              Assert.AreEqual(new Uri("IMAP://MATTHEW@SERVER2/"), ex.ReferToUri);
            }
              }
        }
        public void TestLoginHomerServerReferralAsException()
        {
            server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port, true)) {
            server.EnqueueResponse("0000 NO [REFERRAL IMAP://MIKE@SERVER2/] Specified user is invalid on this server. Try SERVER2.\r\n");

            try {
              session.Login(credential);

              Assert.Fail("logged in without exception");
            }
            catch (ImapLoginReferralException ex) {
              Assert.IsTrue(ex.Message.Contains("Specified user is invalid on this server. Try SERVER2."));
              Assert.AreEqual(new Uri("IMAP://MIKE@SERVER2/"), ex.ReferToUri);
            }
              }
        }
        public void TestLoginHomerServerReferralAsError()
        {
            server.EnqueueResponse("* OK ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port, false)) {
            server.EnqueueResponse("0000 NO [REFERRAL IMAP://MIKE@SERVER2/] Specified user is invalid on this server. Try SERVER2.\r\n");

            Assert.IsFalse((bool)session.Login(credential));
              }
        }
        public void TestLoginDisabled()
        {
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1 LOGINDISABLED] ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port)) {
            session.Login(credential);
              }
        }
        public void TestLoginCredentialNotFound()
        {
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port)) {
            Assert.IsFalse((bool)session.Login(new NullCredential()));
            Assert.IsFalse((bool)session.Login(new NullCredential(), "user"));
              }
        }
        public void TestLogin()
        {
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ImapSimulatedServer ready\r\n");

              using (var session = new ImapSession(host, port)) {
            session.HandlesIncapableAsException = true;

            server.EnqueueResponse("0000 NO incorrect\r\n");

            Assert.IsFalse((bool)session.Login(credential));

            Assert.AreEqual(string.Format("0000 LOGIN {0} {1}\r\n", username, password),
                        server.DequeueRequest());

            server.EnqueueResponse("0001 OK authenticated\r\n");

            Assert.IsTrue((bool)session.Login(credential));

            Assert.AreEqual(string.Format("0001 LOGIN {0} {1}\r\n", username, password),
                        server.DequeueRequest());

            Assert.AreEqual(new Uri(string.Format("imap://{0}@{1}:{2}/", username, host, port)), session.Authority);
            Assert.AreEqual(ImapSessionState.Authenticated, session.State);
              }
        }