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);
        }
Example #2
0
        protected async Task ApplyADAccessToken(HttpClient httpClient)
        {
            try
            {
                httpClient.BaseAddress = new Uri(GaloreBaseUrl);
                ClientCredentialsOptions options = new ClientCredentialsOptions()
                {
                    AuthorityUrl = AuthorityUrl + TenantId + "/",
                    ClientId     = ClientId,
                    ClientSecret = ClientSecret
                };

                IClientCredentialsManager clientCredentialsManager = new ClientCredentialsManager(options);
                var token = await clientCredentialsManager.GetAccessTokenAsync(Resource);

                if (httpClient.DefaultRequestHeaders.Contains(AUTHORIZATION_HEADER))
                {
                    httpClient.DefaultRequestHeaders.Remove(AUTHORIZATION_HEADER);
                }
                httpClient.DefaultRequestHeaders.Add(APIMSUBKEY_HEADER, APIMSubscriptionKey);
                httpClient.DefaultRequestHeaders.Add(AUTHORIZATION_HEADER, $"Bearer {token}");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        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_ShouldSelectClientCredentials()
        {
            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 ClientCredentialsOptions
            {
                Address      = "http://localhost/" + Guid.NewGuid(),
                ClientId     = "ClientId",
                ClientSecret = "secret",
                Scope        = "scope"
            };

            var accessToken = await identityServerService.GetTokenResponseAsync(tokenServiceOptions);

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