Exemplo n.º 1
0
        // Dispose(bool disposing) executes in two distinct scenarios.
        // If disposing equals true, the method has been called directly
        // or indirectly by a user's code. Managed and unmanaged resources
        // can be disposed.
        // If disposing equals false, the method has been called by the
        // runtime from inside the finalizer and you should not reference
        // other objects. Only unmanaged resources can be disposed.
        private void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.
                    // Remove the event handler or we will be in trouble with too many events
                    this.m_XmppClient.StreamParser.OnStreamElement -= new StreamHandler(this.OnStreamElement);
                    this.m_XmppClient = null;
                    this.m_Mechanism  = null;
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // If disposing is false,
                // only the following code is executed.
            }
            this.disposed = true;
        }
Exemplo n.º 2
0
        internal void OnStreamElement(object sender, Node e)
        {
            if (this.m_XmppClient.XmppConnectionState == XmppConnectionState.Securing ||
                this.m_XmppClient.XmppConnectionState == XmppConnectionState.StartCompression)
            {
                return;
            }

            if (e.GetType() == typeof(StreamFeatures))
            {
                var f = e as StreamFeatures;
                if (!this.m_XmppClient.Authenticated)
                {
                    // RECV: <stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
                    //			<mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism>
                    //			</mechanisms>
                    //			<register xmlns='http://jabber.org/features/iq-register'/>
                    //		</stream:features>
                    // SENT: <auth mechanism="DIGEST-MD5" xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
                    // Select a SASL mechanism

                    var args = new SaslEventArgs(f.Mechanisms);

                    OnSaslStart?.Invoke(this, args);

                    if (args.Auto == true)
                    {
                        // Library handles the Sasl stuff
                        if (f.Mechanisms != null)
                        {
                            if (this.m_XmppClient.UseStartTLS == false && this.m_XmppClient.UseSSL == false &&
                                f.Mechanisms.SupportsMechanism(MechanismType.X_GOOGLE_TOKEN))
                            {
                                // This is the only way to connect to GTalk on a unsecure Socket for now
                                // Secure authentication is done over https requests to pass the
                                // authentication credentials on a secure connection
                                args.Mechanism = Protocol.sasl.Mechanism.GetMechanismName(MechanismType.X_GOOGLE_TOKEN);
                            }
                            else if (f.Mechanisms.SupportsMechanism(MechanismType.DIGEST_MD5))
                            {
                                args.Mechanism = Protocol.sasl.Mechanism.GetMechanismName(MechanismType.DIGEST_MD5);
                            }
                            else if (f.Mechanisms.SupportsMechanism(MechanismType.PLAIN))
                            {
                                args.Mechanism = Protocol.sasl.Mechanism.GetMechanismName(MechanismType.PLAIN);
                            }
                            else
                            {
                                args.Mechanism = null;
                            }
                        }
                        else
                        {
                            // Hack for Google
                            // TODO: i don't think we need this anymore. This was in an very early version of the gtalk server.
                            args.Mechanism = null;
                            //args.Mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(agsXMPP.protocol.sasl.MechanismType.PLAIN);
                        }
                    }
                    if (args.Mechanism != null)
                    {
                        this.m_Mechanism = Factory.SaslFactory.GetMechanism(args.Mechanism);
                        // Set properties for the SASL mechanism
                        this.m_Mechanism.Username = this.m_XmppClient.Username;
                        this.m_Mechanism.Password = this.m_XmppClient.Password;
                        this.m_Mechanism.Server   = this.m_XmppClient.Server;
                        // Call Init Method on the mechanism
                        this.m_Mechanism.Init(this.m_XmppClient);
                    }
                    else
                    {
                        this.m_XmppClient.RequestLoginInfo();
                    }
                }
                else if (!this.m_XmppClient.Binded)
                {
                    if (f.SupportsBind)
                    {
                        this.m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binding);

                        BindIq bIq;
                        if (this.m_XmppClient.Resource == null || this.m_XmppClient.Resource.Length == 0)
                        {
                            bIq = new BindIq(IQType.Set, new Jid(this.m_XmppClient.Server));
                        }
                        else
                        {
                            bIq = new BindIq(IQType.Set, new Jid(this.m_XmppClient.Server), this.m_XmppClient.Resource);
                        }

                        this.m_XmppClient.IqGrabber.SendIq(bIq, new IqCB(this.BindResult), null);
                    }
                }
            }
            else if (e.GetType() == typeof(Challenge))
            {
                if (this.m_Mechanism != null && !this.m_XmppClient.Authenticated)
                {
                    this.m_Mechanism.Parse(e);
                }
            }
            else if (e.GetType() == typeof(Success))
            {
                // SASL authentication was successfull
                OnSaslEnd?.Invoke(this);

                this.m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Authenticated);

                this.m_Mechanism = null;

                this.m_XmppClient.Reset();
            }
            else if (e.GetType() == typeof(Failure))
            {
                // Authentication failure
                this.m_XmppClient.FireOnAuthError(e as Element);
            }
        }