Exemple #1
0
    public static async Task <HttpClientService> GetNew(HttpStatusCode coreStatusCode, string coreContent, bool validTokenResponse)
    {
        var httpClientService = new HttpClientServiceFactory(
            new CoreHttpClient(
                IHttpClientFactoryMocks.Get(coreStatusCode, coreContent).CreateClient()
                ),
            new HttpRequestMessageFactory(
                IHttpContextAccessorMocks.Get(),
                Options.Create(new HttpClientServiceOptions())
                ),
            new IdentityServerService(
                new IdentityServerHttpClientSelector(
                    new List <IIdentityServerHttpClient> {
            {
                new ClientCredentialsHttpClient(
                    IHttpClientFactoryMocks.Get(HttpStatusCode.OK).CreateClient()
                    )
            },
            {
                new PasswordHttpClient(
                    IHttpClientFactoryMocks.Get(HttpStatusCode.OK).CreateClient()
                    )
            }
        }
                    ),
                ITokenResponseCacheManagerMocks.Get(
                    validTokenResponse
                    ? await TokenResponseObjects.GetValidTokenResponseAsync("access_token", 5)
                    : await TokenResponseObjects.GetInvalidTokenResponseAsync("invalid_client")
                    )
                )
            ).CreateHttpClientService();

        return(httpClientService);
    }
        public async Task IdentityServerService_GetTokenResponse_ShouldReturnAccessToken()
        {
            var identityServerService = new IdentityServerService(
                new IdentityServerHttpClientSelector(
                    new List <IIdentityServerHttpClient> {
                {
                    new ClientCredentialsHttpClient(
                        IHttpClientFactoryMocks.Get(
                            HttpStatusCode.OK,
                            TokenResponseObjects.GetValidTokenResponseString("live_access_token", 10)
                            ).CreateClient("test")
                        )
                }
            }
                    ),
                new TokenResponseCacheManager(
                    new MemoryCache(
                        Options.Create(new MemoryCacheOptions())
                        )
                    )
                );

            var tokenServiceOptions = new ClientCredentialsOptions
            {
                Address      = "http://localhost/" + Guid.NewGuid(),
                ClientId     = "ClientId",
                ClientSecret = "secret",
                Scope        = "scope"
            };

            var accessToken = await identityServerService.GetTokenResponseAsync(tokenServiceOptions);

            Assert.AreEqual("live_access_token", accessToken.AccessToken);
        }
        public void ClientCredentialsHttpClient_HttpClientOptionsType_ShouldBeCorrect()
        {
            var httpClient = new ClientCredentialsHttpClient(
                IHttpClientFactoryMocks.Get(
                    HttpStatusCode.OK,
                    TokenResponseObjects.GetValidTokenResponseString("access_token", 10)
                    ).CreateClient("test")
                );

            Assert.AreEqual(typeof(ClientCredentialsOptions), httpClient.HttpClientOptionsType);
        }
    public static async Task <TokenResponse> GetInvalidTokenResponseAsync(string error)
    {
        var httpClient = IHttpClientFactoryMocks.Get(
            HttpStatusCode.OK,
            GetInvalidTokenResponseString(error)
            ).CreateClient("TokenResponse");

        return(await httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
        {
            Address = "http://localhost/"
        }));
    }
        public void ClientCredentialsHttpClient_GetCacheKey_ShouldReturnCorrectHash()
        {
            var httpClient = new ClientCredentialsHttpClient(
                IHttpClientFactoryMocks.Get(HttpStatusCode.OK).CreateClient("test")
                );

            var clientCredentialOptions = new ClientCredentialsOptions
            {
                Address      = "http://localhost/" + Guid.NewGuid(),
                ClientId     = "ClientId",
                ClientSecret = "secret",
                Scope        = "scope"
            };

            var cacheKey = httpClient.GetCacheKey(clientCredentialOptions);
            var hash     = (clientCredentialOptions.Address + clientCredentialOptions.ClientId + clientCredentialOptions.Scope).GetHashCode().ToString();

            Assert.AreEqual(hash, cacheKey);
        }
        public async Task ClientCredentialsHttpClient_GetTokenResponseAsync_ShouldReturnAccessToken()
        {
            var httpClient = new ClientCredentialsHttpClient(
                IHttpClientFactoryMocks.Get(
                    HttpStatusCode.OK,
                    TokenResponseObjects.GetValidTokenResponseString("access_token", 10)
                    ).CreateClient("test")
                );

            var clientCredentialOptions = new ClientCredentialsOptions
            {
                Address      = "http://localhost/" + Guid.NewGuid(),
                ClientId     = "ClientId",
                ClientSecret = "secret",
                Scope        = "scope"
            };

            var tokenResponse = await httpClient.GetTokenResponseAsync(clientCredentialOptions);

            Assert.AreEqual(HttpStatusCode.OK, tokenResponse.HttpStatusCode);
            Assert.AreEqual("access_token", tokenResponse.AccessToken);
        }
        public async Task IdentityServerHttpClientSelector_Get_ShouldSelectPassword()
        {
            var identityServerService = new IdentityServerService(
                new IdentityServerHttpClientSelector(
                    new List <IIdentityServerHttpClient> {
                {
                    new ClientCredentialsHttpClient(
                        IHttpClientFactoryMocks.Get(HttpStatusCode.OK, "{\"access_token\": \"client_credentials_access_token\"}").CreateClient("test")
                        )
                },
                {
                    new PasswordHttpClient(
                        IHttpClientFactoryMocks.Get(HttpStatusCode.OK, "{\"access_token\": \"password_access_token\"}").CreateClient("test")
                        )
                }
            }
                    ),
                new TokenResponseCacheManager(
                    new MemoryCache(
                        Options.Create(new MemoryCacheOptions())
                        )
                    )
                );

            var tokenServiceOptions = new PasswordOptions
            {
                Address      = "http://localhost/" + Guid.NewGuid(),
                ClientId     = "ClientId",
                ClientSecret = "secret",
                Scope        = "scope",
                Username     = "******",
                Password     = "******"
            };

            var accessToken = await identityServerService.GetTokenResponseAsync(tokenServiceOptions);

            Assert.AreEqual("password_access_token", accessToken.AccessToken);
        }