Пример #1
0
        public async Task GetDiscoveryDocumentAsync()
        {
            var issuer                = "http://test.openiddict.com";
            var mockConfiguration     = GetMockConfiguration(issuer: issuer);
            var mockHttpClientFactory = GetMockHttpClientFactory((mockHttp) =>
            {
                mockHttp.Expect($"{issuer}/.well-known/openid-configuration")
                .Respond("application/json", @"
                    {
                        ""issuer"": """ + issuer + @""",
                        ""authorization_endpoint"": ""TestAuthorizationEndpoint"",                        
                        ""token_endpoint"": ""TestTokenEndpoint"",
                        ""scopes_supported"": [ ""scope1"", ""scope2"" ]
                    }");
            });

            var          mockHttpContext = A.Fake <HttpContext>();
            StringValues ignored         = StringValues.Empty;

            A.CallTo(() => mockHttpContext.Request.Headers.TryGetValue("Authorization", out ignored))
            .Returns(false);

            var mockHttpContextAccessor = A.Fake <IHttpContextAccessor>();

            A.CallTo(() => mockHttpContextAccessor.HttpContext)
            .Returns(mockHttpContext);

            var authorizationHandler = new AuthenticationHandler(mockConfiguration, mockHttpClientFactory, mockHttpContextAccessor);
            var discoveryDocument    = await authorizationHandler.GetDiscoveryDocumentAsync();

            Assert.AreEqual(issuer, discoveryDocument.Issuer);
            Assert.AreEqual("TestAuthorizationEndpoint", discoveryDocument.AuthorizationEndpoint);
            Assert.AreEqual("TestTokenEndpoint", discoveryDocument.TokenEndpoint);
            CollectionAssert.AreEqual(new[] { "scope1", "scope2" }, discoveryDocument.ScopesSupported.ToList());
        }
Пример #2
0
        public async Task GetDiscoveryDocumentAsync_ResponseCode404_Throws()
        {
            var tenantId     = "TestTenant";
            var issuer       = "http://test.openiddict.com";
            var scopeApiPath = "http://somewhere.else.com/scope/{{tenantId}}";

            var mockConfiguration     = GetMockConfiguration(issuer: issuer, scopeApiPath: scopeApiPath);
            var mockHttpClientFactory = GetMockHttpClientFactory((mockHttp) =>
            {
                mockHttp.Expect($"{issuer}/.well-known/openid-configuration")
                .Respond(System.Net.HttpStatusCode.NotFound, "application/json", string.Empty);
            });

            var          mockHttpContext = A.Fake <HttpContext>();
            StringValues ignored         = StringValues.Empty;

            A.CallTo(() => mockHttpContext.Request.Headers.TryGetValue("Authorization", out ignored))
            .Returns(false);

            var mockHttpContextAccessor = A.Fake <IHttpContextAccessor>();

            A.CallTo(() => mockHttpContextAccessor.HttpContext)
            .Returns(mockHttpContext);

            var authorizationHandler = new AuthenticationHandler(mockConfiguration, mockHttpClientFactory, mockHttpContextAccessor);

            var exception = await Assert.ThrowsExceptionAsync <AuthenticationException>(() => authorizationHandler.GetDiscoveryDocumentAsync());

            Assert.AreEqual("Could not fetch discovery document at URL: http://test.openiddict.com/.well-known/openid-configuration", exception.Message);
        }