Inheritance: IResourceOwnerPasswordValidator
        public async Task GetProfileDataAsync(ProfileDataRequestContext profileContext)
        {
            if (!string.IsNullOrEmpty(profileContext.Subject.Identity.Name))
            {
                var customer = await _context.Customers.SingleOrDefaultAsync(m => m.Email == profileContext.Subject.Identity.Name);

                if (customer != null)
                {
                    var claims = ResourceOwnerPasswordValidator.GetUserClaims(customer);
                    profileContext.IssuedClaims = claims.Where(x =>
                                                               profileContext.RequestedClaimTypes.Contains(x.Type)).ToList();
                }
            }
            else
            {
                var customerId = profileContext.Subject.Claims.FirstOrDefault(x => x.Type == "sub");
                if (!string.IsNullOrEmpty(customerId.Value))
                {
                    var customer = await _context.Customers.SingleOrDefaultAsync(u => u.Id == Guid.Parse(customerId.Value));

                    if (customer != null)
                    {
                        var claims = ResourceOwnerPasswordValidator.GetUserClaims(customer);
                        profileContext.IssuedClaims = claims.Where(x =>
                                                                   profileContext.RequestedClaimTypes.Contains(x.Type)).ToList();
                    }
                }
            }
        }
示例#2
0
        public void InitialData()
        {
            this.passwordValidator = new ResourceOwnerPasswordValidator(userStore);

            this.userWithoutClaims = new User {
                Id       = "1",
                Nickname = "gustavo.matos",
                Username = "******",
                Roles    = new List <string> {
                    "Admin",
                    "Analyst"
                },
                Email    = "*****@*****.**",
                Password = "******"
            };

            this.userWitHashedPassword = new User {
                Id       = "1",
                Nickname = "gustavo.matos",
                Username = "******",
                Roles    = new List <string> {
                    "Admin",
                    "Analyst"
                },
                Email    = "*****@*****.**",
                Password = BCrypt.Net.BCrypt.HashPassword("123456")
            };

            this.userWithClaims = new User {
                Id       = "1",
                Nickname = "gustavo.matos",
                Username = "******",
                Roles    = new List <string> {
                    "Admin",
                    "Analyst"
                },
                Email    = "*****@*****.**",
                Password = BCrypt.Net.BCrypt.HashPassword("123456"),
                Claims   = new Dictionary <string, IEnumerable <string> >()
                {
                    { "TESTE", new List <string>()
                      {
                          "Hahahah"
                      } }
                }
            };

            this.grantValidationResultError = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid_user_credentials");
            this.grantValidationResultWhenExceptionIsThrown = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "user_validation_error");
            this.additionalInfoResult = new GrantValidationResult(
                subject: userWithClaims.Id,
                authenticationMethod: "custom",
                claims: ResourceOwnerPasswordValidator.GetUserClaims(userWithClaims)
                );
        }
示例#3
0
        public void When_UserClaims_is_not_null_return_Claims_array_of_length_4_plus_UserRolesCount_plus_UserClaimsCount()
        {
            var listaProdutos = new List <string> {
                "Mensagem"
            };

            this.userWithClaims.Claims.Add("Produtos", listaProdutos);

            var claimsArray = ResourceOwnerPasswordValidator.GetUserClaims(this.userWithClaims);

            Assert.AreEqual(4 + this.userWithClaims.Roles.Count + this.userWithClaims.Claims.Count, claimsArray.Length);
        }
示例#4
0
        /// <summary>
        /// Gets user profile data in terms of claims when calling /connect/userinfo ( to load claims for a user)
        /// </summary>
        /// <param name="context">Context</param>
        /// <returns>profile data getting task</returns>
        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            try
            {
                // depending on the scope accessing the user data.
                if (!string.IsNullOrEmpty(context.Subject.Identity.Name))
                {
                    // get user from db (in my case this is by login)
                    var user = await this.userRepository.FindAsync(context.Subject.Identity.Name);

                    // checking user
                    if (user != null)
                    {
                        var claims = ResourceOwnerPasswordValidator.GetUserClaims(user);

                        // set issued claims to return
                        context.IssuedClaims.AddRange(claims);
                    }
                }
                else
                {
                    // get subject from context (this was set ResourceOwnerPasswordValidator.ValidateAsync),
                    // where and subject was set to my user id.
                    var userId = context.Subject.Claims.FirstOrDefault(x => x.Type == "sub");

                    if (!string.IsNullOrEmpty(userId?.Value) && int.Parse(userId.Value) > 0)
                    {
                        // get user from db (find user by user id)
                        var user = await this.userRepository.FindAsync(int.Parse(userId.Value));

                        // issue the claims for the user
                        if (user != null)
                        {
                            var claims = ResourceOwnerPasswordValidator.GetUserClaims(user);

                            context.IssuedClaims.AddRange(claims);
                        }
                    }
                }
                //return context.IssuedClaims;
            }
            catch (Exception ex)
            {
                //log your error
            }
        }
示例#5
0
        public void When_User_and_ContextPassword_are_valid_creates_valid_GrantValidationResult()
        {
            var userClaims = ResourceOwnerPasswordValidator.GetUserClaims(userWithClaims);

            this.userWithClaims.Claims.Add("username", new List <string>()
            {
                this.userWithClaims.Username
            });
            this.userWithClaims.Claims.Add("sub", new List <string>()
            {
                this.userWithClaims.Id
            });
            this.userWithClaims.Claims.Add("role", this.userWithClaims.Roles);
            this.userWithClaims.Claims.Add("email", new List <string>()
            {
                this.userWithClaims.Email
            });

            foreach (var cc in this.userWithClaims.Claims)
            {
                var match = userClaims.Where(e => e.Type == cc.Key);
                Assert.IsTrue(match.Where(e => cc.Value.Contains(e.Value)).Any());
            }
        }
示例#6
0
        public void When_UserClaims_is_null_return_Claims_array_of_length_4_plus_UserRolesCount()
        {
            var claimsArray = ResourceOwnerPasswordValidator.GetUserClaims(this.userWithoutClaims);

            Assert.AreEqual(4 + this.userWithClaims.Roles.Count, claimsArray.Length);
        }