public async Task GetTokenValidClientReturns200(
            TestIdentityServer4TestServerFactory serverFactory,
            TestIdentityServer4TestClientFactory clientFactory,
            string clientId,
            string clientSecret,
            string apiResourceName,
            string apiResourceDisplayName)
        {
            using (var server = await serverFactory
                                .WithClient(new Client
            {
                ClientId = clientId,
                ClientSecrets = new List <Secret> {
                    new Secret(clientSecret.Sha256())
                },
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                AllowedScopes = new List <string> {
                    apiResourceName
                },
            })
                                .WithApiResource(new ApiResource(apiResourceName, apiResourceDisplayName))
                                .WithLogging(new XUnitLoggerFactory(this.output))
                                .Create())
            {
                using (var client = clientFactory
                                    .WithClientId(clientId)
                                    .WithClientSecret(clientSecret)
                                    .Create(server))
                {
                    var response = await client.GetToken();

                    Assert.Equal(HttpStatusCode.OK, response.HttpStatusCode);
                }
            }
        }
        public async Task InvalidClientIdRaiseUnknownClientEvent(
            TestIdentityServer4TestServerFactory serverFactory,
            TestIdentityServer4TestClientFactory clientFactory,
            string clientId,
            string clientSecret,
            string apiResourceName,
            string apiResourceDisplayName,
            IdentityServerEventCapture eventCapture)
        {
            using (var server = await serverFactory
                                .WithApiResource(new ApiResource(apiResourceName, apiResourceDisplayName))
                                .WithEventCapture(eventCapture)
                                .Create())
            {
                using (var client = clientFactory
                                    .WithClientId(clientId)
                                    .WithClientSecret(clientSecret)
                                    .Create(server))
                {
                    await client.GetToken();

                    Assert.True(eventCapture.ContainsMessage("Unknown client"));
                }
            }
        }
        public async Task CreateTokenFactoryReturnsToken(
            TestIdentityServer4TestServerFactory serverFactory,
            TestIdentityServer4TestClientFactory clientFactory,
            string clientId,
            string clientSecret,
            string apiResourceName,
            string apiResourceDisplayName,
            int lifetime)
        {
            using (var server = await serverFactory
                                .WithLogging(new XUnitLoggerFactory(this.output))
                                .WithApiResource(new ApiResource(apiResourceName, apiResourceDisplayName))
                                .WithClient(new Client
            {
                ClientId = clientId,
                ClientSecrets = new List <Secret> {
                    new Secret(clientSecret.Sha256())
                },
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                AllowedScopes = new List <string> {
                    apiResourceName
                },
            }).Create())
            {
                var tokenFactory = server.CreateTokenFactory();
                var tokenResult  = await tokenFactory.CreateToken(
                    lifetime : lifetime,
                    claims : new List <Claim>());

                Assert.NotNull(tokenResult.Token);
            }
        }
        public async Task GetDiscoveryDocReturnsDoc(
            TestIdentityServer4TestServerFactory serverFactory,
            TestIdentityServer4TestClientFactory clientFactory)
        {
            using (var server = await serverFactory.Create())
            {
                using (var client = clientFactory.Create(server))
                {
                    var disco = await client.GetDiscovery();

                    Assert.NotNull(disco.TokenEndpoint);
                }
            }
        }
        public async Task ConfigureServicesValidClientReturns200(
            TestIdentityServer4TestServerFactory serverFactory,
            TestIdentityServer4TestClientFactory clientFactory,
            string clientId,
            string clientSecret,
            string apiResourceName,
            string apiResourceDisplayName)
        {
            using (var server = await serverFactory
                                .WithConfigureServices((context, services) =>
            {
                services
                .AddIdentityServer()
                .AddInMemoryClients(new List <Client>
                {
                    new Client
                    {
                        ClientId = clientId,
                        ClientSecrets = new List <Secret> {
                            new Secret(clientSecret.Sha256())
                        },
                        AllowedGrantTypes = GrantTypes.ClientCredentials,
                        AllowedScopes = new List <string> {
                            apiResourceName
                        },
                    },
                })
                .AddInMemoryApiResources(new List <ApiResource>
                {
                    new ApiResource(apiResourceName, apiResourceDisplayName),
                })
                .AddDefaultEndpoints()
                .AddDefaultSecretParsers()
                .AddDefaultSecretValidators()
                .AddDeveloperSigningCredential();
            })
                                .WithLogging(new XUnitLoggerFactory(this.output))
                                .Create())
            {
                using (var client = clientFactory
                                    .WithClientId(clientId)
                                    .WithClientSecret(clientSecret)
                                    .Create(server))
                {
                    var response = await client.GetToken();

                    Assert.Equal(HttpStatusCode.OK, response.HttpStatusCode);
                }
            }
        }
        public async Task RequestClientCredentialsReturnsAcccessToken(
            TestIdentityServer4TestServerFactory serverFactory,
            TestIdentityServer4TestClientFactory clientFactory,
            string clientId,
            string clientSecret,
            string apiResourceName,
            string apiResourceDisplayName)
        {
            using (var server = await serverFactory
                                .WithLogging(new XUnitLoggerFactory(this.output))
                                .WithApiResource(new ApiResource(apiResourceName, apiResourceDisplayName))
                                .WithClient(new Client
            {
                ClientId = clientId,
                ClientSecrets = new List <Secret> {
                    new Secret(clientSecret.Sha256())
                },
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                AllowedScopes = new List <string> {
                    apiResourceName
                },
            }).Create())
            {
                using (var client = clientFactory
                                    .WithClientId(clientId)
                                    .WithClientSecret(clientSecret)
                                    .Create(server))
                {
                    using (var tokenClient = await client.CreateTokenClient())
                    {
                        var response = await tokenClient.RequestClientCredentialsAsync();

                        Assert.NotNull(response.AccessToken);
                    }
                }
            }
        }
        public async Task InvalidClientSecretRaisesInvalidClientSecretEvent(
            TestIdentityServer4TestServerFactory serverFactory,
            TestIdentityServer4TestClientFactory clientFactory,
            string clientId,
            string clientSecret,
            string invalidClientSecret,
            string apiResourceName,
            string apiResourceDisplayName,
            IdentityServerEventCapture eventCapture)
        {
            using (var server = await serverFactory
                                .WithClient(new Client
            {
                ClientId = clientId,
                ClientSecrets = new List <Secret> {
                    new Secret(clientSecret.Sha256())
                },
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                AllowedScopes = new List <string> {
                    apiResourceName
                },
            })
                                .WithApiResource(new ApiResource(apiResourceName, apiResourceDisplayName))
                                .WithEventCapture(eventCapture)
                                .Create())
            {
                using (var client = clientFactory
                                    .WithClientId(clientId)
                                    .WithClientSecret(invalidClientSecret)
                                    .Create(server))
                {
                    await client.GetToken();

                    Assert.True(eventCapture.ContainsMessage("Invalid client secret"));
                }
            }
        }