public static Func<string, string, IOStrategy, IByteConverter, ISaslMechanism> GetFactory3()
 {
     return (username, password, strategy, converter) =>
     {
         ISaslMechanism saslMechanism = null;
         var connection = strategy.ConnectionPool.Acquire();
         try
         {
             var saslListResult = strategy.Execute(new SaslList(converter), connection);
             if (saslListResult.Success)
             {
                 if (saslListResult.Value.Contains("CRAM-MD5"))
                 {
                     saslMechanism = new CramMd5Mechanism(strategy ,username, password, converter);
                 }
                 else
                 {
                     saslMechanism = new PlainTextMechanism(strategy, username, password, converter);
                 }
             }
         }
         catch (Exception e)
         {
             Log.Error(e);
         }
         return saslMechanism;
     };
 } 
 public static Func <string, string, IOStrategy, IByteConverter, ISaslMechanism> GetFactory3()
 {
     return((username, password, strategy, converter) =>
     {
         ISaslMechanism saslMechanism = null;
         var connection = strategy.ConnectionPool.Acquire();
         try
         {
             var saslListResult = strategy.Execute(new SaslList(converter), connection);
             if (saslListResult.Success)
             {
                 if (saslListResult.Value.Contains("CRAM-MD5"))
                 {
                     saslMechanism = new CramMd5Mechanism(strategy, username, password, converter);
                 }
                 else
                 {
                     saslMechanism = new PlainTextMechanism(strategy, username, password, converter);
                 }
             }
         }
         catch (Exception e)
         {
             Log.Error(e);
         }
         if (saslMechanism != null)
         {
             saslMechanism.IOStrategy = strategy;
         }
         return saslMechanism;
     });
 }
        public const uint DefaultTimeout = 2500; //2.5sec

        public static Func <string, string, IOStrategy, ITypeTranscoder, ISaslMechanism> GetFactory3()
        {
            return((username, password, strategy, transcoder) =>
            {
                ISaslMechanism saslMechanism = null;
                var connection = strategy.ConnectionPool.Acquire();
                try
                {
                    var saslListResult = strategy.Execute(new SaslList(transcoder, DefaultTimeout), connection);
                    if (saslListResult.Success)
                    {
                        if (saslListResult.Value.Contains("CRAM-MD5"))
                        {
                            saslMechanism = new CramMd5Mechanism(strategy, username, password, transcoder);
                        }
                        else
                        {
                            saslMechanism = new PlainTextMechanism(strategy, username, password, transcoder);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
                finally
                {
                    strategy.ConnectionPool.Release(connection);
                }
                return saslMechanism;
            });
        }
        public void When_Non_Sasl_Bucket_And_Empty_Password_Authenticate_Returns_true()
        {
            var authenticator = new PlainTextMechanism(_ioService, new DefaultTranscoder());
            _ioService.ConnectionPool.Initialize();

            foreach (var connection in _ioService.ConnectionPool.Connections)
            {
                var isAuthenticated = authenticator.Authenticate(connection, "default", "");
                Assert.IsTrue(isAuthenticated);
            }
        }
        public void When_Valid_Invalid_Credentials_Provided_Authenticate_Returns_False()
        {
            var authenticator = new PlainTextMechanism(_ioService, new DefaultTranscoder());
            _ioService.ConnectionPool.Initialize();

            foreach (var connection in _ioService.ConnectionPool.Connections)
            {
                var isAuthenticated = authenticator.Authenticate(connection, "authenticated", "badpass");
                Assert.IsFalse(isAuthenticated);
            }
        }
        public void When_Valid_Invalid_Credentials_Provided_Authenticate_Returns_False()
        {
            var authenticator = new PlainTextMechanism(_ioStrategy, new ManualByteConverter());
            _ioStrategy.ConnectionPool.Initialize();

            foreach (var connection in _ioStrategy.ConnectionPool.Connections)
            {
                var isAuthenticated = authenticator.Authenticate(connection, "authenticated", "badpass");
                Assert.IsFalse(isAuthenticated);
            }
        }
        public void When_Valid_Credentials_Provided_Authenticate_Returns_True()
        {
            var authenticator = new PlainTextMechanism(_ioStrategy, new DefaultTranscoder());
            _ioStrategy.ConnectionPool.Initialize();

            foreach (var connection in _ioStrategy.ConnectionPool.Connections)
            {
                var isAuthenticated = authenticator.Authenticate(connection, "authenticated", "secret");
                Assert.IsTrue(isAuthenticated);
            }
        }
        public void Test_GetConfig_Non_Default_Bucket()
        {
            var saslMechanism = new PlainTextMechanism(_ioService, "authenticated", "secret", new DefaultTranscoder());
            _ioService = new PooledIOService(_connectionPool, saslMechanism);

            var response = _ioService.Execute(new Config(new DefaultTranscoder(), OperationLifespan, _endPoint));

            Assert.IsTrue(response.Success);
            Assert.IsNotNull(response.Value);
            Assert.AreEqual("authenticated", response.Value.Name);
            Console.WriteLine(response.Value.ToString());
        }
        public void Test_GetConfig_Non_Default_Bucket()
        {
            var saslMechanism = new PlainTextMechanism(_ioStrategy, "authenticated", "secret", new AutoByteConverter());
            _ioStrategy = new DefaultIOStrategy(_connectionPool, saslMechanism);

            var response = _ioStrategy.Execute(new Config(new ManualByteConverter()));

            Assert.IsTrue(response.Success);
            Assert.IsNotNull(response.Value);
            Assert.AreEqual("authenticated", response.Value.Name);
            Console.WriteLine(response.Value.ToString());
        }
Esempio n. 10
0
        public const uint DefaultTimeout = 2500; //2.5sec

        #endregion Fields

        #region Methods

        public static Func<string, string, IIOService, ITypeTranscoder, ISaslMechanism> GetFactory(ILoggerFactory loggerFactory)
        {
            return (username, password, service, transcoder) =>
            {
                var logger = loggerFactory.CreateLogger("Couchbase.Authentication.SASL." + nameof(SaslFactory));

                ISaslMechanism saslMechanism = null;
                IConnection connection = null;
                try
                {
                    connection = service.ConnectionPool.Acquire();

                    var saslListResult = service.Execute(new SaslList(transcoder, DefaultTimeout), connection);
                    if (saslListResult.Success)
                    {
                        if (saslListResult.Value.Contains("CRAM-MD5"))
                        {
                            saslMechanism = new CramMd5Mechanism(service, username, password, transcoder, loggerFactory);
                        }
                        else
                        {
                            saslMechanism = new PlainTextMechanism(service, username, password, transcoder, loggerFactory);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e.Message, e);
                }
                finally
                {
                    if (connection != null)
                    {
                        service.ConnectionPool.Release(connection);
                    }
                }
                return saslMechanism;
            };
        }
        public const uint DefaultTimeout = 2500; //2.5sec

        public static Func<string, string, IOStrategy, ITypeTranscoder, ISaslMechanism> GetFactory()
        {
            return (username, password, strategy, transcoder) =>
            {
                ISaslMechanism saslMechanism = null;
                IConnection connection = null;
                try
                {
                    connection = strategy.ConnectionPool.Acquire();
                    var saslListResult = strategy.Execute(new SaslList(transcoder, DefaultTimeout), connection);
                    if (saslListResult.Success)
                    {
                        if (saslListResult.Value.Contains("CRAM-MD5"))
                        {
                            saslMechanism = new CramMd5Mechanism(strategy, username, password, transcoder);
                        }
                        else
                        {
                            saslMechanism = new PlainTextMechanism(strategy, username, password, transcoder);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
                finally
                {
                    if (connection != null)
                    {
                        strategy.ConnectionPool.Release(connection);
                    }
                }
                return saslMechanism;
            };
        }