public async Task StoreAuthorizationCodeAsync_should_persist_grant()
    {
        var code1 = new AuthorizationCode()
        {
            ClientId        = "test",
            CreationTime    = DateTime.UtcNow,
            Lifetime        = 10,
            Subject         = _user,
            CodeChallenge   = "challenge",
            RedirectUri     = "http://client/cb",
            Nonce           = "nonce",
            RequestedScopes = new string[] { "scope1", "scope2" }
        };

        var handle = await _codes.StoreAuthorizationCodeAsync(code1);

        var code2 = await _codes.GetAuthorizationCodeAsync(handle);

        code1.ClientId.Should().Be(code2.ClientId);
        code1.CreationTime.Should().Be(code2.CreationTime);
        code1.Lifetime.Should().Be(code2.Lifetime);
        code1.Subject.GetSubjectId().Should().Be(code2.Subject.GetSubjectId());
        code1.CodeChallenge.Should().Be(code2.CodeChallenge);
        code1.RedirectUri.Should().Be(code2.RedirectUri);
        code1.Nonce.Should().Be(code2.Nonce);
        code1.RequestedScopes.Should().BeEquivalentTo(code2.RequestedScopes);
    }
        /// <summary>
        /// Creates the response for a hybrid flow request
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual async Task <AuthorizeResponse> CreateHybridFlowResponseAsync(ValidatedAuthorizeRequest request)
        {
            Logger.LogDebug("Creating Hybrid Flow response.");

            var code = await CreateCodeAsync(request);

            var id = await AuthorizationCodeStore.StoreAuthorizationCodeAsync(code);

            var response = await CreateImplicitFlowResponseAsync(request, id);

            response.Code = id;

            return(response);
        }
        private async Task <string> CreateCodeAsync(ValidatedAuthorizeRequest request)
        {
            var code = new AuthorizationCode
            {
                ClientId            = request.Client.ClientId,
                Lifetime            = request.Client.AuthorizationCodeLifetime,
                Subject             = request.Subject,
                SessionId           = request.SessionId,
                CodeChallenge       = request.CodeChallenge.Sha256(),
                CodeChallengeMethod = request.CodeChallengeMethod,

                IsOpenId        = request.IsOpenIdRequest,
                RequestedScopes = request.ValidatedScopes.GrantedScopes.Select(a => a.Name),
                RedirectUri     = request.RedirectUri,
                Nonce           = request.Nonce,

                WasConsentShown = request.WasConsentShown,
            };

            // store id token and access token and return authorization code
            var id = CryptoRandom.CreateUniqueId();
            await _authorizationCodeStore.StoreAuthorizationCodeAsync(id, code);

            await RaiseCodeIssuedEventAsync(id, code);

            return(id);
        }
示例#4
0
        /// <summary>
        /// Creates an authorization code
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected async Task <string> CreateCodeAsync(ValidatedAuthorizeRequest request)
        {
            var code = new AuthorizationCode
            {
                CreationTime        = Clock.UtcNow.UtcDateTime,
                ClientId            = request.Client.ClientId,
                Lifetime            = request.Client.AuthorizationCodeLifetime,
                Subject             = request.Subject,
                SessionId           = request.SessionId,
                CodeChallenge       = request.CodeChallenge.Sha256(),
                CodeChallengeMethod = request.CodeChallengeMethod,

                IsOpenId        = request.IsOpenIdRequest,
                RequestedScopes = request.ValidatedScopes.GrantedResources.ToScopeNames(),
                RedirectUri     = request.RedirectUri,
                Nonce           = request.Nonce,

                WasConsentShown = request.WasConsentShown
            };

            // store id token and access token and return authorization code
            var id = await AuthorizationCodeStore.StoreAuthorizationCodeAsync(code);

            return(id);
        }
        public async Task GetAllGrantsAsync_should_return_all_grants()
        {
            await _userConsent.StoreUserConsentAsync(new Consent()
            {
                CreationTime = DateTime.UtcNow,
                ClientId     = "client1",
                SubjectId    = "123",
                Scopes       = new string[] { "foo1", "foo2" }
            });

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

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

            var handle1 = await _referenceTokens.StoreReferenceTokenAsync(new Token()
            {
                ClientId     = "client1",
                Audiences    = { "aud" },
                CreationTime = DateTime.UtcNow,
                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,
                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,
                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" }
            });

            var grants = await _subject.GetAllGrantsAsync("123");

            grants.Count().Should().Be(2);
            var grant1 = grants.First(x => x.ClientId == "client1");

            grant1.SubjectId.Should().Be("123");
            grant1.ClientId.Should().Be("client1");
            grant1.Scopes.ShouldBeEquivalentTo(new string[] { "foo1", "foo2", "bar1", "bar2", "baz1", "baz2", "quux1", "quux2" });

            var grant2 = grants.First(x => x.ClientId == "client2");

            grant2.SubjectId.Should().Be("123");
            grant2.ClientId.Should().Be("client2");
            grant2.Scopes.ShouldBeEquivalentTo(new string[] { "foo3", "bar3", "baz3", "quux3" });
        }