public void ApiKey_ExecutionRequestValidationShouldSucceedIfGoodToken()
        {
            Prepare();
            ServiceProxySystem.Register <ApiKeyRequiredEcho>();

            string methodName = MethodBase.GetCurrentMethod().Name;
            IApplicationNameProvider nameProvider = new TestApplicationNameProvider(methodName);
            IApiKeyProvider          keyProvider  = new LocalApiKeyProvider();

            string           className = "ApiKeyRequiredEcho";
            string           method    = "Send";
            string           data      = ApiParameters.ParametersToJsonParamsArray("some random data").ToJson();
            ExecutionRequest er        = new ExecutionRequest(className, method, "json")
            {
                JsonParams     = data,
                ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider),
                Request        = new ServiceProxyTestHelpers.FormUrlEncodedTestRequest()
            };

            er.ApiKeyResolver.SetKeyToken(er.Request.Headers, ApiParameters.GetStringToHash(className, method, data));

            ValidationResult result = er.Validate();

            Expect.IsTrue(result.Success);
        }
        public void ApiKey_ExecutionRequestValidationShouldFailIfBadToken()
        {
            RegisterDb();
            ServiceProxySystem.Register <ApiKeyRequiredEcho>();

            IUserResolver mockUserResolver = Substitute.For <IUserResolver>();

            mockUserResolver.GetUser(Arg.Any <IHttpContext>()).Returns("testUser");
            LocalApiKeyManager.Default.UserResolver = mockUserResolver;

            string methodName = MethodBase.GetCurrentMethod().Name;
            IApplicationNameProvider nameProvider = new TestApplicationNameProvider(methodName.RandomLetters(4));
            IApiKeyProvider          keyProvider  = new LocalApiKeyProvider();

            ExecutionRequest er = new ExecutionRequest("ApiKeyRequiredEcho", "Send", "json")
            {
                ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider),
                Request        = new ServiceProxyTestHelpers.JsonTestRequest()
            };
            string data = ApiParameters.ParametersToJsonParamsObjectString("some random data");

            er.InputString = data;
            ApiKeyResolver resolver = new ApiKeyResolver(keyProvider, nameProvider);

            resolver.SetKeyToken(er.Request.Headers, data);

            er.Request.Headers[Headers.KeyToken] = "bad token value";

            ValidationResult result = er.Validate();

            Expect.IsFalse(result.Success, "Validation should have failed");
            List <ValidationFailures> failures = new List <ValidationFailures>(result.ValidationFailures);

            Expect.IsTrue(failures.Contains(ValidationFailures.InvalidApiKeyToken), "ValidationFailure should have been InvalidApiKeyToken");
        }
        public void ApiKey_ExecutionRequestShouldValidateApiKey()
        {
            RegisterDb();
            ServiceProxySystem.Register <ApiKeyRequiredEcho>();
            string        testName         = MethodBase.GetCurrentMethod().Name;
            IUserResolver mockUserResolver = Substitute.For <IUserResolver>();

            mockUserResolver.GetUser(Arg.Any <IHttpContext>()).Returns("testUser");
            LocalApiKeyManager.Default.UserResolver = mockUserResolver;

            IApplicationNameProvider nameProvider = new TestApplicationNameProvider(testName.RandomLetters(6));
            IApiKeyProvider          keyProvider  = new LocalApiKeyProvider();

            ExecutionRequest er = new ExecutionRequest("ApiKeyRequiredEcho", "Send", "json")
            {
                ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider),
                Request        = new ServiceProxyTestHelpers.JsonTestRequest()
            };

            string data = ApiParameters.ParametersToJsonParamsObjectString("some random data");

            er.InputString = data;

            ValidationResult result = er.Validate();

            result.Success.IsFalse();
            List <ValidationFailures> failures = new List <ValidationFailures>(result.ValidationFailures);

            failures.Contains(ValidationFailures.InvalidApiKeyToken).IsTrue();
        }
