Example #1
0
        public async Task <AccessTokenResponse> LogInAsync(CredentialProxy credentials)
        {
            ValidateCredentials(credentials);

            var user = await _userModel.GetByEmailAsync(credentials.Email);

            if (user == null || !HasPasswordMatch(user.PasswordHash, credentials.Password))
            {
                ThrowHttp.Unauthorized(ErrorMessage.CREDENTIAL_MISMATCH);
            }

            var claim = new Claim(ClaimTypes.Email, user.Email);

            var token = _accessTokenManager.CreateAccessToken(new[] { claim });

            return(new AccessTokenResponse(token));
        }
Example #2
0
        private void Validate(BookProxy book)
        {
            if (book == null)
            {
                ThrowHttp.BadRequest(ErrorMessage.BOOK_REQUIRED);
            }

            if (string.IsNullOrWhiteSpace(book.Title))
            {
                ThrowHttp.BadRequest(ErrorMessage.TITLE_REQUIRED);
            }

            var dateError = _dateValidator.Validate(book.PublishDate);

            if (dateError != null)
            {
                ThrowHttp.BadRequest(dateError);
            }
        }
        private void Validate(AuthorProxy author)
        {
            if (author == null)
            {
                ThrowHttp.BadRequest(ErrorMessage.AUTHOR_REQUIRED);
            }

            if (string.IsNullOrWhiteSpace(author.FirstName))
            {
                ThrowHttp.BadRequest(ErrorMessage.FIRST_NAME_REQUIRED);
            }

            if (string.IsNullOrWhiteSpace(author.LastName))
            {
                ThrowHttp.BadRequest(ErrorMessage.LAST_NAME_REQUIRED);
            }

            if (!author.NobelPrize.HasValue)
            {
                ThrowHttp.BadRequest(ErrorMessage.NOBEL_PRIZE_REQUIRED);
            }

            var dateOfBirthError = _dateValidator.Validate(author.DateOfBirth);

            if (dateOfBirthError != null)
            {
                ThrowHttp.BadRequest(dateOfBirthError);
            }

            if (string.IsNullOrWhiteSpace(author.DateOfDeath))
            {
                return;
            }

            var dateOfDeathError = _dateValidator.Validate(author.DateOfDeath);

            if (dateOfDeathError != null)
            {
                ThrowHttp.BadRequest(dateOfDeathError);
            }
        }