public async Task Malformed_response_document_should_be_handled_correctly()
        {
            var document = "invalid";
            var handler  = new NetworkHandler(document, HttpStatusCode.OK);

            var client = new UserInfoClient(
                Endpoint,
                innerHttpMessageHandler: handler);

            var response = await client.GetAsync("token");

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Exception);
            response.Raw.Should().Be("invalid");
            response.Exception.Should().NotBeNull();
        }
        public async Task Valid_protocol_response_should_be_handled_correctly()
        {
            var document = File.ReadAllText(FileName.Create("success_userinfo_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.OK);

            var client = new UserInfoClient(
                Endpoint,
                innerHttpMessageHandler: handler);

            var response = await client.GetAsync("token");

            response.IsError.Should().BeFalse();
            response.ErrorType.Should().Be(ResponseErrorType.None);
            response.HttpStatusCode.Should().Be(HttpStatusCode.OK);
            response.Claims.Should().NotBeEmpty();
        }
예제 #3
0
        public async Task Http_error_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(HttpStatusCode.NotFound, "not found");

            var client   = new HttpClient(handler);
            var response = await client.GetUserInfoAsync(new UserInfoRequest
            {
                Address = Endpoint,
                Token   = "token"
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Http);
            response.HttpStatusCode.Should().Be(HttpStatusCode.NotFound);
            response.Error.Should().Be("not found");
        }
예제 #4
0
        public async Task Valid_protocol_response_should_be_handled_correctly()
        {
            var document = File.ReadAllText(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "documents", "success_userinfo_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.OK);

            var client = new UserInfoClient(
                Endpoint,
                innerHttpMessageHandler: handler);

            var response = await client.GetAsync("token");

            response.IsError.Should().BeFalse();
            response.ErrorType.Should().Be(ResponseErrorType.None);
            response.HttpStatusCode.Should().Be(HttpStatusCode.OK);
            response.Claims.Should().NotBeEmpty();
        }
예제 #5
0
        public async Task Malformed_response_document_should_be_handled_correctly()
        {
            var document = "invalid";
            var handler  = new NetworkHandler(document, HttpStatusCode.Created);

            var client = new DynamicRegistrationClient(
                Endpoint,
                handler);

            var response = await client.RegisterAsync(new RegistrationRequest());

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Exception);
            response.Raw.Should().Be("invalid");
            response.Exception.Should().NotBeNull();
        }
        public async Task Exception_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(new Exception("exception"));

            var client   = new HttpClient(handler);
            var response = await client.IntrospectTokenAsync(new TokenIntrospectionRequest
            {
                Address = Endpoint,
                Token   = "token"
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Exception);
            response.Error.Should().Be("exception");
            response.Exception.Should().NotBeNull();
        }
        public async Task Valid_protocol_response_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(HttpStatusCode.OK, "ok");
            var client  = new HttpClient(handler);

            var response = await client.RevokeTokenAsync(new HttpClientExtensions.TokenRevocationRequest
            {
                Address  = Endpoint,
                Token    = "token",
                ClientId = "client"
            });

            response.IsError.Should().BeFalse();
            response.ErrorType.Should().Be(ResponseErrorType.None);
            response.HttpStatusCode.Should().Be(HttpStatusCode.OK);
        }
        public async Task Exception_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(new Exception("exception"));

            var client = new TokenRevocationClient(
                Endpoint,
                "client",
                innerHttpMessageHandler: handler);

            var response = await client.RevokeAccessTokenAsync("token");

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Exception);
            response.Error.Should().Be("exception");
            response.Exception.Should().NotBeNull();
        }
        public async Task Exception_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(new Exception("exception"));

            var client   = new HttpClient(handler);
            var response = await client.RegisterClientAsync(new DynamicClientRegistrationRequest
            {
                Address  = Endpoint,
                Document = new DynamicClientRegistrationDocument()
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Exception);
            response.Error.Should().Be("exception");
            response.Exception.Should().NotBeNull();
        }
        public async Task Http_error_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(HttpStatusCode.NotFound, "not found");

            var client   = new HttpClient(handler);
            var response = await client.RegisterClientAsync(new DynamicClientRegistrationRequest
            {
                Address  = Endpoint,
                Document = new DynamicClientRegistrationDocument()
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Http);
            response.HttpStatusCode.Should().Be(HttpStatusCode.NotFound);
            response.Error.Should().Be("not found");
        }
        public async Task Http_error_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(HttpStatusCode.NotFound, "not found");

            var client   = new HttpClient(handler);
            var response = await client.RequestDeviceAuthorizationAsync(new DeviceAuthorizationRequest
            {
                Address  = Endpoint,
                ClientId = "client"
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Http);
            response.HttpStatusCode.Should().Be(HttpStatusCode.NotFound);
            response.Error.Should().Be("not found");
        }
        public async Task Exception_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(new Exception("exception"));

            var client   = new HttpClient(handler);
            var response = await client.RequestDeviceAuthorizationAsync(new DeviceAuthorizationRequest
            {
                Address  = Endpoint,
                ClientId = "client"
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Exception);
            response.Error.Should().Be("exception");
            response.Exception.Should().NotBeNull();
        }
