private async Task <TokenAndClaimsPrincipal> CreateShortTokenAsync( User user, string lat2r, string lat2, long sessionId) { List <Claim> claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(TokenClaimNames.LongToken2Ran, lat2r), new Claim(JwtRegisteredClaimNames.Jti, CryptoRandomizer.GetRandomString(DbColumnSizes.BlackListShortToken_TokenId)) }; var roleNames = await userManager.GetRolesAsync(user); foreach (var role in roleNames) { claims.Add(new Claim(ClaimTypes.Role, role)); } var expiration = DateTime.UtcNow.AddMinutes(jwtOptions.ShortTokenLiveTimeMinutes); var token = new JwtSecurityToken( claims: claims.ToArray(), expires: expiration); var claimsIdentity = new ClaimsIdentity(claims, "JwtShortToken"); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); return(new TokenAndClaimsPrincipal { ClaimsPrincipal = new SunClaimsPrincipal(claimsPrincipal, rolesCache, sessionId, lat2), Token = cryptService.Crypt(CipherSecrets.ShortJwt, token.Payload.SerializeToJson()), Expiration = expiration }); }
public MongoDbWriteModelUnitOfWorkTests() { fixture = new Fixture(); //Setup mongo configuration configuration = fixture.Create <MongoDbConfiguration>(); //Setup client factory client = Substitute.For <IMongoClient>(); sessionHandle = Substitute.For <IClientSessionHandle>(); database = Substitute.For <IMongoDatabase>(); clientFactory = Substitute.For <IMongoClientFactory>(); client.StartSession().Returns(sessionHandle); client.GetDatabase(Arg.Any <string>()).Returns(database); clientFactory.CreateClient(Arg.Any <MongoDbConfiguration>()).Returns(client); //Setup user factory cryptService = Substitute.For <ICryptService>(); cryptService.Crypt(Arg.Any <string>()).Returns(args => args[0]); userFactory = new WriteModelUserFactory(cryptService); //Create unit of work unitOfWork = new MongoDbWriteModelUnitOfWork(configuration, clientFactory, userFactory); }
public SubscribeHandlerTests() { fixture = new Fixture(); cryptService = Substitute.For <ICryptService>(); cryptServiceOutput = fixture.Create <string>(); cryptService.Crypt(Arg.Any <string>()).Returns(cryptServiceOutput); userFactory = new WriteModelUserFactory(cryptService); userReadRepository = Substitute.For <IReadModelUserRepository>(); readModelUnitOfWork = Substitute.For <IReadModelUnitOfWork>(); readModelUnitOfWork.UserRepository.Returns(userReadRepository); userWriteRepository = Substitute.For <IWriteModelUserRepository>(); writeModelUnitOfWork = Substitute.For <IWriteModelUnitOfWork>(); writeModelUnitOfWork.UserRepository.Returns(userWriteRepository); eventPublisher = Substitute.For <IEventPublisher>(); handler = new SubscribeHandler( userFactory, readModelUnitOfWork, writeModelUnitOfWork, eventPublisher ); }
public WriteModelUserFactoryTests() { fixture = new Fixture(); cryptService = Substitute.For <ICryptService>(); cryptServiceOutput = fixture.Create <string>(); cryptService.Crypt(Arg.Any <string>()).Returns(cryptServiceOutput); factory = new WriteModelUserFactory(cryptService); }
public InMemoryUserRepositoryTests() { fixture = new Fixture(); cryptService = Substitute.For <ICryptService>(); cryptService.Crypt(Arg.Any <string>()).Returns(args => args[0]); userFactory = new WriteModelUserFactory(cryptService); data = new InMemoryUserWriteData(); repository = new InMemoryWriteModelUserRepository(userFactory, data); }
public MongoDbWriteModelUserRepositoryTests() { fixture = new Fixture(); fixture.Customize <UserSubscribed>(composer => composer.With(u => u.Email, "*****@*****.**")); database = Substitute.For <IMongoDatabase>(); userCollection = Substitute.For <IMongoCollection <User> >(); database.GetCollection <User>(Arg.Is(MongoDbWriteModelUserRepository.collectionName)).Returns(userCollection); cryptService = Substitute.For <ICryptService>(); cryptService.Crypt(Arg.Any <string>()).Returns(args => args[0]); userFactory = new WriteModelUserFactory(cryptService); repository = new MongoDbWriteModelUserRepository(database, userFactory); }
public virtual string GenerateChangeEmailToken(User user, string email) { List <Claim> claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(JwtRegisteredClaimNames.Email, email), new Claim(JwtRegisteredClaimNames.Jti, CryptoRandomizer.GetRandomString(16)) }; var token = new JwtSecurityToken( claims: claims.ToArray(), expires: DateTime.UtcNow.AddDays(3)); cryptService.Crypt(CipherSecrets.EmailChange, token.Payload.SerializeToJson()); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public AuthenticateWithCredentialsHandlerTests() { fixture = new Fixture(); readModel = Substitute.For <IReadModelUnitOfWork>(); readModelUserRepository = Substitute.For <IReadModelUserRepository>(); readModel.UserRepository.Returns(readModelUserRepository); writeModel = Substitute.For <IWriteModelUnitOfWork>(); writeModelUserRepository = Substitute.For <IWriteModelUserRepository>(); writeModel.UserRepository.Returns(writeModelUserRepository); cryptService = Substitute.For <ICryptService>(); cryptService.Crypt(Arg.Any <string>()).Returns(args => args[0]); writeModelUserFactory = new WriteModelUserFactory(cryptService); handler = new AuthenticateWithCredentialsHandler(readModel, writeModel, cryptService); }