private void CreateAndSetPrincipal(CardSortIdentity cardSortIdentity) { var principal = _authService.CreatePrincipal(cardSortIdentity); Thread.CurrentPrincipal = principal; _currentHttpContext.SetPrincipal(principal); }
public void ShouldNotHaveAnyRolesIfUsernameNull() { var cardSortIdentity = new CardSortIdentity("tester", "JWT", true, "123", null); var results = _classUnderTest.CreatePrincipal(cardSortIdentity); results.Claims.Count(x => x.Type == ClaimTypes.Role).Should().Be(0); }
public void ShouldHaveUserRoleAndAdminRole_WhenUsernameIsAdminUsername() { var identity = new CardSortIdentity(null, null, true, null, "lateknight1"); var results = _classUnderTest.CreatePrincipal(identity); results.Claims.Count(x => x.Type == ClaimTypes.Role).Should().Be(2); results.Claims.Where(x => x.Type == ClaimTypes.Role).Select(c => c.Value).First().Should().Be("user"); results.Claims.Where(x => x.Type == ClaimTypes.Role).Select(c => c.Value).Last().Should().Be("admin"); }
public void ShouldOnlyHaveUserRole_WhenUsernameIsNotAdminUsername() { var identity = new CardSortIdentity(null, null, true, null, "puppyLover15"); var results = _classUnderTest.CreatePrincipal(identity); results.Claims.Count(x => x.Type == ClaimTypes.Role).Should().Be(1); results.Claims.Where(x => x.Type == ClaimTypes.Role).Select(c => c.Value).Should().NotBeNull().And .Equal("user"); }
public GenericPrincipal CreatePrincipal(CardSortIdentity cardSortIdentity) { var roles = new List <string>(); if (string.IsNullOrEmpty(cardSortIdentity.Username)) { return(new GenericPrincipal(cardSortIdentity, roles.ToArray())); } roles.Add("user"); if (cardSortIdentity.Username.Equals("lateknight1")) { roles.Add("admin"); } return(new GenericPrincipal(cardSortIdentity, roles.ToArray())); }
public async Task <CardSortIdentity> GetIdentity(string authHeaderParameter) { var token = ParseJwt(authHeaderParameter); var claims = token.Claims; var claimsList = claims.ToList(); var userId = claimsList.FirstOrDefault(x => x.Type == "nameid")?.Value; var username = claimsList.FirstOrDefault(x => x.Type == "username")?.Value; var user = await _authRepository.GetUser(userId); if (username != user.Username) { return(null); } var identity = new CardSortIdentity(user.Name, "JWT", true, user.UserId.ToString(), user.Username); return(identity); }