public async Task TestDigest()
        {
            string authType       = DigestGenerator.AuthorizationHeaderMarker;
            string userName       = "******";
            string password       = "******";
            string relativeUri    = "/api/auth/digest";
            Uri    fullUri        = new Uri(baseUri, relativeUri);
            string initialRequest = CreateInitialRequest(fullUri);

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Connect("localhost", port);
                HttpResponse initialResponse = await SendRequestAndGetResponse(socket, initialRequest);

                Assert.That(initialResponse.StatusCode, Is.EqualTo(401));
                Assert.That(initialResponse.Headers.ContainsKey("WWW-Authenticate"));

                string authHeader = GetDesiredAuthHeader(authType, initialResponse);
                Assert.That(authHeader, Is.Not.Null);

                DigestGenerator generator          = new DigestGenerator(userName, password, "GET", relativeUri, authHeader);
                string          authorizeRequest   = CreateRequest(fullUri, generator.GenerateAuthorizationHeader());
                HttpResponse    authorizedResponse = await SendRequestAndGetResponse(socket, authorizeRequest);

                Assert.That(authorizedResponse.StatusCode, Is.EqualTo(200));
                socket.Close();
            }
        }
Example #2
0
        public void TestDigest()
        {
            string          expectedHeaderValue = "Digest username=\"user.name\", realm=\"test.dev\", nonce=\"064af982c5b571cea6450d8eda91c20d\", uri=\"/login\", response=\"70eda34f1683041fd9ab72056c51b740\", qop=auth, nc=00000001, cnonce=\"61417766e50cb980\", algorithm=MD5, opaque=\"d8ea7aa61a1693024c4cc3a516f49b3c\"";
            DigestGenerator generator           = new DigestGenerator(userName, password, httpVerb, url, "61417766e50cb980", 1, authenticationHeader);
            string          expectedResponse    = "70eda34f1683041fd9ab72056c51b740";

            Assert.That(generator.GetAuthorizationValue(), Is.EqualTo(expectedResponse));
            Assert.That(generator.GenerateAuthorizationHeader(), Is.EqualTo(expectedHeaderValue));
        }
        public async Task GivenKnownDataDigestIsCorrectlyCalculatedIetfTest()
        {
            // Arrange
            var content = new StringContent("{\"hello\": \"world\"}");

            DigestGenerator generator = new DigestGenerator("SHA-256");

            // Act
            var result = generator.CalculateDigest(await content.ReadAsStreamAsync());

            // Assert
            Assert.Equal("SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=", result);
        }
Example #4
0
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            if (HasChanged)
            {
                writer.WriteStartElement(GetType().Name);
                writer.WriteAttributeString("name", Name);

                DigestGenerator.WriteDigestEntry(writer, "Tile1", Default.Items[0], Items[0]);
                DigestGenerator.WriteDigestEntry(writer, "Tile2", Default.Items[1], Items[1]);
                DigestGenerator.WriteDigestEntry(writer, "Tile3", Default.Items[2], Items[2]);
                DigestGenerator.WriteDigestEntry(writer, "Tile4", Default.Items[3], Items[3]);

                writer.WriteEndElement();
            }
        }
        public async Task UseTheSpecificationToGetTheHashAlgorithm()
        {
            // Arrange
            var content = new StringContent("{\"hello\": \"world\"}");
            ISignatureSpecification signatureSpecification = new SignatureSpecification()
            {
                HashAlgorithm = "SHA-256"
            };

            DigestGenerator generator = new DigestGenerator(signatureSpecification);

            // Act
            var result = generator.CalculateDigest(await content.ReadAsStreamAsync());

            // Assert
            Assert.Equal("SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=", result);
        }
Example #6
0
 public void WriteXmlDigest(System.Xml.XmlWriter writer)
 {
     if (HasChanged)
     {
         writer.WriteStartElement(this.GetType().Name);
         writer.WriteAttributeString("changed", HasChanged.ToString());
         foreach (PoachProbability p in PoachProbabilities)
         {
             if (p.HasChanged)
             {
                 writer.WriteStartElement(p.GetType().Name);
                 writer.WriteAttributeString("name", p.MonsterName);
                 DigestGenerator.WriteXmlDigest(p, writer, false, true);
             }
         }
         writer.WriteEndElement();
     }
 }
