/// <summary>
        /// Generate auth token for the user
        /// use Password to encrypt the new generated token
        /// </summary>
        /// <param name="authTokenRequest">phone , password</param>
        /// <returns> return AuthTokenResponse(validation errors + token string)</returns>
        public AuthTokenResponse GenerateAuthToken(AuthTokenRequest authTokenRequest)
        {
            var result = new AuthTokenResponse();

            var error = ValidateRequest(authTokenRequest);

            if (error.HasErrors)
            {
                result.Errors = error;
                return(result);
            }

            var createdAt = DateTime.UtcNow;

            var authTokenRow = new DTO.AuthToken();

            authTokenRow.Password = authTokenRequest.Password;
            authTokenRow.Phone    = authTokenRequest.Phone;
            authTokenRow.ExpireAt = createdAt.AddHours(1);
            authTokenRow.Token    = GenerateToken(authTokenRequest.Password, createdAt);

            _authTokenRepository.Add(authTokenRow);

            result.Token = authTokenRow.Token;
            return(result);
        }
        public async Task AuthToken_Verify_Success()
        {
            //arrange
            _authTokenAPI = new AuthTokenController(new AuthTokenService(_authTokenRepositoryMock.Object, _personsRepositoryMock.Object))
            {
                Request       = new System.Net.Http.HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            var authTokenRequest = new VerifyAuthTokenRequest()
            {
                Phone = "123456789123",
                Token = "GNSaYTxnLWWlyeyOL9RCUiYI4MRm++18mTkIwTMHIpwfZSygAAUovW/fkVV+saGe0+XfMKzEKjtpdluNSjUlZ6TUQllSvh32zNHZRCbyBpglvcJwJKJqjH+wV95lpvPpYXj+SotD8vflPzAVD7x0q3NxxK50TJhUOYQm6VblWR4="
            };

            var authTokenRow = new DTO.AuthToken()
            {
                Phone    = "123456789123",
                Token    = "GNSaYTxnLWWlyeyOL9RCUiYI4MRm++18mTkIwTMHIpwfZSygAAUovW/fkVV+saGe0+XfMKzEKjtpdluNSjUlZ6TUQllSvh32zNHZRCbyBpglvcJwJKJqjH+wV95lpvPpYXj+SotD8vflPzAVD7x0q3NxxK50TJhUOYQm6VblWR4=",
                ExpireAt = DateTime.UtcNow.AddMinutes(10)
            };

            _personsRepositoryMock.Setup(x => x.IsPhoneExists(It.IsAny <string>())).Returns(true);
            _authTokenRepositoryMock.Setup(x => x.Get(It.IsAny <string>(), It.IsAny <string>())).Returns(authTokenRow);

            //act
            var response = await _authTokenAPI.VerifyAsync(authTokenRequest);

            var responseBody = await Task.FromResult(response.Content.ReadAsStringAsync()).Result;

            //assert
            Assert.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode);
            Assert.AreEqual(responseBody, "\"valid\"");
        }
        public int Add(DTO.AuthToken token)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <DTO.AuthToken, Data.AuthToken>();
            });

            IMapper iMapper = config.CreateMapper();

            var dataItem = iMapper.Map <DTO.AuthToken, Data.AuthToken>(token);

            this._dbcontext.AuthTokens.Add(dataItem);
            this._dbcontext.SaveChanges();

            return(dataItem.Id);
        }