コード例 #1
0
                public void Good_Url(string url)
                {
                    var data = new ApiKeyData
                    {
                        Ip         = "",
                        UrlPattern = url,
                        AppStatus  = ApiKey.ApplicationStatus.Production
                    };

                    var command = new ValidateAndGetKeyTypeCommand(data);
                    var type    = CommandExecutor.ExecuteCommand(command);

                    Assert.That(type, Is.EqualTo(ApiKey.ApplicationType.Browser));
                    Assert.That(command.ErrorMessage, Is.Null);
                }
コード例 #2
0
        public void Empty_Parameters_Returns_None_with_Message()
        {
            var data = new ApiKeyData
            {
                Ip         = "",
                UrlPattern = "",
                AppStatus  = ApiKey.ApplicationStatus.Production
            };

            var command = new ValidateAndGetKeyTypeCommand(data);
            var type    = CommandExecutor.ExecuteCommand(command);

            Assert.That(type, Is.EqualTo(ApiKey.ApplicationType.None));
            Assert.That(command.ErrorMessage, Is.Not.Null);
        }
コード例 #3
0
                public void Good_Url(string url)
                {
                    var data = new ApiKeyData
                        {
                            Ip = "",
                            UrlPattern = url,
                            AppStatus = ApiKey.ApplicationStatus.Production
                        };

                    var command = new ValidateAndGetKeyTypeCommand(data);
                    var type = CommandExecutor.ExecuteCommand(command);

                    Assert.That(type, Is.EqualTo(ApiKey.ApplicationType.Browser));
                    Assert.That(command.ErrorMessage, Is.Null);
                }
コード例 #4
0
        public void Empty_Parameters_Returns_None_with_Message()
        {
            var data = new ApiKeyData
                {
                    Ip = "",
                    UrlPattern = "",
                    AppStatus = ApiKey.ApplicationStatus.Production
                };

            var command = new ValidateAndGetKeyTypeCommand(data);
            var type = CommandExecutor.ExecuteCommand(command);

            Assert.That(type, Is.EqualTo(ApiKey.ApplicationType.None));
            Assert.That(command.ErrorMessage, Is.Not.Null);
        }
コード例 #5
0
        public ActionResult Generate(ApiKeyData data)
        {
            var account = Account;

            if (account.KeyQuota.KeysAllowed - account.KeyQuota.KeysUsed <= 0)
            {
                ErrorMessage =
                    "You have reached the API key quota. Please delete or deactivate keys that aren't in use or contact UGRC to increase your quota.";

                return(RedirectToAction("Index", "KeyManagement"));
            }

            ApiKey.ApplicationType type;
            var command = new ValidateAndGetKeyTypeCommand(data);

            try
            {
                type = CommandExecutor.ExecuteCommand(command);
            }
            catch (CommandValidationException e)
            {
                ErrorMessage = e.Message;

                return(RedirectToAction("Index", "KeyManagement"));
            }

            if (type == ApiKey.ApplicationType.None)
            {
                ErrorMessage = command.ErrorMessage;

                if (!string.IsNullOrEmpty(data.Ip))
                {
                    TempData["ip"] = data.Ip;
                }
                else
                {
                    TempData["url"] = data.UrlPattern;
                }

                return(RedirectToAction("Index", "GenerateKey"));
            }

            if (type == ApiKey.ApplicationType.Server)
            {
                if (LocalIp.IsMatch(data.Ip))
                {
                    Message = "The key you created looks like an internal IP address and will most likely not authenticate. " +
                              "Please visit <a href='http://whatismyip.com' target='_blank'>whatismyip.com</a> and use your public " +
                              "IP address. If you receive a 400 status code, be sure to read the response body as it will detail " +
                              "the reasons. ";
                }
            }

            var key     = CommandExecutor.ExecuteCommand(new GenerateUniqueApiKeyCommand(Session));
            var pattern = CommandExecutor.ExecuteCommand(new FormatKeyPatternCommand(type, data));
            var apiKey  = new ApiKey(key)
            {
                AccountId      = account.Id,
                ApiKeyStatus   = ApiKey.KeyStatus.Active,
                Type           = type,
                AppStatus      = data.AppStatus,
                Pattern        = data.UrlPattern ?? data.Ip,
                RegexPattern   = pattern,
                CreatedAtTicks = DateTime.UtcNow.Ticks,
                Deleted        = false,
                Key            = key
            };

            Session.Store(apiKey);

            Session.SaveChanges();

            Account.KeyQuota.KeysUsed = CommandExecutor.ExecuteCommand(new CountApiInfosForUserQuery(Session, account));

            Task.Factory.StartNew(() => CommandExecutor.ExecuteCommand(new KeyCreatedEmailCommand(account, apiKey)));

            Message += "Key created successfully.";

            return(RedirectToAction("Index", "KeyManagement"));
        }