<commit_before>﻿// --------------------------------
// <copyright file="FacebookSignedRequest.cs" company="Facebook C# SDK">
//     Microsoft Public License (Ms-PL)
// </copyright>
// <author><NAME> (ntotten.com) and <NAME> (jim<EMAIL>.<EMAIL>)</author>
// <license>Released under the terms of the Microsoft Public License (Ms-PL)</license>
// <website>http://facebooksdk.codeplex.com</website>
// ---------------------------------

namespace Facebook
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Rerpesents a Facebook signed request.
    /// </summary>
    public sealed class FacebookSignedRequest
    {

        public FacebookSignedRequest(IDictionary<string, object> value)
        {
            // common
            this.Algorithm = value.ContainsKey("algorithm") ? (string)value["algorithm"] : null;
            this.IssuedAt = value.ContainsKey("issued_at") ? FacebookUtils.FromUnixTime(Convert.ToInt64(value["issued_at"])) : DateTime.MinValue;

            if (value.ContainsKey("payload"))
            {
                // new signed_request: http://developers.facebook.com/docs/authentication/canvas/encryption_proposal
                var payload = (IDictionary<string, object>)value["payload"];

                this.AccessToken = payload.ContainsKey("access_token") ? (string)payload["access_token"] : null;
                this.Expires = payload.ContainsKey("expires_in")
                                   ? FacebookUtils.FromUnixTime(Convert.ToInt64(payload["expires_in"]))
                                   : DateTime.MinValue;

                this.UserId = payload.ContainsKey("user_id") ? (string)payload["user_id"] : null;
            }
            else
            {
                // old signed_request: http://developers.facebook.com/docs/authentication/canvas
                this.UserId = value.ContainsKey("user_id") ? (string)value["user_id"] : null;
                this.AccessToken = value.ContainsKey("oauth_token") ? (string)value["oauth_token"] : null;
                this.Expires = value.ContainsKey("expires")
                                   ? FacebookUtils.FromUnixTime(Convert.ToInt64(value["expires"]))
                                   : DateTime.MinValue;
                this.ProfileId = value.ContainsKey("profile_id") ? (string)value["profile_id"] : null;

                if (value.ContainsKey("user"))
                {
                    var user = (IDictionary<string, object>)value["user"];
                    this.User = new FacebookSignedRequestUser
                                    {
                                        Country = user.ContainsKey("country") ? (string)user["country"] : null,
                                        Locale = user.ContainsKey("locale") ? (string)user["locale"] : null,
                                    };
                }
            }
        }

        /// <summary>
        /// Gets or sets the user id.
        /// </summary>
        /// <value>The user id.</value>
        public string UserId { get; set; }

        /// <summary>
        /// Gets or sets the access token.
        /// </summary>
        /// <value>The access token.</value>
        public string AccessToken { get; set; }

        /// <summary>
        /// Gets or sets the expires.
        /// </summary>
        /// <value>The expires.</value>
        public DateTime Expires { get; set; }

        /// <summary>
        /// Gets or sets the expires.
        /// </summary>
        /// <value>The expires.</value>
        public DateTime IssuedAt { get; set; }

        /// <summary>
        /// Gets or sets the profile id.
        /// </summary>
        /// <value>The profile id.</value>
        public string ProfileId { get; set; }

        /// <summary>
        /// Gets or sets the algorithm.
        /// </summary>
        /// <value>The algorithm.</value>
        public string Algorithm { get; set; }

        public FacebookSignedRequestUser User { get; set; }

        /// <summary>
        /// Parse the signed request string.
        /// </summary>
        /// <param name="signedRequestValue">
        /// The encoded signed request value.
        /// </param>
        /// <param name="secret">
        /// The application secret.
        /// </param>
        /// <param name="maxAge">
        /// The max age.
        /// </param>
        /// <param name="currentTime">
        /// The current time (in unix time format).
        /// </param>
        /// <returns>
        /// The valid signed request.
        /// </returns>
        /// <remarks>
        /// Supports both http://developers.facebook.com/docs/authentication/canvas/encryption_proposal
        /// and http://developers.facebook.com/docs/authentication/canvas
        /// </remarks>
        public static FacebookSignedRequest Parse(string secret, string signedRequestValue, int maxAge, double currentTime)
        {
            Contract.Requires(!String.IsNullOrEmpty(signedRequestValue));
            Contract.Requires(!String.IsNullOrEmpty(secret));
            Contract.Requires(maxAge >= 0);
            Contract.Requires(currentTime >= 0);
            Contract.Requires(signedRequestValue.Contains("."), Properties.Resources.InvalidSignedRequest);

            // NOTE: currentTime added to parameters to make it unit testable.

            string[] split = signedRequestValue.Split('.');
            if (split.Length != 2)
            {
                // need to have exactly 2 parts
                throw new InvalidOperationException(Properties.Resources.InvalidSignedRequest);
            }

            string encodedSignature = split[0];
            string encodedEnvelope = split[1];

            if (string.IsNullOrEmpty(encodedSignature))
            {
                throw new InvalidOperationException(Properties.Resources.InvalidSignedRequest);
            }
<commit_msg>+ FacebookSignedRequest.Parse method taking currentTime marked as internal from public<commit_after>﻿// --------------------------------
// <copyright file="FacebookSignedRequest.cs" company="Facebook C# SDK">
//     Microsoft Public License (Ms-PL)
// </copyright>
// <author><NAME> (ntotten.com) and <NAME> (j<EMAIL>)</author>
// <license>Released under the terms of the Microsoft Public License (Ms-PL)</license>
// <website>http://facebooksdk.codeplex.com</website>
// ---------------------------------

namespace Facebook
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Rerpesents a Facebook signed request.
    /// </summary>
    public sealed class FacebookSignedRequest
    {

        public FacebookSignedRequest(IDictionary<string, object> value)
        {
            // common
            this.Algorithm = value.ContainsKey("algorithm") ? (string)value["algorithm"] : null;
            this.IssuedAt = value.ContainsKey("issued_at") ? FacebookUtils.FromUnixTime(Convert.ToInt64(value["issued_at"])) : DateTime.MinValue;

            if (value.ContainsKey("payload"))
            {
                // new signed_request: http://developers.facebook.com/docs/authentication/canvas/encryption_proposal
                var payload = (IDictionary<string, object>)value["payload"];

                this.AccessToken = payload.ContainsKey("access_token") ? (string)payload["access_token"] : null;
                this.Expires = payload.ContainsKey("expires_in")
                                   ? FacebookUtils.FromUnixTime(Convert.ToInt64(payload["expires_in"]))
                                   : DateTime.MinValue;

                this.UserId = payload.ContainsKey("user_id") ? (string)payload["user_id"] : null;
            }
            else
            {
                // old signed_request: http://developers.facebook.com/docs/authentication/canvas
                this.UserId = value.ContainsKey("user_id") ? (string)value["user_id"] : null;
                this.AccessToken = value.ContainsKey("oauth_token") ? (string)value["oauth_token"] : null;
                this.Expires = value.ContainsKey("expires")
                                   ? FacebookUtils.FromUnixTime(Convert.ToInt64(value["expires"]))
                                   : DateTime.MinValue;
                this.ProfileId = value.ContainsKey("profile_id") ? (string)value["profile_id"] : null;

                if (value.ContainsKey("user"))
                {
                    var user = (IDictionary<string, object>)value["user"];
                    this.User = new FacebookSignedRequestUser
                                    {
                                        Country = user.ContainsKey("country") ? (string)user["country"] : null,
                                        Locale = user.ContainsKey("locale") ? (string)user["locale"] : null,
                                    };
                }
            }
        }

        /// <summary>
        /// Gets or sets the user id.
        /// </summary>
        /// <value>The user id.</value>
        public string UserId { get; set; }

        /// <summary>
        /// Gets or sets the access token.
        /// </summary>
        /// <value>The access token.</value>
        public string AccessToken { get; set; }

        /// <summary>
        /// Gets or sets the expires.
        /// </summary>
        /// <value>The expires.</value>
        public DateTime Expires { get; set; }

        /// <summary>
        /// Gets or sets the expires.
        /// </summary>
        /// <value>The expires.</value>
        public DateTime IssuedAt { get; set; }

        /// <summary>
        /// Gets or sets the profile id.
        /// </summary>
        /// <value>The profile id.</value>
        public string ProfileId { get; set; }

        /// <summary>
        /// Gets or sets the algorithm.
        /// </summary>
        /// <value>The algorithm.</value>
        public string Algorithm { get; set; }

        public FacebookSignedRequestUser User { get; set; }

        /// <summary>
        /// Parse the signed request string.
        /// </summary>
        /// <param name="signedRequestValue">
        /// The encoded signed request value.
        /// </param>
        /// <param name="secret">
        /// The application secret.
        /// </param>
        /// <param name="maxAge">
        /// The max age.
        /// </param>
        /// <param name="currentTime">
        /// The current time (in unix time format).
        /// </param>
        /// <returns>
        /// The valid signed request.
        /// </returns>
        /// <remarks>
        /// Supports both http://developers.facebook.com/docs/authentication/canvas/encryption_proposal
        /// and http://developers.facebook.com/docs/authentication/canvas
        /// </remarks>
        internal static FacebookSignedRequest Parse(string secret, string signedRequestValue, int maxAge, double currentTime)
        {
            Contract.Requires(!String.IsNullOrEmpty(signedRequestValue));
            Contract.Requires(!String.IsNullOrEmpty(secret));
            Contract.Requires(maxAge >= 0);
            Contract.Requires(currentTime >= 0);
            Contract.Requires(signedRequestValue.Contains("."), Properties.Resources.InvalidSignedRequest);

            // NOTE: currentTime added to parameters to make it unit testable.

            string[] split = signedRequestValue.Split('.');
            if (split.Length != 2)
            {
                // need to have exactly 2 parts
                throw new InvalidOperationException(Properties.Resources.InvalidSignedRequest);
            }

            string encodedSignature = split[0];
            string encodedEnvelope = split[1];

            if (string.IsNullOrEmpty(encodedSignature))
            {
                throw new InvalidOperationException(Properties.Resources.InvalidSignedRequest);
            }

{'fraction_non_alphanumeric': 0.13948265823837644, 'fraction_numerical': 0.001996173999833652, 'mean_word_length': 1.6803388319215338, 'pattern_counts': {'":': 0, '<': 112, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 41, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6257681', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2944, 'n_words': 651}