public async Task RemoveAllGrantsAsync_should_remove_all_grants()
        {
            await _subject.StoreUserConsentAsync(new Consent
            {
                ClientId  = "client1",
                SubjectId = "123",
                Scopes    = new[] { "foo1", "foo2" }
            });

            await _subject.StoreUserConsentAsync(new Consent
            {
                ClientId  = "client2",
                SubjectId = "123",
                Scopes    = new[] { "foo3" }
            });

            await _subject.StoreUserConsentAsync(new Consent
            {
                ClientId  = "client1",
                SubjectId = "456",
                Scopes    = new[] { "foo3" }
            });

            await _subject.StoreReferenceTokenAsync("key1", new Token
            {
                ClientId     = "client1",
                Audience     = "aud",
                CreationTime = DateTime.Now,
                Type         = "type",
                Claims       = new List <Claim>
                {
                    new Claim("sub", "123"),
                    new Claim("scope", "bar1"),
                    new Claim("scope", "bar2")
                }
            });

            await _subject.StoreReferenceTokenAsync("key2", new Token
            {
                ClientId     = "client2",
                Audience     = "aud",
                CreationTime = DateTime.Now,
                Type         = "type",
                Claims       = new List <Claim>
                {
                    new Claim("sub", "123"),
                    new Claim("scope", "bar3")
                }
            });

            await _subject.StoreReferenceTokenAsync("key3", new Token
            {
                ClientId     = "client1",
                Audience     = "aud",
                CreationTime = DateTime.Now,
                Type         = "type",
                Claims       = new List <Claim>
                {
                    new Claim("sub", "456"),
                    new Claim("scope", "bar3")
                }
            });

            await _subject.StoreRefreshTokenAsync("key4", new RefreshToken
            {
                CreationTime = DateTime.Now,
                Lifetime     = 10,
                AccessToken  = new Token
                {
                    ClientId     = "client1",
                    Audience     = "aud",
                    CreationTime = DateTime.Now,
                    Type         = "type",
                    Claims       = new List <Claim>
                    {
                        new Claim("sub", "123"),
                        new Claim("scope", "baz1"),
                        new Claim("scope", "baz2")
                    }
                },
                Version = 1
            });

            await _subject.StoreRefreshTokenAsync("key5", new RefreshToken
            {
                CreationTime = DateTime.Now,
                Lifetime     = 10,
                AccessToken  = new Token
                {
                    ClientId     = "client1",
                    Audience     = "aud",
                    CreationTime = DateTime.Now,
                    Type         = "type",
                    Claims       = new List <Claim>
                    {
                        new Claim("sub", "456"),
                        new Claim("scope", "baz3")
                    }
                },
                Version = 1
            });

            await _subject.StoreRefreshTokenAsync("key6", new RefreshToken
            {
                CreationTime = DateTime.Now,
                Lifetime     = 10,
                AccessToken  = new Token
                {
                    ClientId     = "client2",
                    Audience     = "aud",
                    CreationTime = DateTime.Now,
                    Type         = "type",
                    Claims       = new List <Claim>
                    {
                        new Claim("sub", "123"),
                        new Claim("scope", "baz3")
                    }
                },
                Version = 1
            });

            await _subject.StoreAuthorizationCodeAsync("key7", new AuthorizationCode
            {
                ClientId        = "client1",
                CreationTime    = DateTime.Now,
                Lifetime        = 10,
                Subject         = _user,
                CodeChallenge   = "challenge",
                RedirectUri     = "http://client/cb",
                Nonce           = "nonce",
                RequestedScopes = new[] { "quux1", "quux2" }
            });

            await _subject.StoreAuthorizationCodeAsync("key8", new AuthorizationCode
            {
                ClientId        = "client2",
                CreationTime    = DateTime.Now,
                Lifetime        = 10,
                Subject         = _user,
                CodeChallenge   = "challenge",
                RedirectUri     = "http://client/cb",
                Nonce           = "nonce",
                RequestedScopes = new[] { "quux3" }
            });

            await _subject.StoreAuthorizationCodeAsync("key9", new AuthorizationCode
            {
                ClientId        = "client1",
                CreationTime    = DateTime.Now,
                Lifetime        = 10,
                Subject         = IdentityServerPrincipal.Create("456", "alice"),
                CodeChallenge   = "challenge",
                RedirectUri     = "http://client/cb",
                Nonce           = "nonce",
                RequestedScopes = new[] { "quux3" }
            });

            await _subject.RemoveAllGrantsAsync("123", "client1");

            (await _subject.GetReferenceTokenAsync("key1")).Should().BeNull();
            (await _subject.GetReferenceTokenAsync("key2")).Should().NotBeNull();
            (await _subject.GetReferenceTokenAsync("key3")).Should().NotBeNull();
            (await _subject.GetRefreshTokenAsync("key4")).Should().BeNull();
            (await _subject.GetRefreshTokenAsync("key5")).Should().NotBeNull();
            (await _subject.GetRefreshTokenAsync("key6")).Should().NotBeNull();
            (await _subject.GetAuthorizationCodeAsync("key7")).Should().BeNull();
            (await _subject.GetAuthorizationCodeAsync("key8")).Should().NotBeNull();
            (await _subject.GetAuthorizationCodeAsync("key9")).Should().NotBeNull();
        }
        public async Task RemoveAllGrantsAsync_should_remove_all_grants()
        {
            await _userConsent.StoreUserConsentAsync(new Consent()
            {
                ClientId  = "client1",
                SubjectId = "123",
                Scopes    = new string[] { "foo1", "foo2" }
            });

            await _userConsent.StoreUserConsentAsync(new Consent()
            {
                ClientId  = "client2",
                SubjectId = "123",
                Scopes    = new string[] { "foo3" }
            });

            await _userConsent.StoreUserConsentAsync(new Consent()
            {
                ClientId  = "client1",
                SubjectId = "456",
                Scopes    = new string[] { "foo3" }
            });

            var handle1 = await _referenceTokens.StoreReferenceTokenAsync(new Token()
            {
                ClientId     = "client1",
                Audiences    = { "aud" },
                CreationTime = DateTime.UtcNow,
                Lifetime     = 10,
                Type         = "type",
                Claims       = new List <Claim>
                {
                    new Claim("sub", "123"),
                    new Claim("scope", "bar1"),
                    new Claim("scope", "bar2")
                }
            });

            var handle2 = await _referenceTokens.StoreReferenceTokenAsync(new Token()
            {
                ClientId     = "client2",
                Audiences    = { "aud" },
                CreationTime = DateTime.UtcNow,
                Lifetime     = 10,
                Type         = "type",
                Claims       = new List <Claim>
                {
                    new Claim("sub", "123"),
                    new Claim("scope", "bar3")
                }
            });

            var handle3 = await _referenceTokens.StoreReferenceTokenAsync(new Token()
            {
                ClientId     = "client1",
                Audiences    = { "aud" },
                CreationTime = DateTime.UtcNow,
                Lifetime     = 10,
                Type         = "type",
                Claims       = new List <Claim>
                {
                    new Claim("sub", "456"),
                    new Claim("scope", "bar3")
                }
            });

            var handle4 = await _refreshTokens.StoreRefreshTokenAsync(new RefreshToken()
            {
                CreationTime = DateTime.UtcNow,
                Lifetime     = 10,
                AccessToken  = new Token
                {
                    ClientId     = "client1",
                    Audiences    = { "aud" },
                    CreationTime = DateTime.UtcNow,
                    Type         = "type",
                    Claims       = new List <Claim>
                    {
                        new Claim("sub", "123"),
                        new Claim("scope", "baz1"),
                        new Claim("scope", "baz2")
                    }
                },
                Version = 1
            });

            var handle5 = await _refreshTokens.StoreRefreshTokenAsync(new RefreshToken()
            {
                CreationTime = DateTime.UtcNow,
                Lifetime     = 10,
                AccessToken  = new Token
                {
                    ClientId     = "client1",
                    Audiences    = { "aud" },
                    CreationTime = DateTime.UtcNow,
                    Type         = "type",
                    Claims       = new List <Claim>
                    {
                        new Claim("sub", "456"),
                        new Claim("scope", "baz3")
                    }
                },
                Version = 1
            });

            var handle6 = await _refreshTokens.StoreRefreshTokenAsync(new RefreshToken()
            {
                CreationTime = DateTime.UtcNow,
                Lifetime     = 10,
                AccessToken  = new Token
                {
                    ClientId     = "client2",
                    Audiences    = { "aud" },
                    CreationTime = DateTime.UtcNow,
                    Type         = "type",
                    Claims       = new List <Claim>
                    {
                        new Claim("sub", "123"),
                        new Claim("scope", "baz3")
                    }
                },
                Version = 1
            });

            var handle7 = await _codes.StoreAuthorizationCodeAsync(new AuthorizationCode()
            {
                ClientId        = "client1",
                CreationTime    = DateTime.UtcNow,
                Lifetime        = 10,
                Subject         = _user,
                CodeChallenge   = "challenge",
                RedirectUri     = "http://client/cb",
                Nonce           = "nonce",
                RequestedScopes = new string[] { "quux1", "quux2" }
            });

            var handle8 = await _codes.StoreAuthorizationCodeAsync(new AuthorizationCode()
            {
                ClientId        = "client2",
                CreationTime    = DateTime.UtcNow,
                Lifetime        = 10,
                Subject         = _user,
                CodeChallenge   = "challenge",
                RedirectUri     = "http://client/cb",
                Nonce           = "nonce",
                RequestedScopes = new string[] { "quux3" }
            });

            var handle9 = await _codes.StoreAuthorizationCodeAsync(new AuthorizationCode()
            {
                ClientId        = "client1",
                CreationTime    = DateTime.UtcNow,
                Lifetime        = 10,
                Subject         = new IdentityServerUser("456").CreatePrincipal(),
                CodeChallenge   = "challenge",
                RedirectUri     = "http://client/cb",
                Nonce           = "nonce",
                RequestedScopes = new string[] { "quux3" }
            });

            await _subject.RemoveAllGrantsAsync("123", "client1");

            (await _referenceTokens.GetReferenceTokenAsync(handle1)).Should().BeNull();
            (await _referenceTokens.GetReferenceTokenAsync(handle2)).Should().NotBeNull();
            (await _referenceTokens.GetReferenceTokenAsync(handle3)).Should().NotBeNull();
            (await _refreshTokens.GetRefreshTokenAsync(handle4)).Should().BeNull();
            (await _refreshTokens.GetRefreshTokenAsync(handle5)).Should().NotBeNull();
            (await _refreshTokens.GetRefreshTokenAsync(handle6)).Should().NotBeNull();
            (await _codes.GetAuthorizationCodeAsync(handle7)).Should().BeNull();
            (await _codes.GetAuthorizationCodeAsync(handle8)).Should().NotBeNull();
            (await _codes.GetAuthorizationCodeAsync(handle9)).Should().NotBeNull();
        }