Example #1
0
        public void Verify_GetIpAddress_Should_Return_IpAddress()
        {
            // Arrange
            Mock <HttpContext> httpContext = new HttpContextMock().HttpContext;

            // Act
            var ipAddress = HttpContextHelpers.GetIpAddress(httpContext.Object);

            // Assert
            ipAddress.Should().Be("127.0.0.1");
        }
Example #2
0
        public async Task Invoke(
            HttpContext httpContext,
            IAuthenticatedUserService authenticatedUserService,
            IUnitOfWork unitOfWork)
        {
            string requestMethod      = default;
            string requestPath        = default;
            string requestIpAddress   = default;
            string requestUserId      = default;
            int    responseStatusCode = default;

            if (httpContext.Request != default)
            {
                requestMethod    = httpContext.Request.Method;
                requestPath      = httpContext.Request.Path.Value;
                requestIpAddress = HttpContextHelpers.GetIpAddress(httpContext.Request.HttpContext);
                requestUserId    = authenticatedUserService.IsValid ? authenticatedUserService.Id : default;
            }

            await this._next(httpContext);

            if (httpContext.Request != default && httpContext.Response != default)
            {
                responseStatusCode = httpContext.Response.StatusCode;

                var log = new ActivityLog
                {
                    Method             = requestMethod,
                    Path               = requestPath,
                    IpAddress          = requestIpAddress,
                    UserId             = requestUserId,
                    ResponseStatusCode = responseStatusCode,
                    Timestamp          = DateTime.UtcNow,
                };

                unitOfWork.ActivityLogs.Add(log);

                if (unitOfWork.Finish() == 0)
                {
                    throw new Exception("Could not save the activity log into the database.");
                }
            }
        }
Example #3
0
        public async Task <ActionResult <IResponse <string> > > RevokeRefreshToken(RevokeRefreshTokenDto dto)
        {
            IResponse response;

            var command = this._mapper.Map <RevokeRefreshTokenCommand>(dto);

            command.IpAddress = HttpContextHelpers.GetIpAddress(this.Request.HttpContext);

            response = await this._mediator.Send(command);

            if (response == default)
            {
                return(NotFound(this._responseFactory.Create(message: ResponseMessages.RefreshTokenNotFound)));
            }

            if (!response.Success)
            {
                return(BadRequest(response));
            }

            return(Ok(response));
        }
Example #4
0
        public async Task <ActionResult <IResponse <AccessTokenDto> > > RefreshAccessToken(RefreshAccessTokenDto dto)
        {
            IResponse <AccessTokenDto> response;

            var command = this._mapper.Map <RefreshAccessTokenCommand>(dto);

            command.IpAddress = HttpContextHelpers.GetIpAddress(this.Request.HttpContext);

            response = await this._mediator.Send(command);

            if (response == default)
            {
                return(NotFound(this._responseFactory.Create <AccessTokenDto>(message: ResponseMessages.UserNotRegistered)));
            }

            if (!response.Success)
            {
                return(BadRequest(response));
            }

            this.Response.Headers.Append("refresh-token", response.Data.RefreshToken);

            return(Ok(response));
        }