/// <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);
        }
        public void When_CreatedByParsing_ValueShouldBeValid()
        {
            var attribute = ScramAttribute.Parse("v=rmF9pqV8S7suAoZWja4dJRkFsKQ=");
            var result    = new ServerSignatureAttribute("rmF9pqV8S7suAoZWja4dJRkFsKQ=");

            attribute.ShouldBe(result);
        }
Exemplo n.º 3
0
 public void When_ThereAreMoreParts_ShouldThrowAnException()
 {
     Should.Throw <FormatException>(() =>
     {
         var _ = ScramAttribute.Parse("value");
     });
 }
        public void When_CreatingTwoAttributes_TheHashCodesShouldBeEqual()
        {
            var first  = ScramAttribute.Parse("v=rmF9pqV8S7suAoZWja4dJRkFsKQ=");
            var second = new ServerSignatureAttribute("rmF9pqV8S7suAoZWja4dJRkFsKQ=");

            first.GetHashCode().ShouldBe(second.GetHashCode());
        }
Exemplo n.º 5
0
 public void When_NameIsTwoCharacters_ShouldThrowAnException()
 {
     Should.Throw <FormatException>(() =>
     {
         var _ = ScramAttribute.Parse("bh=name");
     });
 }
Exemplo n.º 6
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.º 7
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.º 8
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.º 9
0
        public void When_ParsingAnUnknownAttribute_ShouldBeValid()
        {
            var attribute = ScramAttribute.Parse("h=name");

            attribute.ShouldBeOfType <UnknownAttribute>();
        }
Exemplo n.º 10
0
        public void When_ParsingAClientProofAttribute_ShouldBeValid()
        {
            var attribute = ScramAttribute.Parse("p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=");

            attribute.ShouldBeOfType <ClientProofAttribute>();
        }
Exemplo n.º 11
0
        public void When_ParsingAChannelAttribute_ShouldBeValid()
        {
            var attribute = ScramAttribute.Parse("c=biws");

            attribute.ShouldBeOfType <ChannelAttribute>();
        }
Exemplo n.º 12
0
        public void When_ParsingAUserAttribute_ShouldBeValid()
        {
            var attribute = ScramAttribute.Parse("n=name");

            attribute.ShouldBeOfType <UserAttribute>();
        }
Exemplo n.º 13
0
        public void When_ParsingAnAuthorizationAttribute_ShouldBeValid()
        {
            var attribute = ScramAttribute.Parse("a=name");

            attribute.ShouldBeOfType <AuthorizationIdentityAttribute>();
        }
Exemplo n.º 14
0
 public void When_ChannelIsNotSupported_ShouldBeValid()
 {
     var _ = ScramAttribute.ParseAll("n,,n=name");
 }
        public void When_AttributeIsNotNull_ShouldBeEqual()
        {
            var attribute = ScramAttribute.Parse("v=rmF9pqV8S7suAoZWja4dJRkFsKQ=");

            Assert.False(attribute.Equals(null));
        }
Exemplo n.º 16
0
 public ServerFirstMessage(int iterations, string nonce, byte[] salt)
 {
     Iterations = new IterationsAttribute(iterations);
     Nonce      = new NonceAttribute(nonce);
     Salt       = new SaltAttribute(salt);
 }
Exemplo n.º 17
0
 private ServerFirstMessage(IterationsAttribute iterations, NonceAttribute nonce, SaltAttribute salt)
 {
     Iterations = iterations;
     Nonce      = nonce;
     Salt       = salt;
 }
        public void When_AttributeIsComparedToItself_ShouldBeEqual()
        {
            var attribute = ScramAttribute.Parse("v=rmF9pqV8S7suAoZWja4dJRkFsKQ=");

            Assert.True(attribute.Equals(attribute));
        }