示例#1
0
        private ServerFeatures DiscoverAuthMechanisms(StreamFeatures features)
        {
            var mechanisms = ServerFeatures.None;

            foreach (string mechanism in features.Mechanisms.Mechanism)
            {
                switch (mechanism)
                {
                case XmppCodes.SaslGoogleXOAuth2Authenticator:
                    mechanisms |= ServerFeatures.SaslGoogleXOAuth2;
                    break;

                case XmppCodes.SaslScramSha1Mechanism:
                    mechanisms |= ServerFeatures.SaslScramSha1;
                    break;

                case XmppCodes.SaslDigestMD5Mechanism:
                    mechanisms |= ServerFeatures.SaslDigestMD5;
                    break;

                case XmppCodes.SaslPlainMechanism:
                    mechanisms |= ServerFeatures.SaslPlain;
                    break;
                }
            }

            return(mechanisms);
        }
示例#2
0
 private async Task HandleStreamFeaturesAsync(StreamFeatures features, CancellationToken cancellationToken)
 {
     if (XmppSessionState.Value < SessionState.Securing && features.SupportsStartTls && Tls)
     {
         await HandleStreamFeaturesAsync(await DoStartTlsAsync(cancellationToken), cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Registering && features.SupportsRegistration && RegistrationHandler?.RegisterNewAccount == true)
     {
         await DoRegisterAsync(cancellationToken);
         await HandleStreamFeaturesAsync(features, cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Authenticating)
     {
         var authRet = await DoAuthenicateAsync(features.Mechanisms, cancellationToken);
         await HandleStreamFeaturesAsync(authRet, cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Compressing && features.SupportsZlibCompression && Compression)
     {
         await HandleStreamFeaturesAsync(await DoEnableCompressionAsync(cancellationToken), cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Binding)
     {
         await DoBindAsync(features, cancellationToken);
     }
 }
示例#3
0
 internal async Task HandleStreamFeaturesAsync(
     StreamFeatures features,
     CancellationToken cancellationToken)
 {
     if (XmppSessionState.Value < SessionState.Securing && features.SupportsStartTls && Tls)
     {
         await HandleStreamFeaturesAsync(await DoStartTlsAsync(cancellationToken), cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Registering && features.SupportsRegistration && RegistrationHandler?.RegisterNewAccount == true)
     {
         await DoRegisterAsync(cancellationToken);
         await HandleStreamFeaturesAsync(features, cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Authenticating)
     {
         var authRet = await DoAuthenicateAsync(features.Mechanisms, cancellationToken);
         await HandleStreamFeaturesAsync(authRet, cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Binding &&
              Pipeline.Contains <StreamManagementHandler>() &&
              Pipeline.Get <StreamManagementHandler>().CanResume &&
              Pipeline.Get <StreamManagementHandler>().StreamId != null)
     {
         await Pipeline.Get <StreamManagementHandler>().ResumeAsync(cancellationToken);
         await HandleStreamFeaturesAsync(features, cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Compressing && features.SupportsZlibCompression && Compression)
     {
         await HandleStreamFeaturesAsync(await DoEnableCompressionAsync(cancellationToken), cancellationToken);
     }
     else if (XmppSessionState.Value < SessionState.Binding)
     {
         await DoBindAsync(features, cancellationToken);
     }
 }
示例#4
0
        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);
        }
示例#5
0
 public void AddStreamFeatures(ServerConnectionHandler serverSession, StreamFeatures features)
 {
     if (serverSession.SessionState < SessionState.Binded)
     {
         features.Add(new Bind());
     }
 }
示例#6
0
文件: XMPP.cs 项目: remobjects/ipxmpp
 protected void OnStreamFeatures(StreamFeatures aFeatures)
 {
     if (StreamFeatures != null)
     {
         StreamFeaturesArgs args = new StreamFeaturesArgs(aFeatures);
         StreamFeatures(this, args);
     }
 }
示例#7
0
 public void AddStreamFeatures(ServerConnectionHandler serverSession, StreamFeatures features)
 {
     ServerSession = serverSession;
     if (serverSession.SessionState < Matrix.SessionState.Secure)
     {
         features.Add(new StartTls());
     }
 }
示例#8
0
        private async Task <StreamFeatures> HandleStreamFeaturesAsync(StreamFeatures features, CancellationToken cancellationToken)
        {
            if (SessionState < SessionState.Securing && features.SupportsStartTls && Tls)
            {
                return(await HandleStreamFeaturesAsync(await DoStartTlsAsync(cancellationToken), cancellationToken));
            }

            return(features);
        }
        public void AddStreamFeatures(ServerConnectionHandler serverSession, StreamFeatures features)
        {
            if (serverSession.SessionState < Matrix.SessionState.Authenticated)
            {
                if (features.Mechanisms == null)
                {
                    features.Mechanisms = new Mechanisms();
                }

                features.Mechanisms.AddMechanism(SaslMechanism.Plain);
            }
        }
        private XmppXElement BuildStreamFeatures(IChannelHandlerContext ctx)
        {
            var feat = new StreamFeatures();

            ctx.Channel.Pipeline
            .Where(h => h.GetType().GetTypeInfo().ImplementedInterfaces.Contains(typeof(IStreamFeature)))
            .Cast <IStreamFeature>()
            .ToList().ForEach(
                sf => sf.AddStreamFeatures(this, feat)
                );

            return(feat);
        }
示例#11
0
文件: XMPP.cs 项目: remobjects/ipxmpp
        private void GotFeatures(StreamFeatures features)
        {
            EndTimeout();
            OnStreamFeatures(features);
            if (fState == XMPP.State.Authenticated)
            {
                if (BindResource)
                {
                    fState = XMPP.State.BindingResource;

                    SendResourceBinding();
                }
                else
                {
                    fState = XMPP.State.Active; // finally!

                    OnActive();
                }
                return;
            }
            fServerMechanisms = features.AuthenticationMechanisms;
            var starttls = features.StartTLS;

            if (!(fConnection is SslConnection))
            {
                switch (InitTLS)
                {
                case InitTLSMode.Always:
                    if (starttls == null)
                    {
                        OnError(new TlsRequiredException("TLS required but not supported by server"));
                        Close();
                        return;
                    }
                    StartTLS();
                    break;

                case InitTLSMode.IfAvailable:
                    StartTLS();
                    break;

                default:
                    StartAuth();
                    break;
                }
            }
            else
            {
                StartAuth();
            }
        }
示例#12
0
        public void SerializeWithBindFeature()
        {
            var exp = @"<stream:features xmlns:stream=""http://etherx.jabber.org/streams"">
                          <bind xmlns=""urn:ietf:params:xml:ns:xmpp-bind""/>
                        </stream:features>";

            var features = new StreamFeatures
            {
                Bind = new Bind()
            };

            var buffer = XmppSerializer.Serialize(features);
            var xml    = XmppEncoding.Utf8.GetString(buffer, 0, buffer.Length);

            Assert.IsTrue(exp.CultureAwareCompare(xml));
        }
示例#13
0
        /// <summary>
        ///   Process an Stream Features XMPP message
        /// </summary>
        /// <param name = "features"></param>
        private void ProcessStreamFeatures(StreamFeatures features)
        {
            if (features.Mechanisms != null && features.Mechanisms.SaslMechanisms.Count > 0)
            {
                foreach (string mechanism in features.Mechanisms.SaslMechanisms)
                {
                    switch (mechanism)
                    {
                    case XmppCodes.SaslDigestMD5Mechanism:
                        streamFeatures |= XmppStreamFeatures.SaslDigestMD5;
                        break;

                    case XmppCodes.SaslPlainMechanism:
                        streamFeatures |= XmppStreamFeatures.SaslPlain;
                        break;

                    case XmppCodes.SaslXGoogleTokenMechanism:
                        streamFeatures |= XmppStreamFeatures.XGoogleToken;
                        break;
                    }
                }
            }

            if (features.Bind != null)
            {
                streamFeatures |= XmppStreamFeatures.ResourceBinding;
            }

            if (features.SessionSpecified)
            {
                streamFeatures |= XmppStreamFeatures.Sessions;
            }

            if (features.Items.Count > 0)
            {
                foreach (object item in features.Items)
                {
                    if (item is RegisterIQ)
                    {
                        streamFeatures |= XmppStreamFeatures.InBandRegistration;
                    }
                }
            }

            streamFeaturesEvent.Set();
        }
示例#14
0
        XmppXElement BuildFeatures()
        {
            var feat = new StreamFeatures();

            if (!IsOnline)
            {
                var mechs = new Mechanisms();
                mechs.AddMechanism(SaslMechanism.PLAIN);
                feat.Mechanisms = mechs;
            }
            else
            {
                feat.Bind    = new Xmpp.Bind.Bind();
                feat.Session = new Xmpp.Session.Session();
            }

            return(feat);
        }
示例#15
0
        private XmppXElement BuildStreamFeatures()
        {
            var feat = new StreamFeatures();

            //feat.Add(new StartTls());

            if (!IsAuthenticated)
            {
                var mechs = new Mechanisms();
                mechs.AddMechanism(SaslMechanism.Plain);
                feat.Mechanisms = mechs;
            }
            else if (!IsBinded && IsAuthenticated)
            {
                feat.Add(new Bind());
            }

            return(feat);
        }
示例#16
0
        private async Task OnNegotiateStreamFeaturesAsync(StreamFeatures features)
        {
            this.serverFeatures = ServerFeatures.None;

            if (features.SecureConnectionRequired)
            {
                this.serverFeatures |= ServerFeatures.SecureConnection;
            }

            if (features.HasAuthMechanisms)
            {
                this.serverFeatures |= this.DiscoverAuthMechanisms(features);
            }

            if (features.SupportsResourceBinding)
            {
                this.serverFeatures |= ServerFeatures.ResourceBinding;
            }

            if (features.SupportsSessions)
            {
                this.serverFeatures |= ServerFeatures.Sessions;
            }

            if (features.SupportsInBandRegistration)
            {
                this.serverFeatures |= ServerFeatures.InBandRegistration;
            }

            if (features.SupportsEntityCapabilities)
            {
                this.serverFeatures |= ServerFeatures.EntityCapabilities;

                this.serverCapabilities.Address = this.UserAddress.DomainName;
                this.serverCapabilities.ServiceDiscoveryNode = features.EntityCapabilities.DiscoveryNode;
            }

            await this.NegotiateStreamFeaturesAsync().ConfigureAwait(false);
        }
示例#17
0
        public void SerializeWithStartTlsFeature()
        {
            var exp = @"<stream:features xmlns:stream=""http://etherx.jabber.org/streams"">
                          <starttls xmlns=""urn:ietf:params:xml:ns:xmpp-tls"">
                             <required />
                          </starttls>
                        </stream:features>";

            var features = new StreamFeatures
            {
                StartTls = new StartTls
                {
                    Required            = new Empty()
                    , RequiredSpecified = true
                }
            };

            var buffer = XmppSerializer.Serialize(features);
            var xml    = XmppEncoding.Utf8.GetString(buffer, 0, buffer.Length);

            Assert.IsTrue(exp.CultureAwareCompare(xml));
        }
示例#18
0
        public void SerializeWithCompressFeature()
        {
            var exp = @"<stream:features xmlns:stream=""http://etherx.jabber.org/streams"">
                          <compression xmlns=""http://jabber.org/features/compress"">
                            <method>zlib</method>
                            <method>lzw</method>
                          </compression>
                        </stream:features>";

            var features = new StreamFeatures
            {
                Compression = new StreamCompressionFeature
                {
                    Methods = new List <string> {
                        "zlib", "lzw"
                    }
                }
            };

            var buffer = XmppSerializer.Serialize(features);
            var xml    = XmppEncoding.Utf8.GetString(buffer, 0, buffer.Length);

            Assert.IsTrue(exp.CultureAwareCompare(xml));
        }
示例#19
0
        XmppXElement BuildFeatures()
        {
            var feat = new StreamFeatures();
            if (!IsOnline)
            {
                var mechs = new Mechanisms();
                mechs.AddMechanism(SaslMechanism.PLAIN);
                feat.Mechanisms = mechs;
            }
            else
            {
                feat.Bind = new Xmpp.Bind.Bind();
                feat.Session = new Xmpp.Session.Session();
            }

            return feat;
        }
示例#20
0
文件: XMPP.cs 项目: remobjects/ipxmpp
        private Element CreateNode(XmlNodeResult nd, Element parent)
        {
            string ens;

            if (nd.Prefix != null)
            {
                RemObjects.InternetPack.XMPP.Elements.Attribute at = nd.Attribute.FirstOrDefault(a => a.Prefix == "xmlns" && a.Name == nd.Prefix);
                if (at == null)
                {
                    Element el = parent;
                    ens = string.Empty;
                    while (el != null)
                    {
                        RemObjects.InternetPack.XMPP.Elements.Attribute els = el.Attributes.Where(a => a.Prefix == "xmlns" && a.Name == nd.Prefix).FirstOrDefault();
                        if (els != null)
                        {
                            ens = els.Value;
                            break;
                        }
                        el = el.Parent;
                    }
                }
                else
                {
                    ens = at.Value;
                }
            }
            else
            {
                RemObjects.InternetPack.XMPP.Elements.Attribute at = nd.Attribute.FirstOrDefault(a => a.Prefix == null && a.Name == "xmlns");
                if (at == null)
                {
                    ens = string.Empty;
                }
                else
                {
                    ens = at.Value;
                }
            }
            Element res = null;

            switch (ens)
            {
            case Namespaces.ClientStreamNamespace:
            case Namespaces.ServerStreamNamespace:
            case "":
                if (ens == null && parent != null && parent.Type == ElementType.IQ && nd.Name == "error")
                {
                    res = new IQError();
                }
                else
                {
                    switch (nd.Name)
                    {
                    case "iq":
                        res = new IQ();
                        break;

                    case "presence":
                        res = new Presence();
                        break;

                    case "message":
                        res = new Message();
                        break;
                    }
                }
                break;

            case Namespaces.StreamNamespace:
                switch (nd.Name)
                {
                case "stream":

                    RemObjects.InternetPack.XMPP.Elements.Attribute att = nd.Attribute.FirstOrDefault(a => a.Prefix == null && a.Name == "xmlns");
                    if (att == null || att.Value == Namespaces.ClientStreamNamespace)
                    {
                        res = new ClientStream();
                    }
                    else
                    {
                        res = new ServerStream();
                    }
                    break;

                case "features":
                    res = new StreamFeatures();
                    break;

                case "error":
                    res = new StreamError();
                    break;
                }
                break;

            case Namespaces.StartTLSNamespace:
                switch (nd.Name)
                {
                case "starttls":
                    res = new StartTLS();
                    break;

                case "failure":
                    res = new StartTLSFailure();
                    break;

                case "proceed":
                    res = new StartTLSProceed();
                    break;
                }

                break;

            case Namespaces.SaslNamespace:
                switch (nd.Name)
                {
                case "mechanisms":
                    res = new Mechanisms();
                    break;

                case "auth":
                    res = new SaslAuth();
                    break;

                case "challenge":
                    res = new SaslChallenge();
                    break;

                case "response":
                    res = new SaslResponse();
                    break;

                case "abort":
                    res = new SaslAbort();
                    break;

                case "success":
                    res = new SaslSuccess();
                    break;

                case "failure":
                    res = new SaslFailure();
                    break;
                }
                break;
            }
            if (res == null)
            {
                res = new UnknownElement();
            }
            else
            {
                res.Attributes.Clear(); // default ones shouldn't be here during the reading process
            }
            if (parent != null)
            {
                res.Parent = parent;
                if (parent != fServerRoot)
                {
                    parent.Elements.Add(res);
                }
            }
            res.Prefix = nd.Prefix;
            res.Name   = nd.Name;
            foreach (var el in nd.Attribute)
            {
                res.Attributes.Add(el);
            }
            return(res);
        }
示例#21
0
文件: XMPP.cs 项目: remobjects/ipxmpp
 public StreamFeaturesArgs(StreamFeatures features)
 {
     fFeatures = features;
 }