Example #7
0
 public void WriteXmlDigest(System.Xml.XmlWriter writer, FFTPatch FFTPatch)
 {
     if (HasChanged)
     {
         writer.WriteStartElement(this.GetType().Name);
         writer.WriteAttributeString("changed", HasChanged.ToString());
         foreach (Job j in Jobs)
         {
             if (j.HasChanged)
             {
                 writer.WriteStartElement(j.GetType().Name);
                 writer.WriteAttributeString("value", j.Value.ToString("X2"));
                 writer.WriteAttributeString("name", j.Name);
                 DigestGenerator.WriteXmlDigest(j, writer, false, true);
             }
         }
         writer.WriteEndElement();
     }
 }
        public async Task GivenKnownDataDigestIsCorrectlyCalculatedTellerTest()
        {
            // Arrange
            Dictionary <string, string> data = new Dictionary <string, string>
            {
                { "bank_code", "00-00-00" },
                { "account_number", "00000000" },
                { "amount", "0.01" }
            };

            var             content   = new FormUrlEncodedContent(data);
            DigestGenerator generator = new DigestGenerator("SHA256");

            // Act
            var result = generator.CalculateDigest(await content.ReadAsStreamAsync());

            // Assert
            Assert.Equal("SHA256=dX9LYG6i/d+TuzG0QMckFzqOZ6Wll/TlGGjUtqGyMhQ=", result);
        }
Example #9
0
 public void WriteXmlDigest(System.Xml.XmlWriter writer, FFTPatch FFTPatch)
 {
     if (HasChanged)
     {
         writer.WriteStartElement(GetType().Name);
         writer.WriteAttributeString("value", this.ToString());
         writer.WriteAttributeString("changed", HasChanged.ToString());
         for (int i = 0; i < 16; i++)
         {
             if (Units[i].HasChanged)
             {
                 writer.WriteStartElement(typeof(EventUnit).Name);
                 writer.WriteAttributeString("value", i.ToString());
                 DigestGenerator.WriteXmlDigest(Units[i], writer, false, true);
             }
         }
         writer.WriteEndElement();
     }
 }
Example #10
0
 public void WriteXml(System.Xml.XmlWriter writer)
 {
     if (HasChanged)
     {
         writer.WriteStartElement(this.GetType().Name);
         writer.WriteAttributeString("changed", HasChanged.ToString());
         foreach (MonsterSkill m in MonsterSkills)
         {
             if (m.HasChanged)
             {
                 writer.WriteStartElement(m.GetType().Name);
                 writer.WriteAttributeString("value", m.Value.ToString("X2"));
                 writer.WriteAttributeString("name", m.Name);
                 DigestGenerator.WriteXmlDigest(m, writer, false, true);
             }
         }
         writer.WriteEndElement();
     }
 }
Example #11
0
        public void WriteXmlDigest(System.Xml.XmlWriter writer)
        {
            if (HasChanged)
            {
                writer.WriteStartElement(this.GetType().Name);
                writer.WriteAttributeString("changed", HasChanged.ToString());
                foreach (StatusAttribute attr in StatusAttributes)
                {
                    if (attr.HasChanged)
                    {
                        writer.WriteStartElement(attr.GetType().Name);
                        writer.WriteAttributeString("name", attr.Name);
                        writer.WriteAttributeString("value", attr.Value.ToString("X2"));
                        DigestGenerator.WriteXmlDigest(attr, writer, false, true);
                    }
                }

                writer.WriteEndElement();
            }
        }
Example #12
0
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            if (HasChanged)
            {
                writer.WriteStartElement(this.GetType().Name);
                writer.WriteAttributeString("changed", HasChanged.ToString());
                foreach (ActionMenu a in ActionMenus)
                {
                    if (a.HasChanged)
                    {
                        writer.WriteStartElement(a.GetType().Name);
                        writer.WriteAttributeString("value", a.Value.ToString("X2"));
                        writer.WriteAttributeString("name", a.Name);
                        DigestGenerator.WriteXmlDigest(a, writer, false, true);
                    }
                }

                writer.WriteEndElement();
            }
        }
Example #13
0
 public void WriteXmlDigest(System.Xml.XmlWriter writer, FFTPatch FFTPatch)
 {
     if (HasChanged)
     {
         writer.WriteStartElement(this.GetType().Name);
         writer.WriteAttributeString("changed", HasChanged.ToString());
         foreach (ItemAttributes attr in ItemAttributes)
         {
             if (attr.HasChanged)
             {
                 writer.WriteStartElement(attr.GetType().Name);
                 writer.WriteAttributeString("value", attr.Value.ToString("X2"));
                 DigestGenerator.WriteXmlDigest(attr, writer, false, false);
                 writer.WriteElementString("CorrespondingItems", attr.GetCorrespondingItems(FFTPatch));
                 writer.WriteEndElement();
             }
         }
         writer.WriteEndElement();
     }
 }
