private void ProcessBind(Iq iq) { var bind = iq.Query as Bind; string res = bind.Resource; if (!String.IsNullOrEmpty(res)) { var jid = new Jid(User, XmppDomain, res); Jid = jid; var resIq = new BindIq { Id = iq.Id, Type = IqType.Result, Bind = { Jid = jid } }; Send(resIq); Resource = res; IsBinded = true; // connection is bindet now. Add it to our global list of connection. Global.ServerConnections.Add(this); } else { // return error } }
private async Task <Iq> DoBindAsync(StreamFeatures features, CancellationToken cancellationToken) { XmppSessionState.Value = SessionState.Binding; var bIq = new BindIq { Type = IqType.Set, Bind = { Resource = Resource } }; var resBindIq = await SendIqAsync(bIq, cancellationToken); if (resBindIq.Type != IqType.Result) { throw new BindException(resBindIq); } if (features.SupportsSession && !features.Session.Optional) { var sessionIq = new SessionIq { Type = IqType.Set }; var resSessionIq = await SendIqAsync(sessionIq, cancellationToken); } XmppSessionState.Value = SessionState.Binded; return(resBindIq); }
public async Task ProcessStanza(IChannelHandlerContext context, Iq iq) { var bind = iq.Query as Bind; // read desired resource string res = bind.Resource; if (String.IsNullOrEmpty(res) || SecureResource) { // no resource given, assign random res = Guid.NewGuid().ToString(); } var serverSession = context.Channel.Pipeline.Get <ServerConnectionHandler>(); var jid = new Jid(serverSession.User, serverSession.XmppDomain, res); var resIq = new BindIq { Id = iq.Id, Type = Matrix.Xmpp.IqType.Result, Bind = { Jid = jid } }; await SendAsync(resIq); serverSession.Resource = res; serverSession.SessionState = SessionState.Binded; }
internal void DoBind() { m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binding); BindIq bIq = string.IsNullOrEmpty(m_XmppClient.Resource) ? new BindIq(IqType.set) : new BindIq(IqType.set, m_XmppClient.Resource); m_XmppClient.IqGrabber.SendIq(bIq, BindResult, null); }
/// <summary> /// </summary> internal void DoBind() { m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binding); BindIq bIq; if (m_XmppClient.Resource == null || m_XmppClient.Resource.Length == 0) { bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server)); } else { bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server), m_XmppClient.Resource); } m_XmppClient.IqGrabber.SendIq(bIq, new IqCB(BindResult), null); }
private void ProcessBind(Iq iq) { var bind = iq.Query as Bind; string res = bind.Resource; if (!String.IsNullOrEmpty(res)) { var jid = new Jid(User, XmppDomain, res); _Jid = jid; var resIq = new BindIq { Id = iq.Id, Type = IqType.Result, Bind = { Jid = jid } }; Send(resIq); Resource = res; IsBinded = true; // connection is bindet now. Add it to our global list of connection. if (!Global.ServerConnections.ContainsKey(this._Jid)) { Global.ServerConnections[this._Jid] = this; m_server.OnConnect(this._Jid); } //else // Global.ServerConnections.Add(this._Jid, this); // Wait until we receive the HANDSHAKE message //if (m_server != null) //{ // m_server.OnConnect(m_clientJid); //} } else { // return error } }
/// <summary> /// </summary> /// <param name="sender"> /// </param> /// <param name="e"> /// </param> internal void OnStreamElement(object sender, Node e) { if (m_XmppClient.XmppConnectionState == XmppConnectionState.Securing || m_XmppClient.XmppConnectionState == XmppConnectionState.StartCompression) { return; } if (e.GetType() == typeof(Features)) { Features f = e as Features; if (!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 SaslEventArgs args = new SaslEventArgs(f.Mechanisms); if (OnSaslStart != null) { OnSaslStart(this, args); } if (args.Auto == true) { // Library handles the Sasl stuff if (f.Mechanisms != null) { if (m_XmppClient.UseStartTLS == false && 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) { m_Mechanism = SaslFactory.GetMechanism(args.Mechanism); // Set properties for the SASL mechanism m_Mechanism.Username = m_XmppClient.Username; m_Mechanism.Password = m_XmppClient.Password; m_Mechanism.Server = m_XmppClient.Server; // Call Init Method on the mechanism m_Mechanism.Init(m_XmppClient); } else { m_XmppClient.RequestLoginInfo(); } } else if (!m_XmppClient.Binded) { if (f.SupportsBind) { m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binding); BindIq bIq; if (m_XmppClient.Resource == null || m_XmppClient.Resource.Length == 0) { bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server)); } else { bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server), m_XmppClient.Resource); } m_XmppClient.IqGrabber.SendIq(bIq, new IqCB(BindResult), null); } } } else if (e.GetType() == typeof(Challenge)) { if (m_Mechanism != null && !m_XmppClient.Authenticated) { m_Mechanism.Parse(e); } } else if (e.GetType() == typeof(Success)) { // SASL authentication was successfull if (OnSaslEnd != null) { OnSaslEnd(this); } m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Authenticated); m_Mechanism = null; m_XmppClient.Reset(); } else if (e.GetType() == typeof(Failure)) { // Authentication failure m_XmppClient.FireOnAuthError(e as Element); } }
/// <summary> /// 处理StreamElement /// </summary> /// <param name="e"></param> private void OnStreamElement(Node e) { if (ConnectionState == XmppConnectionState.Securing || ConnectionState == XmppConnectionState.StartCompression) { return; } if (e is Features) { Features f = e as Features; SaslEventArgs args = new SaslEventArgs(f.Mechanisms); if (OnSaslStart != null) { OnSaslStart(this, args); } if (args.Auto) { if (!Authenticated) { string mechanism = null; if (f.Mechanisms != null) { if (f.Mechanisms.SupportsMechanism(MechanismType.SCRAM_SHA_1)) { mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(MechanismType.SCRAM_SHA_1); } if (f.Mechanisms.SupportsMechanism(MechanismType.DIGEST_MD5)) { mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(MechanismType.DIGEST_MD5); } else if (f.Mechanisms.SupportsMechanism(MechanismType.PLAIN)) { mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName(MechanismType.PLAIN); } } args.Mechanism = mechanism; if (String.IsNullOrEmpty(mechanism)) { // } else { WSMechanism wsmechanism = MechanismFactory.GetMechanism(mechanism); // Set properties for the SASL mechanism wsmechanism.Username = FullUserName; wsmechanism.Password = Password; wsmechanism.Server = Server; wsmechanism.AuthStr = TokenData.access_token;// "YWMtwFZU6n3bEeWPKIERbxAADQAAAVHmL3PeTIwn69ar3nW_uwFcIiqVYO2XgGA"; // Call Init Method on the mechanism Send(wsmechanism.BuildeAuthNode()); } } else if (!Binded) // 如果没有绑定,则发送绑定消息 { if (f.SupportsBind) { DoChangeXmppConnectionState(XmppConnectionState.Binding); BindIq bIq = string.IsNullOrEmpty(Resource) ? new BindIq(IqType.set) : new BindIq(IqType.set, Resource); this.IqGrabber.SendIq(bIq, BindResult, null); } } } } else if (e is Success) { if (OnSaslEnd != null) { OnSaslEnd(this); } // SASL authentication was successfull DoChangeXmppConnectionState(XmppConnectionState.Authenticated); Authenticated = true; SendOpen(); } else if (e is Failure) { // Authentication failure if (OnAuthError != null) { OnAuthError(this, (Failure)e); } } else if (e is Message) { Message msg = (Message)e; string body = msg.Body; //msg.Type; } }
/// <summary> /// </summary> /// <param name="sender"> /// </param> /// <param name="e"> /// </param> internal void OnStreamElement(object sender, Node e) { if (m_XmppClient.XmppConnectionState == XmppConnectionState.Securing || m_XmppClient.XmppConnectionState == XmppConnectionState.StartCompression) { return; } if (e.GetType() == typeof (Features)) { Features f = e as Features; if (!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 SaslEventArgs args = new SaslEventArgs(f.Mechanisms); if (OnSaslStart != null) { OnSaslStart(this, args); } if (args.Auto == true) { // Library handles the Sasl stuff if (f.Mechanisms != null) { if (m_XmppClient.UseStartTLS == false && 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) { m_Mechanism = SaslFactory.GetMechanism(args.Mechanism); // Set properties for the SASL mechanism m_Mechanism.Username = m_XmppClient.Username; m_Mechanism.Password = m_XmppClient.Password; m_Mechanism.Server = m_XmppClient.Server; // Call Init Method on the mechanism m_Mechanism.Init(m_XmppClient); } else { m_XmppClient.RequestLoginInfo(); } } else if (!m_XmppClient.Binded) { if (f.SupportsBind) { m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binding); BindIq bIq; if (m_XmppClient.Resource == null || m_XmppClient.Resource.Length == 0) { bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server)); } else { bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server), m_XmppClient.Resource); } m_XmppClient.IqGrabber.SendIq(bIq, new IqCB(BindResult), null); } } } else if (e.GetType() == typeof (Challenge)) { if (m_Mechanism != null && !m_XmppClient.Authenticated) { m_Mechanism.Parse(e); } } else if (e.GetType() == typeof (Success)) { // SASL authentication was successfull if (OnSaslEnd != null) { OnSaslEnd(this); } m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Authenticated); m_Mechanism = null; m_XmppClient.Reset(); } else if (e.GetType() == typeof (Failure)) { // Authentication failure m_XmppClient.FireOnAuthError(e as Element); } }
internal void OnStreamElement(object sender, ElementEventArgs eventArgs) { if (m_XmppClient == null) { return; } if (m_XmppClient.XmppConnectionState == XmppConnectionState.Securing || m_XmppClient.XmppConnectionState == XmppConnectionState.StartCompression) { return; } var e = eventArgs.Element; if (e is Features) { Features f = e as Features; if (!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 SaslEventArgs args = new SaslEventArgs(f.Mechanisms); if (OnSaslStart != null) { OnSaslStart(this, args); } if (args.Auto) { // Library handles the Sasl stuff if (f.Mechanisms != null) { if (m_XmppClient.UseStartTLS == false && 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); } #if !(CF || CF_2) else if (m_XmppClient.UseSso && f.Mechanisms.SupportsMechanism(MechanismType.GSSAPI)) { args.Mechanism = protocol.sasl.Mechanism.GetMechanismName(MechanismType.GSSAPI); string kerbPrinc = f.Mechanisms.GetMechanism(MechanismType.GSSAPI).KerberosPrincipal; if (kerbPrinc != null) { m_XmppClient.KerberosPrincipal = f.Mechanisms.GetMechanism(MechanismType.GSSAPI).KerberosPrincipal; } } #endif else if (f.Mechanisms.SupportsMechanism(MechanismType.SCRAM_SHA_1)) { args.Mechanism = protocol.sasl.Mechanism.GetMechanismName(MechanismType.SCRAM_SHA_1); } 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) { m_Mechanism = Factory.SaslFactory.GetMechanism(args.Mechanism); // Set properties for the SASL mechanism m_Mechanism.Username = m_XmppClient.Username; m_Mechanism.Password = m_XmppClient.Password; m_Mechanism.Server = m_XmppClient.Server; m_Mechanism.ExtentedData = args.ExtentedData; // Call Init Method on the mechanism m_Mechanism.Init(m_XmppClient); } else { m_XmppClient.RequestLoginInfo(); } } else if (!m_XmppClient.Binded) { if (f.SupportsBind) { m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binding); BindIq bIq = string.IsNullOrEmpty(m_XmppClient.Resource) ? new BindIq(IqType.set) : new BindIq(IqType.set, m_XmppClient.Resource); m_XmppClient.IqGrabber.SendIq(bIq, BindResult); } } } else if (e is Challenge) { if (m_Mechanism != null && !m_XmppClient.Authenticated) { m_Mechanism.Parse(e); } } else if (e is Success) { // SASL authentication was successfull if (OnSaslEnd != null) { OnSaslEnd(this); } m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Authenticated); m_Mechanism = null; m_XmppClient.Reset(); } else if (e is Failure) { // Authentication failure m_XmppClient.FireOnAuthError(e as Element); } }