private string GetKeyFromDialog(LicenseDialogMessageType messageType)
        {
            _logger.LogDebug("Displaying license dialog...");

            var result = _licenseDialog.Display(messageType);

            _logger.LogDebug("License dialog has returned result.");

            _logger.LogTrace("result.Key = " + result.Key);
            _logger.LogTrace(string.Format("result.Type = {0}", result.Type));

            switch (result.Type)
            {
            case LicenseDialogResultType.Confirmed:
                _logger.LogDebug("Using key typed in license dialog.");
                return(result.Key);

            case LicenseDialogResultType.Aborted:
                _logger.LogDebug("License dialog has been aborted. Cancelling operation.");
                throw new OperationCanceledException();

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private bool TryToHandleApiErrors(int statusCode, ref LicenseDialogMessageType messageType,
                                          bool isUsingCachedKey)
        {
            _logger.LogTrace(string.Format("isUsingCachedKey = {0}", isUsingCachedKey));
            _logger.LogTrace(string.Format("statusCode = {0}", statusCode));

            if (statusCode == 404)
            {
                _logger.LogDebug("License key is not found.");
                HandleApiError(ref messageType, isUsingCachedKey, LicenseDialogMessageType.InvalidLicense);
                return(true);
            }
            if (statusCode == 410)
            {
                _logger.LogDebug("License key is blocked.");
                HandleApiError(ref messageType, isUsingCachedKey, LicenseDialogMessageType.BlockedLicense);
                return(true);
            }
            if (statusCode == 403)
            {
                _logger.LogDebug(
                    "License key validation service is not available.");
                HandleApiError(ref messageType, isUsingCachedKey, LicenseDialogMessageType.ServiceUnavailable);
                return(true);
            }

            _logger.LogError("Unrecognized server or API error.");
            return(false);
        }
Beispiel #3
0
        public LicenseDialogResult Display(LicenseDialogMessageType messageType)
        {
            UnityDispatcher.Invoke(() => UpdateMessage(messageType));

            base.Display(CancellationToken.Empty);

            return(_result);
        }
 private void HandleApiError(ref LicenseDialogMessageType messageType, bool isUsingCachedKey,
                             LicenseDialogMessageType licenseDialogMessageType)
 {
     if (!isUsingCachedKey)
     {
         _logger.LogDebug(string.Format("Setting license dialog message to {0}", licenseDialogMessageType));
         messageType = licenseDialogMessageType;
     }
     else
     {
         _logger.LogDebug(
             "Ignoring API error - the attempt was done with cached key. Prompting user to enter new license key.");
     }
 }
Beispiel #5
0
    public void Execute_DisplaysDialogMessageForApiError(int statusCode, LicenseDialogMessageType messageType)
    {
        const string key       = "key";
        const string keySecret = "key-secret";

        var licenseDialog = Substitute.For <ILicenseDialog>();

        licenseDialog.Display(Arg.Any <LicenseDialogMessageType>()).ReturnsForAnyArgs(new LicenseDialogResult()
        {
            Key  = key,
            Type = LicenseDialogResultType.Confirmed
        });

        var remoteMetaData = Substitute.For <IRemoteMetaData>();

        remoteMetaData.GetAppInfo().Returns(new App()
        {
            UseKeys = true
        });

        var localMetaData = Substitute.For <ILocalMetaData>();

        bool firstAttempt = true;

        remoteMetaData.GetKeySecret(key, Arg.Any <string>()).Returns(info =>
        {
            if (!firstAttempt)
            {
                return(keySecret);
            }

            firstAttempt = false;
            throw new ApiResponseException(statusCode);
        });

        var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, localMetaData, _cache, _logger, _issueReporter);

        command.Prepare(_statusMonitor);
        command.Execute(CancellationToken.Empty);

        licenseDialog.Received(1).Display(LicenseDialogMessageType.None);
        licenseDialog.Received(1).Display(messageType);
        licenseDialog.DidNotReceive().Display(Arg.Is <LicenseDialogMessageType>(type => type != LicenseDialogMessageType.None &&
                                                                                type != messageType));
    }
        private string GetKey(LicenseDialogMessageType messageType, string cachedKey, out bool isUsingCachedKey,
                              ref bool didUseCachedKey)
        {
            bool isCachedKeyAvailable = !string.IsNullOrEmpty(cachedKey);

            if (isCachedKeyAvailable && !didUseCachedKey)
            {
                _licenseDialog.SetKey(cachedKey);
                didUseCachedKey  = true;
                isUsingCachedKey = true;

                return(cachedKey);
            }

            isUsingCachedKey = false;

            return(GetKeyFromDialog(messageType));
        }
Beispiel #7
0
        private void UpdateMessage(LicenseDialogMessageType messageType)
        {
            switch (messageType)
            {
            case LicenseDialogMessageType.None:
                ErrorMessageText.text = string.Empty;
                break;

            case LicenseDialogMessageType.InvalidLicense:
                ErrorMessageText.text = InvalidLicenseMessageText;
                break;

            case LicenseDialogMessageType.BlockedLicense:
                ErrorMessageText.text = BlockedLicenseMessageText;
                break;

            case LicenseDialogMessageType.ServiceUnavailable:
                ErrorMessageText.text = ServiceUnavailableMessageText;
                break;

            default:
                throw new ArgumentOutOfRangeException("messageType", messageType, null);
            }
        }
Beispiel #8
0
        public override void Execute(CancellationToken cancellationToken)
        {
            base.Execute(cancellationToken);

            DebugLogger.Log("Validating license.");

            KeySecret = null;

            var appInfo = _remoteMetaData.GetAppInfo();

            if (!appInfo.UseKeys)
            {
                DebugLogger.Log("Application is not using license keys.");
                return;
            }

            LicenseDialogMessageType messageType = LicenseDialogMessageType.None;

            var  cachedKey      = GetCachedKey();
            bool triedCachedKey = false;

            while (KeySecret == null)
            {
                string key = string.Empty;

                if (!triedCachedKey && !string.IsNullOrEmpty(cachedKey))
                {
                    DebugLogger.Log("Using cached license key.");

                    key            = cachedKey;
                    triedCachedKey = true;
                }
                else
                {
                    DebugLogger.Log("Displaying license dialog.");

                    var result = _licenseDialog.Display(messageType);

                    DebugLogger.Log("Processing dialog result.");

                    if (result.Type == LicenseDialogResultType.Confirmed)
                    {
                        DebugLogger.Log("Using license key typed in dialog.");
                        key = result.Key;
                    }
                    else if (result.Type == LicenseDialogResultType.Aborted)
                    {
                        DebugLogger.Log("License dialog has been aborted. Cancelling operation.");
                        throw new OperationCanceledException();
                    }
                }

                try
                {
                    KeySecret = _remoteMetaData.GetKeySecret(key, GetCachedKeySecret(key));

                    DebugLogger.LogVariable(KeySecret, "KeySecret");
                    DebugLogger.Log("License key has been validated");

                    SetCachedKey(key);
                    SetCachedKeySecret(key, KeySecret);
                }
                catch (ApiResponseException apiResponseException)
                {
                    DebugLogger.LogException(apiResponseException);

                    if (apiResponseException.StatusCode == 404)
                    {
                        KeySecret   = null;
                        messageType = LicenseDialogMessageType.InvalidLicense;
                    }
                    else if (apiResponseException.StatusCode == 410)
                    {
                        KeySecret   = null;
                        messageType = LicenseDialogMessageType.BlockedLicense;
                    }
                    else if (apiResponseException.StatusCode == 403)
                    {
                        KeySecret   = null;
                        messageType = LicenseDialogMessageType.ServiceUnavailable;
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (WebException webException)
                {
                    DebugLogger.LogException(webException);

                    KeySecret   = null;
                    messageType = LicenseDialogMessageType.ServiceUnavailable;

                    if (webException.Status == WebExceptionStatus.ProtocolError)
                    {
                        var response = (HttpWebResponse)webException.Response;
                        if ((int)response.StatusCode == 404)
                        {
                            messageType = LicenseDialogMessageType.InvalidLicense;
                        }
                        else if ((int)response.StatusCode == 410)
                        {
                            messageType = LicenseDialogMessageType.BlockedLicense;
                        }
                        else if ((int)response.StatusCode == 403)
                        {
                            messageType = LicenseDialogMessageType.ServiceUnavailable;
                        }
                    }
                }
            }
        }