Example #14
0
 public void WriteXml(System.Xml.XmlWriter writer)
 {
     if (HasChanged)
     {
         writer.WriteStartElement(this.GetType().Name);
         writer.WriteAttributeString("changed", HasChanged.ToString());
         foreach (SkillSet s in SkillSets)
         {
             if (s.HasChanged)
             {
                 writer.WriteStartElement(s.GetType().Name);
                 writer.WriteAttributeString("value", s.Value.ToString("X2"));
                 writer.WriteAttributeString("name", s.Name);
                 DigestGenerator.WriteXmlDigest(s, writer, false, false);
                 writer.WriteElementString("CorrespondingJobs", s.CorrespondingJobs);
                 writer.WriteEndElement();
             }
         }
         writer.WriteEndElement();
     }
 }
Example #15
0
        public void WriteXmlDigest(System.Xml.XmlWriter writer)
        {
            if (HasChanged)
            {
                writer.WriteStartElement(GetType().Name);
                writer.WriteAttributeString("changed", HasChanged.ToString());
                foreach (Item i in Items)
                {
                    if (i.HasChanged)
                    {
                        writer.WriteStartElement(i.GetType().Name);
                        writer.WriteAttributeString("name", i.Name);
                        DigestGenerator.WriteXmlDigest(i, writer, false, false);
                        if (i is Weapon)
                        {
                            Weapon w = i as Weapon;
                            if (w.Formula.Value == 0x02 &&
                                (w.Formula.Value != w.WeaponDefault.Formula.Value || w.InflictStatus != w.WeaponDefault.InflictStatus))
                            {
                                writer.WriteStartElement("CastSpell");
                                writer.WriteAttributeString("default", AllAbilities.Names[w.WeaponDefault.InflictStatus]);
                                writer.WriteAttributeString("value", AllAbilities.Names[w.InflictStatus]);
                                writer.WriteEndElement();
                            }
                            else if (w.InflictStatus != w.WeaponDefault.InflictStatus)
                            {
                                writer.WriteStartElement("InflictStatusDescription");
                                writer.WriteAttributeString("default", FFTPatch.InflictStatuses.InflictStatuses[w.WeaponDefault.InflictStatus].Statuses.ToString());
                                writer.WriteAttributeString("value", FFTPatch.InflictStatuses.InflictStatuses[w.InflictStatus].Statuses.ToString());
                                writer.WriteEndElement();
                            }
                        }

                        writer.WriteEndElement();
                    }
                }
                writer.WriteEndElement();
            }
        }
Example #16
0
 public void WriteXmlDigest(System.Xml.XmlWriter writer, FFTPatch FFTPatch)
 {
     if (HasChanged)
     {
         writer.WriteStartElement(this.GetType().Name);
         writer.WriteAttributeString("changed", HasChanged.ToString());
         foreach (InflictStatus i in InflictStatuses)
         {
             if (i.HasChanged)
             {
                 writer.WriteStartElement(i.GetType().Name);
                 writer.WriteAttributeString("value", i.Value.ToString("X2"));
                 DigestGenerator.WriteXmlDigest(i, writer, false, false);
                 writer.WriteElementString("CorrespondingAbilities", i.GetCorrespondingAbilities(FFTPatch));
                 writer.WriteElementString("CorrespondingChemistItems", i.GetCorrespondingChemistItems(FFTPatch));
                 writer.WriteElementString("CorrespondingWeapons", i.GetCorrespondingWeapons(FFTPatch));
                 writer.WriteEndElement();
             }
         }
         writer.WriteEndElement();
     }
 }
        /// <summary>
        /// Handles the OnResponseReceived for the proxy, which occurs after the response is
        /// received from the web server, but before it is forwarded on to the browser.
        /// </summary>
        /// <param name="context">A <see cref="BenderProxy.ProcessingContext"/> object.</param>
        public override void OnResponseReceived(ProcessingContext context)
        {
            // These are valid credentials and other necessary initial data
            // for the Digest case.
            string userName        = "******";
            string password        = "******";
            string httpMethod      = context.RequestHeader.Method;
            string hostHeaderValue = context.RequestHeader.Host;
            string requestUri      = context.RequestHeader.RequestURI;
            int    hostIndex       = requestUri.IndexOf(hostHeaderValue);

            if (hostIndex >= 0)
            {
                requestUri = requestUri.Substring(hostIndex + hostHeaderValue.Length);
            }

            // Only do any processing on the response if the response is 401,
            // or "Unauthorized".
            if (context.ResponseHeader != null && context.ResponseHeader.StatusCode == 401)
            {
                // Read the headers from the response and finish reading the response
                // body, if any.
                Console.WriteLine("Received 401 - Unauthorized response");
                context.ServerStream.ReadTimeout  = 5000;
                context.ServerStream.WriteTimeout = 5000;
                StreamReader     reader       = new StreamReader(context.ServerStream);
                HttpHeaderReader headerReader = new HttpHeaderReader(reader);
                if (context.ResponseHeader.EntityHeaders.ContentLength != 0)
                {
                    string drainBody = ReadFromStream(reader);
                }

                // We do not want the proxy to do any further processing after
                // handling this message.
                context.StopProcessing();


                // Read the WWW-Authenticate header. Because of the way the test
                // web app is configured, it returns multiple headers, with
                // different schemes. We need to select the correct one.
                string authHeader = GetAuthenticationHeader(context.ResponseHeader.WWWAuthenticate, DigestGenerator.AuthorizationHeaderMarker);
                Console.WriteLine("Processing WWW-Authenticate header: {0}", authHeader);

                // Calculate the value for the Authorization header, and resend
                // the request (with the Authorization header) to the server
                // using BenderProxy's HttpMessageWriter.
                Console.WriteLine("Generating authorization header value for user name '{0}' and password '{1}'", userName, password);
                DigestGenerator generator = new DigestGenerator(userName, password, httpMethod, requestUri, authHeader);
                string          authorizationHeaderValue = generator.GenerateAuthorizationHeader();
                Console.WriteLine("Resending request with Authorization header: {0}", authorizationHeaderValue);
                context.RequestHeader.Authorization = authorizationHeaderValue;
                HttpMessageWriter writer = new HttpMessageWriter(context.ServerStream);
                writer.Write(context.RequestHeader);

                // Get the authorized response, and forward it on to the browser, using
                // BenderProxy's HttpHeaderReader and support classes.
                HttpResponseHeader header     = new HttpResponseHeader(headerReader.ReadHttpMessageHeader());
                string             body       = ReadFromStream(reader);
                Stream             bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(body));
                new HttpResponseWriter(context.ClientStream).Write(header, bodyStream, bodyStream.Length);
            }
        }
