Beispiel #1
0
        public void GetRoleClaims_UserHasDefaultRoles_ReturnEmptyClaims()
        {
            var result = RolesServiceUtils.GetRoleClaims(new User());

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Empty);
        }
Beispiel #2
0
        public void GetRoleClaims_UserIsNull_ReturnEmptyClaims()
        {
            var result = RolesServiceUtils.GetRoleClaims(null);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Empty);
        }
Beispiel #3
0
        public void GetRoleClaims_VctRoleIsOutOfRange_ThrowArgumentOutOfRangeException()
        {
            var user = new User();

            user.SetRoles(vct: 5);

            Assert.That(() => RolesServiceUtils.GetRoleClaims(user),
                        Throws.Exception.TypeOf <ArgumentOutOfRangeException>());
        }
Beispiel #4
0
        public void GetRoleClaims_UserIsTrialAdminAndMapperIsOutOfRange_ThrowArgumentOutOfRangeException()
        {
            var user = new User();

            user.SetRoles(admin: 1, mapper: 5);

            Assert.That(() => RolesServiceUtils.GetRoleClaims(user),
                        Throws.Exception.TypeOf <ArgumentOutOfRangeException>());
        }
Beispiel #5
0
        public void GetRoleClaims_UserIsAdmin_ReturnClaimsWithAdminRole()
        {
            var user = new User();

            user.SetRoles(2);

            var result = RolesServiceUtils.GetRoleClaims(user);

            Assert.That(result, Is.Not.Empty);
            Assert.That(result.Count(), Is.EqualTo(1));
            Assert.That(result.First().Value, Is.EqualTo("admin").IgnoreCase);
        }
Beispiel #6
0
        public void GetRoleClaims_UserIsSupporterAndScripter_ReturnClaimsWithSupporterAndScripterRole()
        {
            var user = new User();

            user.SetRoles(supporter: 1, scripter: 3);

            var result = RolesServiceUtils.GetRoleClaims(user);

            Assert.That(result, Is.Not.Empty);
            Assert.That(result.Count(), Is.EqualTo(2));
            Assert.That(result.First().Value, Is.EqualTo("supporter").IgnoreCase);
            Assert.That(result.Last().Value, Is.EqualTo("scripter").IgnoreCase);
        }
Beispiel #7
0
        public string GenerateToken(User user)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.Username),
                new Claim(ClaimTypes.Email, user.Email)
            };

            var roleClaims = RolesServiceUtils.GetRoleClaims(user);

            foreach (var roleClaim in roleClaims)
            {
                claims.Add(roleClaim);
            }

            if (Configuration.IsDev(user.Id))
            {
                claims.Add(new Claim(ClaimTypes.Role, Utils.EnumToString(RoleType.Owner)));
            }

            var key = new SymmetricSecurityKey(
                Encoding.UTF8.GetBytes(Configuration.GetValue <string>(AppSettingsKeys.Token)));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(Constants.JwtTokenExpireTimeInDays),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }