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;
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// Closes the connection.
        /// </summary>
        /// <returns></returns>
        public async Task CloseAsync()
        {
            if (this.isDisposed || this.State == XmppTransportState.Closed || this.State == XmppTransportState.Closing)
            {
                return;
            }

            try
            {
                await SoftCloseAsync().ConfigureAwait(false);
            }
            catch
            {
            }
            finally
            {
                this.DisposeSubscriptions();

                this.transport          = null;
                this.saslMechanism      = null;
                this.connectionString   = null;
                this.userAddress        = null;
                this.people             = null;
                this.activity           = null;
                this.capabilities       = null;
                this.personalEventing   = null;
                this.presence           = null;
                this.serverCapabilities = null;
                this.serverFeatures     = ServerFeatures.None;

                this.ReleaseSubjects();
            }
        }
Exemplo n.º 3
0
        private async Task OnStartSaslNegotiationAsync()
        {
            this.State         = XmppTransportState.Authenticating;
            this.saslMechanism = this.CreateSaslMechanism();

            await this.SendAsync(this.saslMechanism.StartSaslNegotiation()).ConfigureAwait(false);
        }
Exemplo n.º 4
0
        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;
            });
        }
Exemplo n.º 5
0
        private async Task SoftCloseAsync()
        {
            if (this.isDisposed || this.State == XmppTransportState.Closed || this.State == XmppTransportState.Closing)
            {
                return;
            }

            try
            {
                this.State = XmppTransportState.Closing;

                // Send the XMPP stream close tag
                await this.CloseStreamAsync().ConfigureAwait(false);

#warning TODO: Wait until the server sends the stream close tag

                // Close the underlying transport
                this.CloseTransport();
            }
            catch
            {
            }
            finally
            {
                this.transport      = null;
                this.saslMechanism  = null;
                this.serverFeatures = ServerFeatures.None;
                this.State          = XmppTransportState.Closed;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Disposes the specified disposing.
        /// </summary>
        /// <param name="disposing">if set to <c>true</c> [disposing].</param>
        private void Dispose(bool disposing)
        {
            if (!this.isDisposed)
            {
                if (disposing)
                {
                    // Release managed resources here
                    this.CloseAsync().GetAwaiter().GetResult();
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // If disposing is false,
                // only the following code is executed.
                this.connectionString   = null;
                this.userAddress        = null;
                this.saslMechanism      = null;
                this.people             = null;
                this.activity           = null;
                this.capabilities       = null;
                this.personalEventing   = null;
                this.presence           = null;
                this.serverCapabilities = null;
                this.serverFeatures     = ServerFeatures.None;
                this.state = XmppTransportState.Closed;
                this.CloseTransport();
                this.DisposeSubscriptions();
                this.ReleaseSubjects();
            }

            this.isDisposed = true;
        }
Exemplo n.º 7
0
 public SocketAsyncStrategy(IConnectionPool connectionPool, SocketAsyncPool socketAsyncPool, ISaslMechanism saslMechanism, IByteConverter converter)
 {
     _connectionPool           = connectionPool;
     _socketAsyncPool          = socketAsyncPool;
     _saslMechanism            = saslMechanism;
     _saslMechanism.IOStrategy = this;
     _converter = converter;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="PooledIOService"/> class.
        /// </summary>
        /// <param name="connectionPool">The connection pool.</param>
        /// <param name="saslMechanism">The sasl mechanism.</param>
        public PooledIOService(IConnectionPool connectionPool, ISaslMechanism saslMechanism)
        {
            Log.Debug("Creating PooledIOService {0}", Identity);
            ConnectionPool = connectionPool;
            SaslMechanism  = saslMechanism;

            var conn = connectionPool.Acquire();

            CheckEnabledServerFeatures(conn);
        }
Exemplo n.º 9
0
        private async Task OnSaslSuccessAsync(SaslSuccess success)
        {
            if (this.saslMechanism.ProcessSuccess(success))
            {
                this.State         = XmppTransportState.Authenticated;
                this.saslMechanism = null;

                await this.transport.ResetStreamAsync().ConfigureAwait(false);
            }
            else
            {
                this.OnSaslFailure("Server reponse cannot be verified.");
            }
        }
Exemplo n.º 10
0
        public const uint DefaultTimeout = 2500; //2.5sec

        public static Func <string, string, IConnectionPool, ITypeTranscoder, ISaslMechanism> GetFactory()
        {
            return((username, password, pool, transcoder) =>
            {
                ISaslMechanism saslMechanism = null;
                IConnection connection = null;
                try
                {
                    if (pool.Configuration?.ForceSaslPlain ?? false)
                    {
                        return new PlainTextMechanism(username, password, transcoder);
                    }

                    connection = pool.Acquire();
                    var saslListResult = Execute(new SaslList(transcoder, DefaultTimeout), connection);
                    if (saslListResult.Success)
                    {
                        if (saslListResult.Value.Contains("SCRAM-SHA1"))
                        {
                            return new ScramShaMechanism(transcoder, username, password, MechanismType.ScramSha1);
                        }
                        if (saslListResult.Value.Contains("CRAM-MD5"))
                        {
                            return new CramMd5Mechanism(username, password, transcoder);
                        }
                        if (saslListResult.Value.Contains("PLAIN"))
                        {
                            return new PlainTextMechanism(username, password, transcoder);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
                finally
                {
                    if (connection != null)
                    {
                        pool.Release(connection);
                    }
                }
                return saslMechanism;
            });
        }
Exemplo n.º 11
0
        private ISaslMechanism CreateSaslMechanism()
        {
            ISaslMechanism mechanism = null;

            if (this.Supports(ServerFeatures.SaslScramSha1))
            {
                mechanism = new SaslScramSha1Mechanism(this.ConnectionString);
            }
            else if (this.Supports(ServerFeatures.SaslDigestMD5))
            {
                mechanism = new SaslDigestMechanism(this.ConnectionString);
            }
            else if (this.Supports(ServerFeatures.SaslPlain))
            {
                mechanism = new SaslPlainMechanism(this.ConnectionString);
            }

            return(mechanism);
        }
Exemplo n.º 12
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="SaslAuthenticationProtocol" /> class.
 /// </summary>
 /// <param name="mechanism">The mechanism.</param>
 public SaslAuthenticationProtocol(ISaslMechanism mechanism)
 {
     _mechanism = mechanism;
 }
 public DefaultIOStrategy(IConnectionPool connectionPool, ISaslMechanism saslMechanism)
 {
     _connectionPool = connectionPool;
     _saslMechanism  = saslMechanism;
 }
Exemplo n.º 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultIOStrategy"/> class.
 /// </summary>
 /// <param name="connectionPool">The connection pool.</param>
 /// <param name="saslMechanism">The sasl mechanism.</param>
 public DefaultIOStrategy(IConnectionPool connectionPool, ISaslMechanism saslMechanism)
 {
     Log.Debug(m => m("Creating DefaultIOStrategy {0}", _identity));
     _connectionPool = connectionPool;
     _saslMechanism  = saslMechanism;
 }
Exemplo n.º 15
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="SaslAuthenticator"/> class.
 /// </summary>
 /// <param name="mechanism">The mechanism.</param>
 protected SaslAuthenticator(ISaslMechanism mechanism)
 {
     _mechanism = Ensure.IsNotNull(mechanism, "mechanism");
 }
Exemplo n.º 16
0
 public SocketAsyncStrategy(IConnectionPool connectionPool, ISaslMechanism saslMechanism)
     : this(connectionPool, new SocketAsyncPool(connectionPool, SocketAsyncFactory.GetSocketAsyncFunc()), saslMechanism, new AutoByteConverter())
 {
 }
Exemplo n.º 17
0
 public SharedPooledIOService(IConnectionPool connectionPool, ISaslMechanism saslMechanism)
     : base(connectionPool, saslMechanism)
 {
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="SaslAuthenticationMethod" /> class.
 /// </summary>
 /// <param name="mechanism">The mechanism.</param>
 public SaslAuthenticationMethod(ISaslMechanism mechanism)
 {
     _mechanism = mechanism;
 }
Exemplo n.º 19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SaslAuthenticator"/> class.
 /// </summary>
 /// <param name="mechanism">The mechanism.</param>
 /// <param name="serverApi">The server API.</param>
 protected SaslAuthenticator(ISaslMechanism mechanism, ServerApi serverApi)
 {
     _mechanism = Ensure.IsNotNull(mechanism, nameof(mechanism));
     _serverApi = serverApi; // can be null
 }
Exemplo n.º 20
0
 public void AddMechanism(ISaslMechanism mechanism)
 {
     mechanisms.Add(mechanism.Name, mechanism);
 }
 public AuthenticationState GenericTest(ISaslMechanism client, ISaslMechanism server)
 {
     return(GenericTest(client.InitialStep, server.InitialStep));
 }
 public MultiplexingIOService(IConnectionPool connectionPool, ISaslMechanism saslMechanism)
     : this(connectionPool)
 {
     SaslMechanism = saslMechanism;
 }
 public AuthenticationState GenericTest(ISaslMechanism client, ISaslMechanism server)
 {
     return GenericTest(client.InitialStep, server.InitialStep);
 }
 // constructors
 protected SaslAuthenticator(ISaslMechanism mechanism)
 {
     _mechanism = Ensure.IsNotNull(mechanism, "mechanism");
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="PooledIOService"/> class.
 /// </summary>
 /// <param name="connectionPool">The connection pool.</param>
 /// <param name="saslMechanism">The sasl mechanism.</param>
 public PooledIOService(IConnectionPool connectionPool, ISaslMechanism saslMechanism)
 {
     Log.Debug(m => m("Creating PooledIOService {0}", _identity));
     _connectionPool = connectionPool;
     _saslMechanism  = saslMechanism;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="SaslAuthenticationProtocol" /> class.
 /// </summary>
 /// <param name="mechanism">The mechanism.</param>
 public SaslAuthenticationProtocol(ISaslMechanism mechanism)
 {
     _mechanism = mechanism;
 }
Exemplo n.º 27
0
 protected SaslAuthenticator(ISaslMechanism mechanism)
     : this(mechanism, serverApi : null)
 {
 }