예제 #1
0
        internal IDictionary <string, object> PrepareCanvasLoginUrlOAuthState(string returnUrlPath, string cancelUrlPath, string state, IDictionary <string, object> loginParameters)
        {
            Contract.Ensures(Contract.Result <IDictionary <string, object> >() != null);

            var oauthJsonState = new JsonObject();

            // make it one letter character so more info can fit in.
            // r -> return_url_path
            // c -> cancel_url_path
            // s -> user_state

            var mergedParameters = FacebookUtils.Merge(null, loginParameters);

            if (mergedParameters.ContainsKey("state"))
            {
                // override the user state if present in the parameters.
                state = mergedParameters["state"] == null ? null : mergedParameters["state"].ToString();
            }

            if (!string.IsNullOrEmpty(state))
            {
                oauthJsonState["s"] = state;
            }

            if (string.IsNullOrEmpty(returnUrlPath))
            {
                oauthJsonState["r"] = CurrentCanvasPage.ToString();
            }
            else
            {
                if (IsRelativeUri(returnUrlPath))
                {
                    oauthJsonState["r"] = BuildCanvasPageUrl(returnUrlPath).ToString();
                }
                else
                {
                    oauthJsonState["r"] = returnUrlPath;
                }
            }

            if (string.IsNullOrEmpty(cancelUrlPath))
            {
                // if cancel url path is empty, get settings from facebook application.
                cancelUrlPath = _settings.CancelUrlPath;
            }

            if (!string.IsNullOrEmpty(cancelUrlPath))
            {
                if (IsRelativeUri(cancelUrlPath))
                {
                    oauthJsonState["c"] = BuildCanvasPageUrl(cancelUrlPath).ToString();
                }
                else
                {
                    oauthJsonState["c"] = cancelUrlPath;
                }
            }

            return(oauthJsonState);
        }
예제 #2
0
        /// <summary>
        /// Gets the canvas login url
        /// </summary>
        /// <param name="returnUrlPath">
        /// The return Url Path.
        /// </param>
        /// <param name="cancelUrlPath">
        /// The cancel Url Path.
        /// </param>
        /// <param name="state">
        /// The state.
        /// </param>
        /// <param name="loginParameters">
        /// The parameters.
        /// </param>
        /// <returns>
        /// Returns the login url.
        /// </returns>
        public Uri GetLoginUrl(string returnUrlPath, string cancelUrlPath, string state, IDictionary <string, object> loginParameters)
        {
            var oauth = new FacebookOAuthClient
            {
                AppId = _settings.AppId
            };

            var oauthJsonState = PrepareCanvasLoginUrlOAuthState(returnUrlPath, cancelUrlPath, state, loginParameters);

            var oauthState            = FacebookWebUtils.Base64UrlEncode(Encoding.UTF8.GetBytes(oauthJsonState.ToString()));
            var mergedLoginParameters = FacebookUtils.Merge(loginParameters, null);

            mergedLoginParameters["state"] = oauthState;

            var appPath = _httpRequest.ApplicationPath;

            if (appPath != "/")
            {
                appPath = string.Concat(appPath, "/");
            }

            string redirectRoot = RedirectPath;

            var uriBuilder = new UriBuilder(CurrentCanvasUrl)
            {
                Path  = string.Concat(appPath, redirectRoot),
                Query = string.Empty
            };

            oauth.RedirectUri = uriBuilder.Uri;

            var loginUrl = oauth.GetLoginUrl(mergedLoginParameters);

            return(loginUrl);
        }
예제 #3
0
        public void TheCountOfResultShouldBe0()
        {
            IDictionary <string, object> first  = null;
            IDictionary <string, object> second = null;

            var result = FacebookUtils.Merge(first, second);

            Assert.Equal(0, result.Count);
        }
예제 #4
0
        public void TheResultShouldNotBeNull()
        {
            IDictionary <string, object> first  = null;
            IDictionary <string, object> second = null;

            var result = FacebookUtils.Merge(first, second);

            Assert.NotNull(result);
        }
예제 #5
0
        public void ResultShouldNotBeNull()
        {
            var first  = new Dictionary <string, object>();
            var second = new Dictionary <string, object>();

            var result = FacebookUtils.Merge(first, second);

            Assert.NotNull(result);
        }
