/// <summary>
        ///     Try and parse the final server message.
        /// </summary>
        /// <param name="message">Message to parse.</param>
        /// <param name="finalMessage"><see cref="ServerFinalMessage"/> instance of the message.</param>
        /// <returns>true if the parsing succeeded; otherwise false.</returns>
        public static bool TryParse(string message, out ServerFinalMessage finalMessage)
        {
            finalMessage = new ServerFinalMessage();

            try
            {
                var attributes = ScramAttribute.ParseAll(message);

                if (!attributes.OfType <ServerSignatureAttribute>().Any())
                {
                    return(false);
                }

                foreach (var attribute in attributes)
                {
                    switch (attribute)
                    {
                    case ServerSignatureAttribute a:
                        finalMessage.ServerSignature = a;
                        break;

                    case ErrorAttribute a:
                        return(false);
                    }
                }
            }
            catch (FormatException)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Parse the first server message.
        /// </summary>
        /// <param name="message">Message to parse.</param>
        /// <param name="firstMessage"><see cref="ServerFirstMessage"/> instance of the message.</param>
        /// <returns>true if parsing was successful; otherwise false.</returns>
        public static bool TryParse(string message, out ServerFirstMessage firstMessage)
        {
            firstMessage = new ServerFirstMessage();

            try
            {
                var attributes = ScramAttribute.ParseAll(message);

                if (!attributes.OfType <IterationsAttribute>().Any() ||
                    !attributes.OfType <NonceAttribute>().Any() ||
                    !attributes.OfType <SaltAttribute>().Any())
                {
                    return(false);
                }

                foreach (var attribute in attributes)
                {
                    switch (attribute)
                    {
                    case IterationsAttribute a:
                        firstMessage.Iterations = a;
                        break;

                    case NonceAttribute a:
                        firstMessage.Nonce = a;
                        break;

                    case SaltAttribute a:
                        firstMessage.Salt = a;
                        break;

                    case ErrorAttribute a:
                        return(false);
                    }
                }
            }
            catch (FormatException)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 3
0
        public static ServerFinalMessage ParseResponse(string response)
        {
            var parts = ScramAttribute.ParseAll(response.Split(','));

            var error = parts.OfType <ErrorAttribute>().ToList();

            if (error.Any())
            {
                throw new InvalidOperationException();
            }

            var signature = parts.OfType <ServerSignatureAttribute>().ToList();

            if (!signature.Any())
            {
                throw new InvalidOperationException();
            }

            return(new ServerFinalMessage(signature.First()));
        }
Exemplo n.º 4
0
        public static ServerFirstMessage ParseResponse(string response)
        {
            var parts = ScramAttribute.ParseAll(response.Split(','));

            var errors = parts.OfType <ErrorAttribute>();

            if (errors.Any())
            {
                throw new InvalidOperationException();
            }

            var iterations = parts.OfType <IterationsAttribute>().ToList();
            var nonces     = parts.OfType <NonceAttribute>().ToList();
            var salts      = parts.OfType <SaltAttribute>().ToList();

            if (!iterations.Any() || !nonces.Any() || !salts.Any())
            {
                throw new InvalidOperationException();
            }

            return(new ServerFirstMessage(iterations.First(), nonces.First(), salts.First()));
        }
Exemplo n.º 5
0
 public void When_ChannelIsNotSupported_ShouldBeValid()
 {
     var _ = ScramAttribute.ParseAll("n,,n=name");
 }