Example #18
0
        public void WriteXml(XmlWriter writer)
        {
            if (HasChanged)
            {
                writer.WriteStartElement(GetType().Name);

                writer.WriteAttributeString("value", Offset.ToString("X4"));
                writer.WriteAttributeString("name", Name);
                DigestGenerator.WriteXmlDigest(this, writer, false, false);
                DigestGenerator.WriteXmlDigest(AIFlags, writer, true, true);

                if (IsNormal)
                {
                    DigestGenerator.WriteDigestEntry(writer, "Effect", Default.Effect, Effect);
                    DigestGenerator.WriteXmlDigest(Attributes, writer, true, true);
                    if (Attributes.Formula.Value == 0x02 &&
                        (Attributes.Formula.Value != Attributes.Default.Formula.Value || Attributes.InflictStatus != Attributes.Default.InflictStatus))
                    {
                        writer.WriteStartElement("CastSpell");
                        writer.WriteAttributeString("default", AllAbilities.Names[Attributes.Default.InflictStatus]);
                        writer.WriteAttributeString("value", AllAbilities.Names[Attributes.InflictStatus]);
                        writer.WriteEndElement();
                    }
                    else if (Attributes.InflictStatus != Attributes.Default.InflictStatus)
                    {
                        writer.WriteStartElement("InflictStatusDescription");
                        writer.WriteAttributeString("default", FFTPatch.InflictStatuses.InflictStatuses[Attributes.Default.InflictStatus].Statuses.ToString());
                        writer.WriteAttributeString("value", FFTPatch.InflictStatuses.InflictStatuses[Attributes.InflictStatus].Statuses.ToString());
                        writer.WriteEndElement();
                    }
                }
                else if (IsItem)
                {
                    DigestGenerator.WriteDigestEntry(writer, "ItemOffset", Default.ItemOffset, ItemOffset, "0x{0:X2}");
                }
                else if (IsThrowing)
                {
                    DigestGenerator.WriteDigestEntry(writer, "Throwing", Default.Throwing, Throwing);
                }
                else if (IsJumping)
                {
                    DigestGenerator.WriteDigestEntry(writer, "JumpHorizontal", Default.JumpHorizontal, JumpHorizontal);
                    DigestGenerator.WriteDigestEntry(writer, "JumpVertical", Default.JumpVertical, JumpVertical);
                }
                else if (IsCharging)
                {
                    DigestGenerator.WriteDigestEntry(writer, "ChargeCT", Default.ChargeCT, ChargeCT);
                    DigestGenerator.WriteDigestEntry(writer, "ChargeBonus", Default.ChargeBonus, ChargeBonus);
                }
                else if (IsArithmetick)
                {
                    DigestGenerator.WriteDigestEntry(writer, "ArithmetickSkill", Default.ArithmetickSkill, ArithmetickSkill, "0x{0:X2}");
                }
                else if (IsOther)
                {
                    DigestGenerator.WriteDigestEntry(writer, "OtherID", Default.OtherID, OtherID, "0x{0:X2}");
                }

                writer.WriteEndElement();
            }
        }