예제 #13
0
        public async Task Http_error_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(HttpStatusCode.NotFound, "not found");
            var client  = new HttpClient(handler)
            {
                BaseAddress = new Uri(_endpoint)
            };

            var disco = await client.GetDiscoveryDocumentAsync();

            disco.IsError.Should().BeTrue();
            disco.ErrorType.Should().Be(ResponseErrorType.Http);
            disco.Error.Should().StartWith("Error connecting to");
            disco.Error.Should().EndWith("not found");
            disco.StatusCode.Should().Be(HttpStatusCode.NotFound);
        }
        public async Task Http_error_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(HttpStatusCode.NotFound, "not found");

            var client = new TokenRevocationClient(
                Endpoint,
                "client",
                innerHttpMessageHandler: handler);

            var response = await client.RevokeAccessTokenAsync("token");

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Http);
            response.HttpStatusCode.Should().Be(HttpStatusCode.NotFound);
            response.Error.Should().Be("not found");
        }
예제 #15
0
        public async Task Http_error_with_non_json_content_should_be_handled_correctly()
        {
            var handler = new NetworkHandler("not_json", HttpStatusCode.Unauthorized);

            var client = new TokenClient(
                Endpoint,
                "client",
                innerHttpMessageHandler: handler);

            var response = await client.RequestClientCredentialsAsync();

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Http);
            response.HttpStatusCode.Should().Be(HttpStatusCode.Unauthorized);
            response.Error.Should().Be("Unauthorized");
            response.Raw.Should().Be("not_json");
        }
        public async Task Malformed_response_document_should_be_handled_correctly()
        {
            var document = "invalid";
            var handler  = new NetworkHandler(document, HttpStatusCode.OK);

            var client   = new HttpClient(handler);
            var response = await client.IntrospectTokenAsync(new TokenIntrospectionRequest
            {
                Address = Endpoint,
                Token   = "token"
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Exception);
            response.Raw.Should().Be("invalid");
            response.Exception.Should().NotBeNull();
        }
        public async Task Http_error_with_non_json_content_should_be_handled_correctly()
        {
            var handler = new NetworkHandler("not_json", HttpStatusCode.Unauthorized);

            var client   = new HttpClient(handler);
            var response = await client.RequestDeviceAuthorizationAsync(new DeviceAuthorizationRequest
            {
                Address  = Endpoint,
                ClientId = "client"
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Http);
            response.HttpStatusCode.Should().Be(HttpStatusCode.Unauthorized);
            response.Error.Should().Be("Unauthorized");
            response.Raw.Should().Be("not_json");
        }
예제 #18
0
        public async Task Http_error_with_non_json_content_should_be_handled_correctly()
        {
            var handler = new NetworkHandler("not_json", HttpStatusCode.InternalServerError);
            var client  = new HttpClient(handler)
            {
                BaseAddress = new Uri(_endpoint)
            };

            var disco = await client.GetDiscoveryDocumentAsync();

            disco.IsError.Should().BeTrue();
            disco.ErrorType.Should().Be(ResponseErrorType.Http);
            disco.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
            disco.Error.Should().Contain("Internal Server Error");
            disco.Raw.Should().Be("not_json");
            disco.Json.Should().BeNull();
        }
        public async Task Valid_protocol_error_should_be_handled_correctly()
        {
            var document = File.ReadAllText(FileName.Create("failure_token_revocation_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.BadRequest);

            var client = new TokenRevocationClient(
                Endpoint,
                "client",
                innerHttpMessageHandler: handler);

            var response = await client.RevokeAccessTokenAsync("token");

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Protocol);
            response.HttpStatusCode.Should().Be(HttpStatusCode.BadRequest);
            response.Error.Should().Be("error");
        }
        public async Task Http_error_with_non_json_content_should_be_handled_correctly()
        {
            var handler = new NetworkHandler("not_json", HttpStatusCode.InternalServerError);
            var client  = new HttpClient(handler)
            {
                BaseAddress = new Uri(_endpoint)
            };

            var jwk = await client.GetJsonWebKeySetAsync();

            jwk.IsError.Should().BeTrue();
            jwk.ErrorType.Should().Be(ResponseErrorType.Http);
            jwk.HttpStatusCode.Should().Be(HttpStatusCode.InternalServerError);
            jwk.Error.Should().Contain("Internal Server Error");
            jwk.Raw.Should().Be("not_json");
            jwk.Json.ValueKind.Should().Be(JsonValueKind.Undefined);
        }
        public async Task Success_protocol_response_should_be_handled_correctly()
        {
            var document = File.ReadAllText(FileName.Create("success_introspection_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.OK);

            var client = new HttpClient(handler)
            {
                BaseAddress = new Uri(Endpoint)
            };

            var response = await client.IntrospectTokenAsync(new TokenIntrospectionRequest
            {
                Token = "token"
            });

            response.IsError.Should().BeFalse();
            response.ErrorType.Should().Be(ResponseErrorType.None);
            response.HttpStatusCode.Should().Be(HttpStatusCode.OK);
            response.IsActive.Should().BeTrue();
            response.Claims.Should().NotBeEmpty();

            var audiences = response.Claims.Where(c => c.Type == "aud").ToList();

            audiences.Count().Should().Be(2);
            audiences.First().Value.Should().Be("https://idsvr4/resources");
            audiences.Skip(1).First().Value.Should().Be("api1");

            response.Claims.First(c => c.Type == "iss").Value.Should().Be("https://idsvr4");
            response.Claims.First(c => c.Type == "nbf").Value.Should().Be("1475824871");
            response.Claims.First(c => c.Type == "exp").Value.Should().Be("1475828471");
            response.Claims.First(c => c.Type == "client_id").Value.Should().Be("client");
            response.Claims.First(c => c.Type == "sub").Value.Should().Be("1");
            response.Claims.First(c => c.Type == "auth_time").Value.Should().Be("1475824871");
            response.Claims.First(c => c.Type == "idp").Value.Should().Be("local");
            response.Claims.First(c => c.Type == "amr").Value.Should().Be("password");
            response.Claims.First(c => c.Type == "active").Value.Should().Be("true");

            var scopes = response.Claims.Where(c => c.Type == "scope").ToList();

            scopes.Count().Should().Be(2);
            scopes.First().Value.Should().Be("api1");
            scopes.First().Issuer.Should().Be("https://idsvr4");
            scopes.Skip(1).First().Value.Should().Be("api2");
            scopes.Skip(1).First().Issuer.Should().Be("https://idsvr4");
        }
        public async Task Valid_protocol_error_should_be_handled_correctly()
        {
            var document = File.ReadAllText(FileName.Create("failure_token_revocation_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.BadRequest);
            var client   = new HttpClient(handler);

            var response = await client.RevokeTokenAsync(new HttpClientExtensions.TokenRevocationRequest
            {
                Address  = Endpoint,
                Token    = "token",
                ClientId = "client"
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Protocol);
            response.HttpStatusCode.Should().Be(HttpStatusCode.BadRequest);
            response.Error.Should().Be("error");
        }
예제 #23
0
        public DiscoveryClientTests()
        {
            var discoFileName = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "documents", "discovery.json");
            var document      = File.ReadAllText(discoFileName);

            var jwksFileName = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "documents", "discovery_jwks.json");
            var jwks         = File.ReadAllText(jwksFileName);

            _successHandler = new NetworkHandler(request =>
            {
                if (request.RequestUri.AbsoluteUri.EndsWith("jwks"))
                {
                    return(jwks);
                }

                return(document);
            }, HttpStatusCode.OK);
        }
예제 #24
0
        public async Task Valid_protocol_error_should_be_handled_correctly()
        {
            var document = File.ReadAllText(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "documents", "failure_registration_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.BadRequest);

            var client = new DynamicRegistrationClient(
                Endpoint,
                handler);

            var response = await client.RegisterAsync(new RegistrationRequest());

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Protocol);
            response.HttpStatusCode.Should().Be(HttpStatusCode.BadRequest);
            response.Error.Should().Be("invalid_redirect_uri");
            response.ErrorDescription.Should().Be("One or more redirect_uri values are invalid");
            response.TryGet("custom").Should().Be("custom");
        }
        public JsonWebkeyExtensionsTests()
        {
            var discoFileName = FileName.Create("discovery.json");
            var document      = File.ReadAllText(discoFileName);

            var jwksFileName = FileName.Create("discovery_jwks.json");
            var jwks         = File.ReadAllText(jwksFileName);

            _successHandler = new NetworkHandler(request =>
            {
                if (request.RequestUri.AbsoluteUri.EndsWith("jwks"))
                {
                    return(jwks);
                }

                return(document);
            }, HttpStatusCode.OK);
        }
예제 #26
0
        public async Task Valid_protocol_response_should_be_handled_correctly()
        {
            var document = File.ReadAllText(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "documents", "success_introspection_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.OK);

            var client = new IntrospectionClient(
                Endpoint,
                "client",
                innerHttpMessageHandler: handler);

            var response = await client.SendAsync(new IntrospectionRequest { Token = "token" });

            response.IsError.Should().BeFalse();
            response.ErrorType.Should().Be(ResponseErrorType.None);
            response.HttpStatusCode.Should().Be(HttpStatusCode.OK);
            response.IsActive.Should().BeTrue();
            response.Claims.Should().NotBeEmpty();
        }
        public async Task Http_error_at_jwk_with_json_content_should_be_handled_correctly()
        {
            var handler = new NetworkHandler(request =>
            {
                HttpResponseMessage response;

                if (!request.RequestUri.AbsoluteUri.Contains("jwk"))
                {
                    var discoFileName = FileName.Create("discovery.json");
                    var document      = File.ReadAllText(discoFileName);

                    response = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent(document)
                    };
                }
                else
                {
                    var content = new
                    {
                        foo = "foo",
                        bar = "bar"
                    };

                    response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(content))
                    };
                }

                return(response);
            });

            var client = new DiscoveryClient(_endpoint, handler);
            var disco  = await client.GetAsync();

            disco.IsError.Should().BeTrue();
            disco.ErrorType.Should().Be(ResponseErrorType.Http);
            disco.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
            disco.Error.Should().Contain("Internal Server Error");

            disco.Json.TryGetString("foo").Should().Be("foo");
            disco.Json.TryGetString("bar").Should().Be("bar");
        }
예제 #28
0
        public async Task Valid_protocol_error_should_be_handled_correctly()
        {
            var document = File.ReadAllText(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "documents", "failure_token_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.BadRequest);

            var client = new TokenClient(
                Endpoint,
                "client",
                innerHttpMessageHandler: handler);

            var response = await client.RequestClientCredentialsAsync();

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Protocol);
            response.HttpStatusCode.Should().Be(HttpStatusCode.BadRequest);
            response.Error.Should().Be("error");
            response.ErrorDescription.Should().Be("error_description");
            response.TryGet("custom").Should().Be("custom");
        }
        public async Task Valid_protocol_error_should_be_handled_correctly()
        {
            var document = File.ReadAllText(FileName.Create("failure_registration_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.BadRequest);

            var client   = new HttpClient(handler);
            var response = await client.RegisterClientAsync(new DynamicClientRegistrationRequest
            {
                Address  = Endpoint,
                Document = new DynamicClientRegistrationDocument()
            });

            response.IsError.Should().BeTrue();
            response.ErrorType.Should().Be(ResponseErrorType.Protocol);
            response.HttpStatusCode.Should().Be(HttpStatusCode.BadRequest);
            response.Error.Should().Be("invalid_redirect_uri");
            response.ErrorDescription.Should().Be("One or more redirect_uri values are invalid");
            response.TryGet("custom").Should().Be("custom");
        }
예제 #30
0
        public async Task Setting_basic_authentication_style_should_send_basic_authentication_header()
        {
            var document = File.ReadAllText(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "documents", "success_token_response.json"));
            var handler  = new NetworkHandler(document, HttpStatusCode.OK);

            var client = new TokenClient(
                Endpoint,
                "client",
                "secret",
                innerHttpMessageHandler: handler);

            var response = await client.RequestClientCredentialsAsync();

            var request = handler.Request;

            request.Headers.Authorization.Should().NotBeNull();
            request.Headers.Authorization.Scheme.Should().Be("Basic");
            request.Headers.Authorization.Parameter.Should().Be(Convert.ToBase64String(Encoding.UTF8.GetBytes("client:secret")));
        }