コード例 #1
0
        public async Task HandleAsync(SignIn command)
        {
            var user = await _userRepository.GetByNameAsync(command.Name);

            if (user is null || !_passwordService.IsValid(user.Password, command.Password))
            {
                _logger.LogError($"User with name: {command.Name} was not found.");
                throw new InvalidCredentialsException(command.Name);
            }

            if (user.Locked)
            {
                throw new UserLockedException(user.Id);
            }

            var claims = user.Permissions.Any()
                ? new Dictionary <string, IEnumerable <string> >
            {
                ["permissions"] = user.Permissions
            }
                : null;
            var auth = _jwtProvider.Create(user.Id, user.Name, user.Role, claims: claims);

            auth.RefreshToken = await CreateRefreshTokenAsync(user.Id);

            _storage.Set(command.Id, auth);
            _logger.LogInformation($"User with ID: {user.Id} has been authenticated.");
            await _messageBroker.PublishAsync(new SignedIn(user.Id));
        }
コード例 #2
0
        public async Task HandleAsync(UseRefreshToken command)
        {
            var token = await _refreshTokenRepository.GetAsync(command.RefreshToken);

            if (token is null)
            {
                throw new InvalidRefreshTokenException();
            }

            if (token.Revoked)
            {
                throw new RevokedRefreshTokenException();
            }

            var user = await _userRepository.GetAsync(token.UserId);

            if (user is null)
            {
                throw new UserNotFoundException(token.UserId);
            }

            var claims = user.Permissions.Any()
                ? new Dictionary <string, IEnumerable <string> >
            {
                ["permissions"] = user.Permissions
            }
                : null;
            var auth = _jwtProvider.Create(token.UserId, user.Name, user.Role, claims: claims);

            auth.RefreshToken = command.RefreshToken;
            _storage.Set(command.Id, auth);
        }