Ejemplo n.º 1
0
        public void EncodeCityHmacSignatureTest()
        {
            HmacEncoder target = new HmacEncoder(); // TODO: Initialize to an appropriate value
            string input = "1000000GEThttps://api.onthecity.org/groups/index"; // TODO: Initialize to an appropriate value
            string key = "1111111111111111111111111111111111111111"; // TODO: Initialize to an appropriate value
            string expected = "IT4oCvIuEF%2FdfJis%2FORNA5eig920vGp9zEo6k5MqRGU%3D"; // TODO: Initialize to an appropriate value

            string unicodeResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.Unicode);
            string asciiResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.ASCII);
            string bigEndianUnicodeResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.BigEndianUnicode);
            string defaultResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.Default);
            string utf32Result = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.UTF32);
            string utf7Result = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.UTF7);
            string utf8Result = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.UTF8);
            //string sbcsCodePageResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.GetEncodings().);

            Dictionary<string, string> encodingResults = new Dictionary<string, string>();
            List<string> workingEncodings = new List<string>();
            List<string> nonWorkingEncodings = new List<string>();

            foreach (var enc in System.Text.Encoding.GetEncodings())
            {
                string result = target.EncodeCityHmacSignature(input, key, enc.GetEncoding());
                encodingResults[enc.Name + " - " + enc.DisplayName] = result;

                if (result == expected)
                {
                    workingEncodings.Add(enc.Name + " - " + enc.DisplayName);
                }
                else
                {
                    nonWorkingEncodings.Add(enc.Name + " - " + enc.DisplayName);
                }
            }
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Ejemplo n.º 2
0
        internal string ExecuteGetRequest(CityAPISession session, string relativeUrl)
        {
            int timeSecs = GetUnixEpochTimeSecs();

            if (!relativeUrl.StartsWith("/"))
            {
                relativeUrl = "/" + relativeUrl;
            }

            string cityApiHost = "api.onthecity.org";
            string absoluteUrl = string.Format("https://{0}{1}", cityApiHost, relativeUrl);

            // city API requests require query string params to be in alphabetical order
            Uri absoluteUri = new Uri(absoluteUrl);
            string queryString = absoluteUri.Query;

            if (!string.IsNullOrEmpty(queryString))
            {
                var queryParams = System.Web.HttpUtility.ParseQueryString(queryString);
                List<string> sortedSegments = new List<string>();

                queryString = string.Join("&", queryParams.AllKeys.OrderBy(k => k).ToList().Select(key => key + "=" + queryParams[key]).ToArray());
                relativeUrl = absoluteUri.AbsolutePath + "?" + queryString;
                absoluteUrl = string.Format("https://{0}{1}", cityApiHost, relativeUrl);
            }

            string httpVerb = "GET";

            string constructedSignatureUrl = ConstructHmacUrl(timeSecs, httpVerb, absoluteUrl);
            string hmacSignature = new HmacEncoder().EncodeCityHmacSignature(constructedSignatureUrl, session.SecretKey);

            System.Net.HttpWebRequest wc = (HttpWebRequest)WebRequest.Create(absoluteUrl);
            wc.KeepAlive = false;
            wc.Headers.Clear();
            wc.Headers.Add("X-City-Sig", hmacSignature);
            wc.Headers.Add("X-City-User-Token", session.UserToken);
            wc.Headers.Add("X-City-Time", timeSecs.ToString());
            wc.Accept = "application/vnd.thecity.admin.v1+json";
            wc.ContentType = "application/json";
            wc.ContentLength = 0;
            wc.UserAgent = "TheCity .NET API, Brandon Potter";

            // uncomment to use Fiddler
            //wc.Proxy = new WebProxy("127.0.0.1", 8888);

            try
            {
                HttpWebResponse resp = (HttpWebResponse)wc.GetResponse();
                string stringResponse = null;

                using (Stream s = resp.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(s))
                    {
                        var responseText = reader.ReadToEnd();
                        stringResponse = responseText;
                    }
                }

                ProcessAPILimitResponseHeaders(resp.Headers, session);

                return stringResponse;
            }
            catch (WebException ex)
            {
                HttpWebResponse resp = (HttpWebResponse)ex.Response;
                using (Stream s = resp.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(s))
                    {
                        var responseText = reader.ReadToEnd();
                        Util.JsonParser jp = new Util.JsonParser();
                        var result = jp.Parse(responseText);
                        string errorMsg = result.error_message;

                        //if (errorMsg.Contains("signature"))
                        //{
                        if (System.Diagnostics.Debugger.IsAttached)
                        {
                            throw new Exception(ex.Message + System.Environment.NewLine + errorMsg + System.Environment.NewLine + System.Environment.NewLine + BuildSignatureDebugInfo(constructedSignatureUrl, timeSecs.ToString(),
                                session.UserToken, session.SecretKey, hmacSignature));
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(errorMsg))
                            {
                                throw new Exception(ex.Message);
                            }
                            else
                            {
                                throw new Exception(errorMsg);
                            }
                        }
                        //}

                        // throw new Exception(errorMsg ?? responseText);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void EncodeCityHmacSignatureTestWithQueryString()
        {
            HmacEncoder target = new HmacEncoder(); // TODO: Initialize to an appropriate value
            string input = "1000000GEThttps://api.onthecity.org/groups?search=Adventure"; // TODO: Initialize to an appropriate value
            string key = "1111111111111111111111111111111111111111"; // TODO: Initialize to an appropriate value
            string expected = "mIGAq0V1OARP16E8wj2US5tU4xgOflQwYKIhK8zVhlQ%3D"; // TODO: Initialize to an appropriate value

            string unicodeResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.Unicode);
            string asciiResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.ASCII);
            string bigEndianUnicodeResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.BigEndianUnicode);
            string defaultResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.Default);
            string utf32Result = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.UTF32);
            string utf7Result = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.UTF7);
            string utf8Result = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.UTF8);
            //string sbcsCodePageResult = target.EncodeCityHmacSignature(input, key, System.Text.Encoding.GetEncodings().);

            Dictionary<string, string> encodingResults = new Dictionary<string, string>();
            List<string> workingEncodings = new List<string>();
            List<string> nonWorkingEncodings = new List<string>();

            foreach (var enc in System.Text.Encoding.GetEncodings())
            {
                string result = target.EncodeCityHmacSignature(input, key, enc.GetEncoding());
                encodingResults[enc.Name + " - " + enc.DisplayName] = result;

                if (result == expected)
                {
                    workingEncodings.Add(enc.Name + " - " + enc.DisplayName);
                }
                else
                {
                    nonWorkingEncodings.Add(enc.Name + " - " + enc.DisplayName);
                }
            }
            Assert.Inconclusive("Verify the correctness of this test method.");
        }