Example #1
0
        private UserException TryAuthenticate(string accessToken, out ClaimsIdentity identity)
        {
            JwtTokenPayload payload;

            identity = null;
            try
            {
                payload = _jwtDecoder.DecodeToObject <JwtTokenPayload>(accessToken, _options.JwtSecret, false);
            }
            catch (Exception ex)
            {
                _logger.Warning(ex, $"Failed to parse authentication token: {ex.Message}");
                return(UserException.Create(AuthenticationError.BadToken, "Invalid authentication token."));
            }
            if (payload.ExpiresAt < DateTime.UtcNow)
            {
                return(UserException.Create(AuthenticationError.ExpiredToken, "Authentication token is expired."));
            }
            var parsed = Guid.TryParse(payload.Subject, out Guid userId);

            if (!parsed)
            {
                return(UserException.Create(AuthenticationError.BadToken, "Invalid subject."));
            }
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, userId.ToString())
            };

            identity = new ClaimsIdentity(claims);
            return(null);
        }
Example #2
0
        private void EnsureTurnIsInProgress(Guid userId)
        {
            var state = _state.PlayerStates.GetOrCreateDefault(userId);

            if (state.TurnStatus == TurnStatus.Ended)
            {
                throw UserException.Create(GameplayError.TurnIsAlreadyEnded, "You have already ended your turn.");
            }
        }
Example #3
0
        private void EnsureGameIsNotStarted()
        {
            if (!_state.IsGameStarted)
            {
                return;
            }
            var message = $"Game {_state.RoomId} is already in progress.";

            throw UserException.Create(GameplayError.GameIsAlreadyInProgress, message);
        }
Example #4
0
        public async Task <IGame> StartGame(Guid userId)
        {
            EnsureUserIsOnline(userId);
            EnsureGameIsNotStarted();

            if (_state.Players.Count < 2)
            {
                throw UserException.Create(
                          GameplayError.NotEnoughPlayers,
                          "At least 2 players are required to start the game.");
            }

            _state.IsGameStarted = true;
            var game = await _gameFactory.CreateGame(userId);

            return(game);
        }
Example #5
0
        public static void EnsureIsValid <T>(this T value, IValidator <T> validator)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (validator == null)
            {
                throw new ArgumentNullException(nameof(validator));
            }
            var results = validator.Validate(value);

            if (results.IsValid)
            {
                return;
            }
            var messages = results.Errors.Select(x => $"[{x.PropertyName}:{x.ErrorCode}] {x.ErrorMessage}");
            var message  = string.Join(Environment.NewLine, messages);

            throw UserException.Create(ValidationError.BadInput, results.ToConreignValidationErrorDetails(), message);
        }