Utilities to convert dates to and from unix time.
Exemplo n.º 1
0
        /// <summary>
        /// Creates a Facebook session from a signed request.
        /// </summary>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// The Facebook session.
        /// </returns>
        internal static FacebookSession Create(IFacebookApplication settings, FacebookSignedRequest signedRequest)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (signedRequest == null)
            {
                return(null);
            }

            var data = (IDictionary <string, object>)signedRequest.Data;

            if (data == null)
            {
                return(null);
            }

            if (!data.ContainsKey("code") && string.IsNullOrEmpty(signedRequest.AccessToken))
            {
                return(null);
            }

            var dictionary = new JsonObject
            {
                { "uid", signedRequest.UserId.ToString() }
            };

            if (!string.IsNullOrEmpty(signedRequest.AccessToken))
            {
                dictionary["access_token"] = signedRequest.AccessToken;
            }

            if (data.ContainsKey("code"))
            {
                foreach (var key in data.Keys)
                {
                    dictionary[key] = data[key];
                }
            }
            else
            {
                if (signedRequest.Expires == DateTime.MaxValue)
                {
                    dictionary["expires"] = 0;
                }
                else if (signedRequest.Expires != DateTime.MinValue)
                {
                    dictionary["expires"] = DateTimeConvertor.ToUnixTime(signedRequest.Expires);
                }

                if (settings != null && !string.IsNullOrEmpty(settings.AppSecret))
                {
                    dictionary["sig"] = GenerateSessionSignature(settings.AppSecret, dictionary);
                }
            }

            return(new FacebookSession(dictionary, settings));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a facebook session from a signed request.
        /// </summary>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// The facebook session.
        /// </returns>
        internal static FacebookSession Create(string appSecret, FacebookSignedRequest signedRequest)
        {
            if (signedRequest == null || String.IsNullOrEmpty(signedRequest.AccessToken))
            {
                return(null);
            }

            var dictionary = new JsonObject
            {
                { "uid", signedRequest.UserId.ToString() },
                { "access_token", signedRequest.AccessToken }
            };

            if (signedRequest.Expires == DateTime.MaxValue)
            {
                dictionary["expires"] = 0;
            }
            else if (signedRequest.Expires != DateTime.MinValue)
            {
                dictionary["expires"] = DateTimeConvertor.ToUnixTime(signedRequest.Expires);
            }

            dictionary["sig"] = GenerateSessionSignature(appSecret, dictionary);

            return(new FacebookSession(dictionary));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parse the signed request.
        /// </summary>
        /// <param name="secret">
        /// The secret.
        /// </param>
        /// <param name="signedRequestValue">
        /// The signed request value.
        /// </param>
        /// <returns>
        /// Returns the signed request.
        /// </returns>
        public static FacebookSignedRequest Parse(string secret, string signedRequestValue)
        {
            Contract.Requires(!string.IsNullOrEmpty(secret));
            Contract.Requires(!String.IsNullOrEmpty(signedRequestValue));
            Contract.Requires(signedRequestValue.Contains("."), Properties.Resources.InvalidSignedRequest);

            var result = TryParse(secret, signedRequestValue, 0, DateTimeConvertor.ToUnixTime(DateTime.UtcNow), true);

            return(result == null ? null : new FacebookSignedRequest(result));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Try parsing the signed request.
        /// </summary>
        /// <param name="secret">
        /// The secret.
        /// </param>
        /// <param name="signedRequestValue">
        /// The signed request value.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// Returns true if parsing was successful otherwise false.
        /// </returns>
        public static bool TryParse(string secret, string signedRequestValue, out FacebookSignedRequest signedRequest)
        {
            if (string.IsNullOrEmpty(signedRequestValue))
            {
                throw new ArgumentNullException("signedRequestValue");
            }
            if (string.IsNullOrEmpty(secret))
            {
                throw new ArgumentNullException("secret");
            }

            var result = TryParse(secret, signedRequestValue, 0, DateTimeConvertor.ToUnixTime(DateTime.UtcNow), false);

            signedRequest = result == null ? null : new FacebookSignedRequest(result);
            return(result != null);
        }
Exemplo n.º 5
0
        public FacebookSession(IDictionary <string, object> dictionary, IFacebookApplication settings)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            _settings = settings;
            var data = dictionary is JsonObject ? dictionary : FacebookUtils.ToDictionary(dictionary);

            AccessToken = data.ContainsKey("access_token") ? (string)data["access_token"] : null;

            if (!data.ContainsKey("uid") && !string.IsNullOrEmpty(AccessToken))
            {
                data.Add("uid", ParseUserIdFromAccessToken(AccessToken));
            }

            string sUserId = data.ContainsKey("uid") && data["uid"] != null ? data["uid"].ToString() : null;
            long   userId  = 0;

            long.TryParse(sUserId, out userId);
            UserId = userId;

            Secret     = data.ContainsKey("secret") ? (string)data["secret"] : null;
            SessionKey = data.ContainsKey("session_key") ? (string)data["session_key"] : null;

            if (data.ContainsKey("expires"))
            {
                Expires = data["expires"].ToString() == "0" ? DateTime.MaxValue : DateTimeConvertor.FromUnixTime(Convert.ToDouble(data["expires"]));
            }
            else
            {
                Expires = DateTime.MinValue;
            }

            Signature  = data.ContainsKey("sig") ? (string)data["sig"] : null;
            BaseDomain = data.ContainsKey("base_domain") ? (string)data["base_domain"] : null;

            _data = data;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a facebook session from a signed request.
        /// </summary>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// The facebook session.
        /// </returns>
        internal static FacebookSession Create(string appSecret, string code)
        {
            FacebookOAuthClient client = new FacebookOAuthClient(FacebookApplication.Current);

            client.RedirectUri = new Uri(FacebookApplication.Current.CanvasUrl);
            dynamic response = client.ExchangeCodeForAccessToken(code);

            string accessToken = response.access_token;
            double expires     = Convert.ToDouble(response.expires);

            var dictionary = new JsonObject
            {
                { "access_token", accessToken }
            };

            dictionary["expires"] = DateTimeConvertor.ToUnixTime(DateTime.Now.AddSeconds(expires));
            dictionary["sig"]     = GenerateSessionSignature(appSecret, dictionary);

            return(new FacebookSession(dictionary));
        }
Exemplo n.º 7
0
        private static string BuildHttpQuery(object parameter, Func <string, string> encode)
        {
            if (parameter == null)
            {
                return("null");
            }
            if (parameter is string)
            {
                return((string)parameter);
            }
            if (parameter is bool)
            {
                return((bool)parameter ? "true" : "false");
            }

            if (parameter is int || parameter is long ||
                parameter is float || parameter is double || parameter is decimal ||
                parameter is byte || parameter is sbyte ||
                parameter is short || parameter is ushort ||
                parameter is uint || parameter is ulong)
            {
                return(parameter.ToString());
            }

            if (parameter is Uri)
            {
                return(parameter.ToString());
            }

            // todo: IEnumerable<KeyValuePair<T1,T2>> and IEnumerable<Tuple<T1, T2>>

            var sb = new StringBuilder();

            if (parameter is IEnumerable <KeyValuePair <string, object> > )
            {
                foreach (var kvp in (IEnumerable <KeyValuePair <string, object> >)parameter)
                {
                    sb.AppendFormat("{0}={1}&", encode(kvp.Key), encode(BuildHttpQuery(kvp.Value, encode)));
                }
            }
            else if (parameter is IEnumerable <KeyValuePair <string, string> > )
            {
                foreach (var kvp in (IEnumerable <KeyValuePair <string, string> >)parameter)
                {
                    sb.AppendFormat("{0}={1}&", encode(kvp.Key), encode(kvp.Value));
                }
            }
            else if (parameter is IEnumerable)
            {
                foreach (var obj in (IEnumerable)parameter)
                {
                    sb.AppendFormat("{0},", encode(BuildHttpQuery(obj, encode)));
                }
            }
            else if (parameter is DateTime)
            {
                return(DateTimeConvertor.ToIso8601FormattedDateTime((DateTime)parameter));
            }
            else
            {
                IDictionary <string, FacebookMediaObject> mediaObjects;
                IDictionary <string, FacebookMediaStream> mediaStreams;
                var dict = ToDictionary(parameter, out mediaObjects, out mediaStreams);
                if (mediaObjects.Count > 0 || mediaStreams.Count > 0)
                {
                    throw new InvalidOperationException("Parameter can contain attachements (FacebookMediaObject/FacebookMediaStream) only in the top most level.");
                }
                return(BuildHttpQuery(dict, encode));
            }

            if (sb.Length > 0)
            {
                sb.Length--;
            }

            return(sb.ToString());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Parse the signed request.
        /// </summary>
        /// <param name="secret">
        /// The secret.
        /// </param>
        /// <param name="signedRequestValue">
        /// The signed request value.
        /// </param>
        /// <returns>
        /// Returns the signed request.
        /// </returns>
        public static FacebookSignedRequest Parse(string secret, string signedRequestValue)
        {
            var result = TryParse(secret, signedRequestValue, 0, DateTimeConvertor.ToUnixTime(DateTime.UtcNow), true);

            return(result == null ? null : new FacebookSignedRequest(result));
        }