public void Build_WhereAddClaimsHasBeenCalled_AssertCorrectClaims() { IClientSecretIdentityBuilder sut = CreateSut(); IEnumerable <Claim> claimCollection = Fixture.CreateMany <Claim>(_random.Next(5, 10)).ToList(); IClientSecretIdentity result = sut.AddClaims(claimCollection).Build(); Assert.That(result.ToClaimsIdentity().Claims.Count(), Is.EqualTo(4 + claimCollection.Count())); foreach (Claim claim in claimCollection) { Assert.That(result.ToClaimsIdentity().Claims.SingleOrDefault(m => string.Compare(m.Type, claim.Type, StringComparison.Ordinal) == 0), Is.Not.Null); } }
public void ToClaimsIdentity_WhenCalled_ReturnsClaimsIdentity() { IClientSecretIdentity sut = CreateSut(); ClaimsIdentity result = sut.ToClaimsIdentity(); Assert.That(result, Is.EqualTo(sut)); }
public void Build_WhereAddClaimsHasNotBeenCalled_AssertDefaultClaims() { IClientSecretIdentityBuilder sut = CreateSut(); IClientSecretIdentity result = sut.Build(); Assert.That(result.ToClaimsIdentity().Claims.Count(), Is.EqualTo(4)); }
public void ToDomain_WhenCalledWithoutClientIdAndClientSecret_ReturnsClientSecretIdentityWithClaimsFromCommand() { IEnumerable <Claim> claims = _fixture.CreateMany <Claim>(_random.Next(5, 10)).ToList();; IClientSecretIdentityCommand sut = CreateSut(claims: claims); IClientSecretIdentity result = sut.ToDomain(); foreach (Claim claim in claims) { Assert.That(result.ToClaimsIdentity().Claims.SingleOrDefault(m => string.CompareOrdinal(claim.ValueType, m.ValueType) == 0 && string.CompareOrdinal(claim.Value, m.Value) == 0), Is.Not.Null); } }
private async Task <ClaimsPrincipal> GetAuthenticatedClaimsPrincipalAsync(string clientId, string clientSecret) { NullGuard.NotNull(clientId, nameof(clientId)) .NotNull(clientSecret, nameof(clientSecret)); IAuthenticateClientSecretCommand command = new AuthenticateClientSecretCommand(clientId, clientSecret); IClientSecretIdentity clientSecretIdentity = await _commandBus.PublishAsync <IAuthenticateClientSecretCommand, IClientSecretIdentity>(command); if (clientSecretIdentity == null) { return(null); } clientSecretIdentity.AddClaims(new[] { ClaimHelper.CreateTokenClaim(clientSecretIdentity.Token, ProtectBase64Token) }); ClaimsIdentity authenticatedClaimsIdentity = new ClaimsIdentity(clientSecretIdentity.ToClaimsIdentity().Claims, Scheme.Name); return(new ClaimsPrincipal(authenticatedClaimsIdentity)); }
public IToken Generate(IClientSecretIdentity clientSecretIdentity) { NullGuard.NotNull(clientSecretIdentity, nameof(clientSecretIdentity)); byte[] key = Encoding.Default.GetBytes(_configuration["Security:JWT:Key"]); DateTime expires = DateTime.UtcNow.AddHours(1); JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor { Subject = clientSecretIdentity.ToClaimsIdentity(), Expires = expires, SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; SecurityToken securityToken = tokenHandler.CreateToken(tokenDescriptor); return(new Token("Bearer", tokenHandler.WriteToken(securityToken), expires)); }
public async Task <IActionResult> UpdateClientSecretIdentity(int identifier) { IEnumerable <Claim> systemClaims = await _queryBus.QueryAsync <EmptyQuery, IEnumerable <Claim> >(new EmptyQuery()); IClientSecretIdentity clientSecretIdentity = await _queryBus.QueryAsync <IGetClientSecretIdentityQuery, IClientSecretIdentity>(new GetClientSecretIdentityQuery { Identifier = identifier }); if (clientSecretIdentity == null) { return(RedirectToAction("ClientSecretIdentities", "Security")); } ClientSecretIdentityViewModel clientSecretIdentityViewModel = _securityViewModelConverter.Convert <IClientSecretIdentity, ClientSecretIdentityViewModel>(clientSecretIdentity); clientSecretIdentityViewModel.Claims = BuildClaimViewModelCollection(systemClaims, clientSecretIdentity.ToClaimsIdentity().Claims); clientSecretIdentityViewModel.EditMode = EditMode.Edit; return(View("UpdateClientSecretIdentity", clientSecretIdentityViewModel)); }