public void ResultShouldBeEmptyString()
        {
            var dict = new Dictionary <string, object>();

            var result = FacebookUtils.ToJsonQueryString(dict);

            Assert.Equal(string.Empty, result);
        }
        public void ResultShouldNotBeEmptyStringOrNull()
        {
            var dict = new Dictionary <string, object>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var result = FacebookUtils.ToJsonQueryString(dict);

            Assert.False(string.IsNullOrEmpty(result));
        }
Exemplo n.º 3
0
        protected internal override Uri GetLoginUrl(IFacebookApplication settings, HttpContextBase httpContext, IDictionary <string, object> parameters)
        {
            var authorizer = new CanvasAuthorizer(settings, httpContext)
            {
                ReturnUrlPath    = ReturnUrlPath,
                CancelUrlPath    = CancelUrlPath,
                LoginDisplayMode = LoginDisplayMode
            };

            if (!String.IsNullOrEmpty(Permissions))
            {
                authorizer.Permissions = Permissions.Replace(" ", String.Empty).Split(',');
            }

            if (string.IsNullOrEmpty(CancelUrlPath))
            {
                // set it to this same url instead of going to facebook.com
                var canvasUrlBuilder    = new CanvasUrlBuilder(settings, httpContext.Request);
                var currentPathAndQuery = canvasUrlBuilder.CurrentCanvasPathAndQuery;

                if (currentPathAndQuery.Contains("?"))
                {
                    var parts = currentPathAndQuery.Split('?');
                    if (parts.Length == 2 && !string.IsNullOrEmpty(parts[1]))
                    {
                        var queryStrings = FacebookUtils.ParseUrlQueryString(parts[1]);

                        // remove oauth 2 error querystrings.
                        // error_reason=user_denied&error_denied=access_denied&error_description=The+user+denied+your+request.
                        if (queryStrings.ContainsKey("error_reason"))
                        {
                            queryStrings.Remove("error_reason");
                        }

                        if (queryStrings.ContainsKey("error_denied"))
                        {
                            queryStrings.Remove("error_denied");
                        }

                        if (queryStrings.ContainsKey("error_description"))
                        {
                            queryStrings.Remove("error_description");
                        }

                        currentPathAndQuery = parts[0] + "?" + FacebookUtils.ToJsonQueryString(queryStrings);
                    }
                }

                authorizer.CancelUrlPath = currentPathAndQuery;
            }

            return(authorizer.GetLoginUrl(null));
        }
        public void ResultShouldBeQuerystringFormatted()
        {
            var dict = new Dictionary <string, object>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
            var exepected = "key1=value1&key2=value2";

            var result = FacebookUtils.ToJsonQueryString(dict);

            // TODO: check key and values that needs to be encoded too
            Assert.Equal(exepected, result);
        }
Exemplo n.º 5
0
        public void ItShouldBeDecodedWithSquareBrackets()
        {
            var dict = new Dictionary <string, object>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", new List <string> {
                      "list_item1", "list_item2"
                  } }
            };

            // key1=value1&key2=value2&key3=["list_item1","list_item2"]
            var expected = "key1=value1&key2=value2&key3=%5B%22list_item1%22%2C%22list_item2%22%5D";

            var result = FacebookUtils.ToJsonQueryString(dict);

            Assert.Equal(expected, result);
        }
        public void ShouldSerializeItCorrectly()
        {
            dynamic attachment = new ExpandoObject();

            attachment.name = "my attachment";
            attachment.href = "http://apps.facebook.com/canvas";

            dynamic parameters = new ExpandoObject();

            parameters.method     = "stream.publish";
            parameters.message    = "my message";
            parameters.attachment = attachment;

#if SILVERLIGHT
            string result = FacebookUtils.ToJsonQueryString((IDictionary <string, object>)parameters);
#else
            string result = FacebookUtils.ToJsonQueryString(parameters);
#endif
            Assert.Equal("method=stream.publish&message=my%20message&attachment=%7B%22name%22%3A%22my%20attachment%22%2C%22href%22%3A%22http%3A%2F%2Fapps.facebook.com%2Fcanvas%22%7D", result);
        }
        protected Uri GetUrl(HttpContextBase context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (context.Request == null)
            {
                throw new Exception("context.Request is null");
            }

            // TODO: need unit tests for this method, might as well need to refactor this method.
            UriBuilder redirectUriBuilder;

            if (!context.Request.QueryString.AllKeys.Contains("state"))
            {
                // todo: better to redirect to the default canvas page.
                return(new Uri("http://www.facebook.com"));
            }

            // if state is present.
            var state = Encoding.UTF8.GetString(FacebookWebUtils.Base64UrlDecode(context.Request.QueryString["state"]));
            var json  = (IDictionary <string, object>)JsonSerializer.Current.DeserializeObject(state);

            // make it one letter character so more info can fit in.
            // r -> return_url_path (full uri)
            // c -> cancel_url_path (full uri)
            // s -> user_state
            FacebookOAuthResult oauthResult;

            if (!FacebookOAuthResult.TryParse(context.Request.Url, out oauthResult))
            {
                // todo: better to redirect to the default canvas page.
                return(new Uri("http://www.facebook.com"));
            }

            if (oauthResult.IsSuccess)
            {
                var returnUrl = json["r"].ToString();

                redirectUriBuilder = new UriBuilder(returnUrl);

                if (returnUrl.Contains("?"))
                {
                    // incase return url path contains querystrings.
                    var returnUrlParts = returnUrl.Split('?');
                    if (returnUrlParts.Length == 2 && !string.IsNullOrEmpty(returnUrlParts[1]))
                    {
                        var queryStrings = FacebookUtils.ParseUrlQueryString(returnUrlParts[1]);

                        if (queryStrings.ContainsKey("error_reason"))
                        {
                            // remove oauth stuffs.
                            if (queryStrings.ContainsKey("error_reason"))
                            {
                                queryStrings.Remove("error_reason");
                            }

                            if (queryStrings.ContainsKey("error"))
                            {
                                queryStrings.Remove("error");
                            }

                            if (queryStrings.ContainsKey("error_description"))
                            {
                                queryStrings.Remove("error_description");
                            }

                            redirectUriBuilder.Query = FacebookUtils.ToJsonQueryString(queryStrings);
                        }
                    }
                }
            }
            else
            {
                if (!json.ContainsKey("c"))
                {
                    // there is no cancel url path
                    redirectUriBuilder = new UriBuilder("http://facebook.com");
                }
                else
                {
                    var cancelUrl = json["c"].ToString();

                    IDictionary <string, object> cancelUrlQueryStrings = new Dictionary <string, object>
                    {
                        { "error_reason", context.Request.QueryString["error_reason"] },
                        { "error", context.Request.QueryString["error"] },
                        { "error_description", context.Request.QueryString["error_description"] }
                    };

                    if (cancelUrl.Contains("?"))
                    {
                        // incase cancel url path contains querystrings.
                        var cancelUrlParts = cancelUrl.Split('?');
                        if (cancelUrlParts.Length == 2 && !string.IsNullOrEmpty(cancelUrlParts[1]))
                        {
                            var queryStrings = FacebookUtils.ParseUrlQueryString(cancelUrlParts[1]);
                            cancelUrlQueryStrings = FacebookUtils.Merge(cancelUrlQueryStrings, queryStrings);
                        }
                    }

                    redirectUriBuilder = new UriBuilder(cancelUrl)
                    {
                        Query = FacebookUtils.ToJsonQueryString(cancelUrlQueryStrings)
                    };
                }
            }

            return(redirectUriBuilder.Uri);
        }