Ejemplo n.º 1
0
        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
            });
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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
                );
        }
Ejemplo n.º 4
0
        public WriteModelUserFactoryTests()
        {
            fixture = new Fixture();

            cryptService       = Substitute.For <ICryptService>();
            cryptServiceOutput = fixture.Create <string>();
            cryptService.Crypt(Arg.Any <string>()).Returns(cryptServiceOutput);

            factory = new WriteModelUserFactory(cryptService);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
        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);
        }