Esempio n. 1
0
        public async Task <SessionDto> RegisterAuthorAsync(AuthorRegisteringDto dto)
        {
            var existingUser = await this.CheckIfExists(u => u.Nickname == dto.Nickname).ConfigureAwait(false);

            if (existingUser)
            {
                throw new DuplicateUserNicknameException("Nickname is already taken.");
            }

            string salt = await HashingService.GetSaltAsync().ConfigureAwait(false);

            string pass = await HashingService.GetHashAsync(dto.Password, salt, HashingType.Strong).ConfigureAwait(false);

            User user = new User()
            {
                Nickname        = dto.Nickname,
                Password        = pass,
                Salt            = salt,
                HaikusRatingSum = 0.0,
                HaikusCount     = 0
            };

            this.UnitOfWork.UsersRepository.Add(user);

            var session = await this.SessionsService.AddNewSessionAsync(user.Nickname, user.Salt).ConfigureAwait(false);

            await this.UnitOfWork.CommitAsync().ConfigureAwait(false);

            return(session);
        }
Esempio n. 2
0
        public async Task <SessionDto> AddNewSessionAsync(string nickname, string salt)
        {
            string token     = SessionsService.GenerateNewSessionToken();
            string tokenHash = await HashingService.GetHashAsync(token, salt, HashingType.Weak).ConfigureAwait(false);

            this.UnitOfWork.SessionsRepository.Add(new Session()
            {
                Token    = tokenHash,
                Nickname = nickname,
                From     = DateTime.Now
            });

            return(new SessionDto()
            {
                Nickname = nickname,
                Token = token
            });
        }
Esempio n. 3
0
        public async Task <SessionDto> LoginAsync(AuthorLoginDto dto)
        {
            var user = await this.UnitOfWork.UsersRepository
                       .GetUniqueAsync(u => u.Nickname == dto.Nickname).ConfigureAwait(false);

            if (user == null)
            {
                throw new NotFoundException("Wrong nickname or password.");
            }

            string pass = await HashingService.GetHashAsync(dto.Password, user.Salt, HashingType.Strong);

            if (pass != user.Password)
            {
                throw new NotFoundException("Wrong nickname or password.");
            }

            var session = await this.SessionsService.AddNewSessionAsync(user.Nickname, user.Salt).ConfigureAwait(false);

            await this.UnitOfWork.CommitAsync().ConfigureAwait(false);

            return(session);
        }
Esempio n. 4
0
        public async Task <SessionDto> CheckAndUpdateSessionAsync(SessionDto session)
        {
            var user = await this.UnitOfWork.UsersRepository.GetUniqueAsync(e => e.Nickname == session.Nickname).ConfigureAwait(false);

            if (user == null)
            {
                throw new SessionException("You should re-enter your credentials.");
            }

            var tokenHash = await HashingService.GetHashAsync(session.Token, user.Salt, HashingType.Weak).ConfigureAwait(false);

            var sessionObj = await this.UnitOfWork.SessionsRepository.GetUniqueAsync(e => e.Token == tokenHash).ConfigureAwait(false);

            if (sessionObj == null)
            {
                throw new SessionException("You should re-enter your credentials.");
            }

            if (sessionObj.Nickname != session.Nickname)
            {
                throw new SessionException("You should re-enter your credentials.");
            }

            string newToken = SessionsService.GenerateNewSessionToken();

            sessionObj.Token = await HashingService.GetHashAsync(newToken, user.Salt, HashingType.Weak).ConfigureAwait(false);

            sessionObj.From = DateTime.Now;
            await this.UnitOfWork.CommitAsync().ConfigureAwait(false);

            return(new SessionDto
            {
                Nickname = session.Nickname,
                Token = newToken
            });
        }