Exemplo n.º 1
0
        public void TestArgumentExceptions()
        {
            var credentials = new NetworkCredential("username", "password");
            var uri         = new Uri("smtp://localhost");

            var sasl = new SaslMechanismLogin(credentials);

            Assert.Throws <NotSupportedException> (() => sasl.Challenge(null));

            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, Encoding.UTF8, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, Encoding.UTF8, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Encoding)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin("username", null));
        }
Exemplo n.º 2
0
        public void TestLoginAuth()
        {
            var credentials = new NetworkCredential("username", "password");
            var sasl        = new SaslMechanismLogin(credentials);

            AssertLogin(sasl, "NetworkCredential");

            sasl = new SaslMechanismLogin("username", "password");

            AssertLogin(sasl, "user/pass");
        }
Exemplo n.º 3
0
        public void TestArgumentExceptions()
        {
            var           credentials = new NetworkCredential("username", "password");
            var           uri         = new Uri("smtp://localhost");
            SaslMechanism sasl;

            Assert.Throws <ArgumentNullException> (() => new SaslException(null, SaslErrorCode.MissingChallenge, "message"));

            sasl = new SaslMechanismCramMd5(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, null));
            Assert.Throws <NotSupportedException> (() => sasl.Challenge(null));

            sasl = new SaslMechanismDigestMd5(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, null));
            Assert.Throws <NotSupportedException> (() => sasl.Challenge(null));

            sasl = new SaslMechanismLogin(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null));
            Assert.Throws <NotSupportedException> (() => sasl.Challenge(null));

            sasl = new SaslMechanismNtlm(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismOAuth2(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismPlain(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismScramSha1(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismScramSha256(uri, credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));
        }
        public void TestLoginAuth()
        {
            const string expected1   = "dXNlcm5hbWU=";
            const string expected2   = "cGFzc3dvcmQ=";
            var          credentials = new NetworkCredential("username", "password");
            var          sasl        = new SaslMechanismLogin(credentials);
            string       challenge;

            challenge = sasl.Challenge(string.Empty);

            Assert.AreEqual(expected1, challenge, "LOGIN initial challenge response does not match the expected string.");
            Assert.IsFalse(sasl.IsAuthenticated, "LOGIN should not be authenticated.");

            challenge = sasl.Challenge(string.Empty);

            Assert.AreEqual(expected2, challenge, "LOGIN final challenge response does not match the expected string.");
            Assert.IsTrue(sasl.IsAuthenticated, "LOGIN should be authenticated.");
        }
Exemplo n.º 5
0
        static void AssertLogin(SaslMechanismLogin sasl, string prefix)
        {
            const string expected1 = "dXNlcm5hbWU=";
            const string expected2 = "cGFzc3dvcmQ=";
            string       challenge;

            Assert.IsFalse(sasl.SupportsInitialResponse, "{0}: SupportsInitialResponse", prefix);

            challenge = sasl.Challenge(string.Empty);

            Assert.AreEqual(expected1, challenge, "{0}: initial challenge response does not match the expected string.", prefix);
            Assert.IsFalse(sasl.IsAuthenticated, "{0}: should not be authenticated.", prefix);

            challenge = sasl.Challenge(string.Empty);

            Assert.AreEqual(expected2, challenge, "{0}: final challenge response does not match the expected string.", prefix);
            Assert.IsTrue(sasl.IsAuthenticated, "{0}: should be authenticated.", prefix);
            Assert.AreEqual(string.Empty, sasl.Challenge(string.Empty), "{0}: challenge while authenticated.", prefix);
        }
Exemplo n.º 6
0
        public void TestLoginAuth()
        {
            var credentials = new NetworkCredential("username", "password");
            var sasl        = new SaslMechanismLogin(credentials);
            var uri         = new Uri("smtp://localhost");

            AssertLogin(sasl, "NetworkCredential");

            sasl = new SaslMechanismLogin("username", "password");

            AssertLogin(sasl, "user/pass");

            sasl = new SaslMechanismLogin(uri, credentials);

            AssertLogin(sasl, "uri/credentials");

            sasl = new SaslMechanismLogin(uri, "username", "password");

            AssertLogin(sasl, "uri/user/pass");
        }
        public void TestArgumentExceptions()
        {
            var           credentials = new NetworkCredential("username", "password");
            var           uri         = new Uri("smtp://localhost");
            SaslMechanism sasl;

            Assert.Throws <ArgumentNullException> (() => new SaslException(null, SaslErrorCode.MissingChallenge, "message"));

            sasl = new SaslMechanismCramMd5(credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismCramMd5("username", null));
            Assert.Throws <NotSupportedException> (() => sasl.Challenge(null));

            sasl = new SaslMechanismDigestMd5(credentials)
            {
                Uri = uri
            };
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, (string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismDigestMd5("username", null));
            Assert.Throws <NotSupportedException> (() => sasl.Challenge(null));

            sasl = new SaslMechanismLogin(credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, Encoding.UTF8, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, Encoding.UTF8, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, Encoding.UTF8, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Uri)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin((Encoding)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(Encoding.UTF8, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismLogin("username", null));
            Assert.Throws <NotSupportedException> (() => sasl.Challenge(null));

            sasl = new SaslMechanismNtlm(credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm((Uri)null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm((Uri)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, (string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismNtlm("username", null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismOAuth2(credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2((Uri)null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2((Uri)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, (string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismOAuth2("username", null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismPlain(credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, Encoding.UTF8, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, Encoding.UTF8, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, Encoding.UTF8, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, Encoding.UTF8, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Uri)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain((Encoding)null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(Encoding.UTF8, null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(Encoding.UTF8, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismPlain("username", null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismScramSha1(credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, (string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1((string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha1("username", null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            sasl = new SaslMechanismScramSha256(credentials);
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null, credentials));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null, "username", "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, (string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(uri, "username", null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256(null));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256((string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SaslMechanismScramSha256("username", null));
            Assert.DoesNotThrow(() => sasl.Challenge(null));

            Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create(null, uri, Encoding.UTF8, credentials));
            Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", null, Encoding.UTF8, credentials));
            Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", uri, null, credentials));
            Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", uri, Encoding.UTF8, null));

            Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create(null, uri, credentials));
            Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", null, credentials));
            Assert.Throws <ArgumentNullException> (() => SaslMechanism.Create("PLAIN", uri, null));

            Assert.Throws <ArgumentNullException> (() => SaslMechanism.SaslPrep(null));
        }
        public async Task <ImapClient> GetImapClient(string emailAddress)
        {
            IMail accountManager            = null;
            GnomeOnlineAccount gnomeAccount = null;


            var mailAccounts = (await GetAll()).Where(a => a.Interfaces.Contains(dbusMailAccountServiceName));

            var dbusSessionConnection = Connection.Session;

            foreach (var account in mailAccounts)
            {
                var dbusAccountManager = dbusSessionConnection.CreateProxy <IMail>(dbusServiceName, account.DbusObjectPath);
                if (emailAddress == await dbusAccountManager.GetEmailAddressAsync())
                {
                    accountManager = dbusAccountManager;
                    gnomeAccount   = account;
                }
            }

            if (null == accountManager)
            {
                throw new ArgumentException("emailAddress not found in Gnome OnlineAccounts");
            }


            var imapClient = new ImapClient();
            var port       = 143;

            if (await accountManager.GetImapUseSslAsync())
            {
                port = 993;
            }

            await imapClient.ConnectAsync(
                await accountManager.GetImapHostAsync(),
                port
                );

            // Determine authentication-scheme
            var sessionConnection = Connection.Session;
            var username          = await accountManager.GetImapUserNameAsync();

            SaslMechanism cred = null;

            if (gnomeAccount.Interfaces.Contains(dbusOAuth2BasedAuth) || gnomeAccount.Interfaces.Contains(dbusOAuthBasedAuth))
            {
                var oAuth2Manager = sessionConnection.CreateProxy <IOAuth2Based>(dbusServiceName, gnomeAccount.DbusObjectPath);
                (var token, var expiry) = await oAuth2Manager.GetAccessTokenAsync();

                cred = new SaslMechanismOAuth2(username, token);
            }
            else if (gnomeAccount.Interfaces.Contains(dbusPasswordBasedAuth))
            {
                var passwordManager = sessionConnection.CreateProxy <IPasswordBased>(dbusServiceName, gnomeAccount.DbusObjectPath);
                var password        = await passwordManager.GetPasswordAsync("imap-password");

                cred = new SaslMechanismLogin(username, password);
            }
            else
            {
                throw new Exception("Could not extract credentials from Gnome");
            }

            await imapClient.AuthenticateAsync(cred);

            return(imapClient);
        }