예제 #6
0
        internal static IDictionary <string, object> AddReturnSslResourceIfRequired(IDictionary <string, object> parameters, bool isSecuredConnection)
        {
            Contract.Ensures(Contract.Result <IDictionary <string, object> >() != null);

            var mergedParameters = FacebookUtils.Merge(null, parameters);

            if (isSecuredConnection && !mergedParameters.ContainsKey(Facebook.Web.Properties.Resources.return_ssl_resources))
            {
                mergedParameters[Facebook.Web.Properties.Resources.return_ssl_resources] = true;
            }

            return(mergedParameters);
        }
        public void ResultShouldNotBeNull()
        {
            var first = new Dictionary <string, object> {
                { "prop1", "value1" }
            };
            var second = new Dictionary <string, object> {
                { "prop2", "value2" }
            };

            var result = FacebookUtils.Merge(first, second);

            Assert.NotNull(result);
        }
        public void TheCountOfResultShouldBeEqualToCountOfFirstAndSecondInputs()
        {
            var first = new Dictionary <string, object> {
                { "prop1", "value1" }
            };
            var second = new Dictionary <string, object> {
                { "prop2", "value2" }
            };

            var result = FacebookUtils.Merge(first, second);

            Assert.Equal(2, result.Count);
        }
        public void TheValuesShouldBeEqualToTheOneInsertedFromFirstOrSecond()
        {
            var first = new Dictionary <string, object> {
                { "prop1", "value1" }
            };
            var second = new Dictionary <string, object> {
                { "prop2", "value2" }
            };

            var result = FacebookUtils.Merge(first, second);

            Assert.Equal(first["prop1"], result["prop1"]);
            Assert.Equal(second["prop2"], result["prop2"]);
        }
        public void TheValuesOfResultShouldBeSameAsValuesOfSecondInput()
        {
            IDictionary <string, object> first  = null;
            IDictionary <string, object> second = new Dictionary <string, object>
            {
                { "prop1", "value1" },
                { "prop2", "value2" }
            };

            var result = FacebookUtils.Merge(first, second);

            Assert.Equal(second["prop1"], result["prop1"]);
            Assert.Equal(second["prop2"], result["prop2"]);
        }
        public void TheCountOfTheResultShouldBeEqualToCountOfSecondInput()
        {
            IDictionary <string, object> first  = null;
            IDictionary <string, object> second = new Dictionary <string, object>
            {
                { "prop1", "value1" },
                { "prop2", "value2" }
            };


            var result = FacebookUtils.Merge(first, second);

            Assert.Equal(second.Count, result.Count);
        }
예제 #12
0
        public void TheValuesOfNonUniqueKeysOfResultShouldBeOverridenBySecond()
        {
            var first = new Dictionary <string, object>
            {
                { "prop1", "value1-first" },
                { "prop2", "value2" }
            };
            var second = new Dictionary <string, object>
            {
                { "prop1", "value1-second" },
                { "prop3", "value3" }
            };

            var result = FacebookUtils.Merge(first, second);

            Assert.Equal(second["prop1"], result["prop1"]);
        }
예제 #13
0
        public void TheCountOfResultShouldBeEqualToNumberOfUniqueKeys()
        {
            var first = new Dictionary <string, object>
            {
                { "prop1", "value1-first" },
                { "prop2", "value2" }
            };
            var second = new Dictionary <string, object>
            {
                { "prop1", "value1-second" },
                { "prop3", "value3" }
            };
            var expected = 3;

            var result = FacebookUtils.Merge(first, second);

            Assert.Equal(expected, result.Count);
        }
예제 #14
0
        /// <summary>
        /// Gets the canvas login url.
        /// </summary>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        /// <returns>
        /// Returns the login url.
        /// </returns>
        public Uri GetLoginUrl(IDictionary <string, object> parameters)
        {
            var defaultParameters = new Dictionary <string, object>();

            if (!string.IsNullOrEmpty(LoginDisplayMode))
            {
                defaultParameters["display"] = LoginDisplayMode;
            }

            if (Permissions != null)
            {
                defaultParameters["scope"] = string.Join(",", Permissions);
            }

            var canvasUrlBuilder = new CanvasUrlBuilder(FacebookWebRequest.Settings, FacebookWebRequest.HttpContext.Request);

            return(canvasUrlBuilder.GetLoginUrl(ReturnUrlPath, CancelUrlPath, State, FacebookUtils.Merge(defaultParameters, parameters)));
        }
        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);
        }