Exemplo n.º 1
0
        /// <summary>
        /// Set the last validity timestamp (VT) received from the server and add to
        ///   preferences. You must manually call PreferenceObfuscator.commit() to
        ///   commit these changes to disk.
        /// </summary>
        /// <param name="timestamp">
        /// the VT string received
        /// </param>
        private void SetValidityTimestamp(string timestamp)
        {
            long t;

            if (!long.TryParse(timestamp, out t))
            {
                // No response or not parsable, expire in one minute.
                System.Diagnostics.Debug.WriteLine("License validity timestamp (VT) missing, caching for a minute");
                t = PolicyExtensions.GetCurrentMilliseconds() + PolicyExtensions.MillisPerMinute;
            }

            this.ValidityTimestamp = t;
        }
        /// <summary>
        /// Process a new response from the license server.
        /// We call this to guarantee that we fetch a fresh policy from the
        /// server. This is to be used if the URL is invalid.
        /// This data will be used for computing future policy decisions.
        /// The following parameters are processed:
        /// <ul>
        /// <li>VT: the timestamp that the client should consider the response valid until</li>
        /// <li>GT: the timestamp that the client should ignore retry errors until</li>
        /// <li>GR: the number of retry errors that the client should ignore</li>
        /// </ul>
        /// </summary>
        /// <param name="response">
        /// the result from validating the server response
        /// </param>
        /// <param name="rawData">
        /// the raw server response data
        /// </param>
        public void ProcessServerResponse(PolicyServerResponse response, ResponseData rawData)
        {
            // Update retry counter
            if (response == PolicyServerResponse.Retry)
            {
                this.RetryCount = this.RetryCount + 1;
            }
            else
            {
                this.RetryCount = 0;
            }

            if (response == PolicyServerResponse.Licensed)
            {
                // Update server policy data
                var extras = new Dictionary <string, string>();
                try
                {
                    extras = PolicyExtensions.DecodeExtras(rawData.Extra);
                }
                catch (Exception)
                {
                    Debug.WriteLine("Invalid syntax error while decoding extras data from server.");
                }

                // If no response or not parseable, expire in one minute.
                this.ValidityTimestamp = PolicyExtensions.GetCurrentMilliseconds() + PolicyExtensions.MillisPerMinute;

                foreach (var pair in extras)
                {
                    this.ProcessResponseExtra(pair);
                }
            }
            else if (response == PolicyServerResponse.NotLicensed)
            {
                // Clear out stale policy data
                this.ValidityTimestamp = 0;
                this.RetryUntil        = 0;
                this.MaxRetries        = 0;
            }

            this.LastResponse = response;
            this.obfuscator.Commit();
        }
Exemplo n.º 3
0
        /// <summary>
        /// This implementation allows access if either:
        ///   <ol>
        ///     <li>a LICENSED response was received within the validity period</li>
        ///     <li>
        ///       a RETRY response was received in the last minute, and we are under
        ///       the RETRY count or in the RETRY period.
        ///     </li>
        ///   </ol>
        /// </summary>
        /// <returns>
        /// The allow access.
        /// </returns>
        public virtual bool AllowAccess()
        {
            bool allowed = false;

            long ts = PolicyExtensions.GetCurrentMilliseconds();

            if (this.LastResponse == PolicyServerResponse.Licensed)
            {
                // Check if the LICENSED response occurred within the validity timeout and is still valid.
                allowed = ts <= this.ValidityTimestamp;
            }
            else if (this.LastResponse == PolicyServerResponse.Retry &&
                     ts < this.LastResponseTime + PolicyExtensions.MillisPerMinute)
            {
                // Only allow access if we are within the retry period or we haven't used up our max retries.
                allowed = ts <= this.RetryUntil || this.RetryCount <= this.MaxRetries;
            }

            return(allowed);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Process a new response from the license server.
        ///   This data will be used for computing future policy decisions. The
        ///   following parameters are processed:
        ///   <ul>
        ///     <li>VT: the timestamp that the client should consider the response valid until</li>
        ///     <li>GT: the timestamp that the client should ignore retry errors until</li>
        ///     <li>GR: the number of retry errors that the client should ignore</li>
        ///   </ul>
        /// </summary>
        /// <param name="response">
        /// the result from validating the server response
        /// </param>
        /// <param name="rawData">
        /// the raw server response data
        /// </param>
        public virtual void ProcessServerResponse(PolicyServerResponse response, ResponseData rawData)
        {
            // Update retry counter
            this.RetryCount = response == PolicyServerResponse.Retry ? this.RetryCount + 1 : 0;

            switch (response)
            {
            case PolicyServerResponse.Licensed:

                // Update server policy data
                Dictionary <string, string> extras;
                if (!PolicyExtensions.TryDecodeExtras(rawData.Extra, out extras))
                {
                    Debug.WriteLine("Invalid syntax error while decoding extras data from server.");
                }

                // If no response or not parseable, expire in one minute.
                this.ValidityTimestamp = PolicyExtensions.GetCurrentMilliseconds() + PolicyExtensions.MillisPerMinute;

                foreach (var pair in extras)
                {
                    this.ProcessResponseExtra(pair);
                }

                break;

            case PolicyServerResponse.NotLicensed:
                this.ValidityTimestamp = Preferences.DefaultValidityTimestamp;
                this.RetryUntil        = Preferences.DefaultRetryUntil;
                this.MaxRetries        = Preferences.DefaultMaxRetries;
                break;
            }

            this.LastResponse = response;

            this.Obfuscator.Commit();
        }
        /// <summary>
        /// This implementation allows access if either:
        /// <ol>
        /// <li>a LICENSED response was received within the validity period</li>
        /// <li>
        /// a RETRY response was received in the last minute, and we
        /// are under the RETRY count or in the RETRY period.
        /// </li>
        /// </ol>
        /// </summary>
        /// <returns>
        /// True if access is allowed, otherwise false.
        /// </returns>
        public bool AllowAccess()
        {
            long ts = PolicyExtensions.GetCurrentMilliseconds();

            if (this.LastResponse == PolicyServerResponse.Licensed)
            {
                // Check if the LICENSED response occurred within the validity
                // timeout.
                if (ts <= this.ValidityTimestamp)
                {
                    // Cached LICENSED response is still valid.
                    return(true);
                }
            }
            else if (this.LastResponse == PolicyServerResponse.Retry &&
                     ts < this.LastResponseTime + PolicyExtensions.MillisPerMinute)
            {
                // Only allow access if we are within the retry period or we
                // haven't used up our max retries.
                return(ts <= this.RetryUntil || this.RetryCount <= this.MaxRetries);
            }

            return(false);
        }