Ejemplo n.º 1
0
        public async Task When_Requesting_UserInformation_JwsPayload_For_Scopes_Then_The_JwsPayload_Is_Correct()
        {
            // ARRANGE
            InitializeMockObjects();
            const string subject = "*****@*****.**";
            const string name    = "Habart Thierry";
            var          claims  = new List <Claim>
            {
                new Claim(Constants.StandardResourceOwnerClaimNames.Name, name),
                new Claim(Constants.StandardResourceOwnerClaimNames.Subject, subject)
            };
            var authorizationParameter = new AuthorizationParameter
            {
                Scope = "profile"
            };
            ICollection <Scope> scopes = FakeOpenIdAssets.GetScopes().Where(s => s.Name == "profile").ToList();

            _clientRepositoryStub.Setup(c => c.GetAllAsync()).Returns(Task.FromResult(FakeOpenIdAssets.GetClients()));
            _scopeRepositoryStub.Setup(s => s.SearchByNamesAsync(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult(scopes));

            // ACT
            var result = await _jwtGenerator.GenerateUserInfoPayloadForScopeAsync(authorizationParameter, claims);

            // ASSERT
            Assert.NotNull(result);
            Assert.True(result.ContainsKey(Constants.StandardResourceOwnerClaimNames.Subject));
            Assert.True(result.ContainsKey(Constants.StandardResourceOwnerClaimNames.Name));
            Assert.True(result[Constants.StandardResourceOwnerClaimNames.Subject].ToString().Equals(subject));
            Assert.True(result[Constants.StandardResourceOwnerClaimNames.Name].ToString().Equals(name));
        }
Ejemplo n.º 2
0
        public void When_Requesting_UserInformation_But_The_Name_Claim_Value_Is_Not_Correct_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeMockObjects();
            const string subject = "*****@*****.**";
            const string state   = "state";
            var          claims  = new List <Claim>
            {
                new Claim(Constants.StandardResourceOwnerClaimNames.Subject, subject),
                new Claim(Constants.StandardResourceOwnerClaimNames.Name, "invalid_name")
            };
            var claimsParameter = new List <ClaimParameter>
            {
                new ClaimParameter
                {
                    Name       = Constants.StandardResourceOwnerClaimNames.Subject,
                    Parameters = new Dictionary <string, object>
                    {
                        {
                            SimpleIdServer.Core.Constants.StandardClaimParameterValueNames.ValueName,
                            subject
                        }
                    }
                },
                new ClaimParameter
                {
                    Name       = Constants.StandardResourceOwnerClaimNames.Name,
                    Parameters = new Dictionary <string, object>
                    {
                        {
                            SimpleIdServer.Core.Constants.StandardClaimParameterValueNames.ValueName,
                            "name"
                        }
                    }
                }
            };

            var authorizationParameter = new AuthorizationParameter
            {
                Scope = "profile",
                State = state
            };
            ICollection <Scope> scopes = FakeOpenIdAssets.GetScopes().Where(s => s.Name == "profile").ToList();

            _clientRepositoryStub.Setup(c => c.GetAllAsync()).Returns(Task.FromResult(FakeOpenIdAssets.GetClients()));
            _scopeRepositoryStub.Setup(s => s.SearchByNamesAsync(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult(scopes));

            // ACT & ASSERT
            var exception = Assert.Throws <IdentityServerExceptionWithState>(() => _jwtGenerator.GenerateFilteredUserInfoPayload(
                                                                                 claimsParameter,
                                                                                 authorizationParameter, claims));

            Assert.NotNull(exception);
            Assert.True(exception.Code == ErrorCodes.InvalidGrant);
            Assert.True(exception.Message == string.Format(ErrorDescriptions.TheClaimIsNotValid, Constants.StandardResourceOwnerClaimNames.Name));
            Assert.True(exception.State == state);
        }
Ejemplo n.º 3
0
        public void When_Requesting_UserInformation_But_The_Essential_Claim_Subject_Is_Empty_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeMockObjects();
            const string subject = "";
            const string state   = "state";
            var          claims  = new List <Claim>
            {
                new Claim(Jwt.Constants.StandardResourceOwnerClaimNames.Subject, subject)
            };
            var claimIdentity   = new ClaimsIdentity(claims, "fake");
            var claimsPrincipal = new ClaimsPrincipal(claimIdentity);
            var claimsParameter = new List <ClaimParameter>
            {
                new ClaimParameter
                {
                    Name       = Jwt.Constants.StandardResourceOwnerClaimNames.Subject,
                    Parameters = new Dictionary <string, object>
                    {
                        {
                            Constants.StandardClaimParameterValueNames.EssentialName,
                            true
                        }
                    }
                }
            };

            var authorizationParameter = new AuthorizationParameter
            {
                Scope = "profile",
                State = state
            };
            ICollection <Scope> scopes = FakeOpenIdAssets.GetScopes().Where(s => s.Name == "profile").ToList();

            _clientRepositoryStub.Setup(c => c.GetAllAsync()).Returns(Task.FromResult(FakeOpenIdAssets.GetClients()));
            _scopeRepositoryStub.Setup(s => s.SearchByNamesAsync(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult(scopes));

            // ACT & ASSERT
            var exception = Assert.Throws <IdentityServerExceptionWithState>(() => _jwtGenerator.GenerateFilteredUserInfoPayload(
                                                                                 claimsParameter,
                                                                                 claimsPrincipal,
                                                                                 authorizationParameter));

            Assert.NotNull(exception);
            Assert.True(exception.Code == ErrorCodes.InvalidGrant);
            Assert.True(exception.Message == string.Format(ErrorDescriptions.TheClaimIsNotValid, Jwt.Constants.StandardResourceOwnerClaimNames.Subject));
            Assert.True(exception.State == state);
        }
Ejemplo n.º 4
0
        public void When_Requesting_UserInformation_For_Some_Valid_Claims_Then_The_JwsPayload_Is_Correct()
        {
            // ARRANGE
            InitializeMockObjects();
            const string subject = "*****@*****.**";
            const string name    = "Habart Thierry";
            var          claims  = new List <Claim>
            {
                new Claim(Constants.StandardResourceOwnerClaimNames.Name, name),
                new Claim(Constants.StandardResourceOwnerClaimNames.Subject, subject)
            };
            var claimsParameter = new List <ClaimParameter>
            {
                new ClaimParameter
                {
                    Name       = Constants.StandardResourceOwnerClaimNames.Name,
                    Parameters = new Dictionary <string, object>
                    {
                        {
                            SimpleIdServer.Core.Constants.StandardClaimParameterValueNames.EssentialName,
                            true
                        }
                    }
                },
                new ClaimParameter
                {
                    Name       = Constants.StandardResourceOwnerClaimNames.Subject,
                    Parameters = new Dictionary <string, object>
                    {
                        {
                            SimpleIdServer.Core.Constants.StandardClaimParameterValueNames.EssentialName,
                            true
                        },
                        {
                            SimpleIdServer.Core.Constants.StandardClaimParameterValueNames.ValueName,
                            subject
                        }
                    }
                }
            };

            var authorizationParameter = new AuthorizationParameter
            {
                Scope = "profile"
            };
            ICollection <Scope> scopes = FakeOpenIdAssets.GetScopes().Where(s => s.Name == "profile").ToList();

            _clientRepositoryStub.Setup(c => c.GetAllAsync()).Returns(Task.FromResult(FakeOpenIdAssets.GetClients()));
            _scopeRepositoryStub.Setup(s => s.SearchByNamesAsync(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult(scopes));

            // ACT
            var result = _jwtGenerator.GenerateFilteredUserInfoPayload(
                claimsParameter,
                authorizationParameter, claims);

            // ASSERT
            Assert.NotNull(result);
            Assert.True(result.ContainsKey(Constants.StandardResourceOwnerClaimNames.Subject));
            Assert.True(result.ContainsKey(Constants.StandardResourceOwnerClaimNames.Name));
            Assert.True(result[Constants.StandardResourceOwnerClaimNames.Subject].ToString().Equals(subject));
            Assert.True(result[Constants.StandardResourceOwnerClaimNames.Name].ToString().Equals(name));
        }