예제 #4
0
        [UnitTest] // RegisterDb is defined in Program.cs
        public void ApiKey_ShouldSetToken()
        {
            ClientTestSetup();
            string testName         = MethodBase.GetCurrentMethod().Name;
            NameValueCollection nvc = new NameValueCollection();
            string data             = "Some random data";

            IApiKeyProvider keyProvider = new LocalApiKeyProvider();
            ApiKeyResolver  resolver    = new ApiKeyResolver(keyProvider, new TestApplicationNameProvider(testName));

            resolver.SetKeyToken(nvc, data);

            Expect.IsNotNullOrEmpty(nvc[Headers.KeyToken], "Key token was not set");
            ClearApps();
        }
예제 #5
0
        [UnitTest] // RegisterDb is defined in Program.cs
        public void ApiKey_ShouldSetValidToken()
        {
            ClientTestSetup();
            string testName                          = MethodBase.GetCurrentMethod().Name;
            NameValueCollection nvc                  = new NameValueCollection();
            string                      data         = "Some random data";
            IApiKeyProvider             keyProvider  = new LocalApiKeyProvider();
            TestApplicationNameProvider nameProvider = new TestApplicationNameProvider(testName);
            ApiKeyResolver              resolver     = new ApiKeyResolver(keyProvider, nameProvider);

            resolver.SetKeyToken(nvc, data);

            string token   = nvc[Headers.KeyToken];
            bool   isValid = resolver.IsValidKeyToken(data, token);

            Expect.IsTrue(isValid, "token was not valid");
            ClearApps();
        }
        public void SecureServiceProxyInvokeWithApiKeyShouldSucceed()
        {
            CleanUp();
            string    methodName = MethodBase.GetCurrentMethod().Name;
            BamServer server;

            SecureChannel.Debug = true;

            string baseAddress;

            ServiceProxyTestHelpers.CreateServer(out baseAddress, out server);
            ServiceProxyTestHelpers.Servers.Add(server); // makes sure it gets stopped after test run
            SecureServiceProxyClient <ApiKeyRequiredEcho> sspc = new SecureServiceProxyClient <ApiKeyRequiredEcho>(baseAddress);

            IApplicationNameProvider nameProvider = new TestApplicationNameProvider(methodName);
            IApiKeyProvider          keyProvider  = new LocalApiKeyProvider();
            ApiKeyResolver           keyResolver  = new ApiKeyResolver(keyProvider, nameProvider);

            SecureChannel channel = new SecureChannel();

            channel.ApiKeyResolver = keyResolver;

            server.AddCommonService <SecureChannel>(channel);
            server.AddCommonService <ApiKeyRequiredEcho>();

            server.Start();

            string value  = "InputValue_".RandomLetters(8);
            bool?  thrown = false;

            sspc.InvocationException += (client, ex) =>
            {
                thrown = true;
            };

            sspc.ApiKeyResolver = keyResolver;
            string result = sspc.Invoke <string>("Send", new object[] { value });

            Expect.IsFalse(thrown.Value, "Exception was thrown");
            Expect.AreEqual(value, result);
            CleanUp();
        }
        public void ApiKey_ExecutionRequestShouldValidateApiKey()
        {
            RegisterDb();
            ServiceProxySystem.Register <ApiKeyRequiredEcho>();
            string testName = MethodBase.GetCurrentMethod().Name;
            IApplicationNameProvider nameProvider = new TestApplicationNameProvider(testName.RandomLetters(6));
            IApiKeyProvider          keyProvider  = new LocalApiKeyProvider();

            ExecutionRequest er = new ExecutionRequest("ApiKeyRequiredEcho", "Send", "json");

            er.ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider);

            er.Request = new ServiceProxyTestHelpers.JsonTestRequest();
            string data = ApiParameters.ParametersToJsonParamsObjectString("some random data");

            er.InputString = data;

            ValidationResult result = er.Validate();

            Expect.IsFalse(result.Success);
            List <ValidationFailures> failures = new List <ValidationFailures>(result.ValidationFailures);

            Expect.IsTrue(failures.Contains(ValidationFailures.InvalidApiKeyToken));
        }