public async Task <IActionResult> Create([FromBody] CredModel model,
                                                 [FromServices] IRequestClient <AuthValidationMQRequest> authValidationReqClient,
                                                 [FromServices] IRequestClient <UserClaimsMQRequest> userClaimsReqClient,
                                                 [FromServices] IGenerateRefreshTokenCommand generateRefreshTokenCommand)
        {
            string correlationId = null;

            try
            {
                if (model == null)
                {
                    return(BadRequest("Something went wrong"));
                }
                correlationId = Guid.NewGuid().ToString("N");

                // Validate username and password.
                Response <AuthValidationMQResponse> authValidationResponse = await authValidationReqClient.GetResponse <AuthValidationMQResponse>(new AuthValidationMQRequest()
                {
                    CorrelationId   = correlationId,
                    UsernameOrEmail = model.Username,
                    Password        = model.Password
                });

                if (authValidationResponse.Message.OpSuccess != true)
                {
                    throw new Exception("Error with authValidationReqClient request");
                }

                if (!authValidationResponse.Message.IsValid)
                {
                    return(BadRequest("Invalid username or password"));
                }

                // Get user and generate user claims.
                Response <UserClaimsMQResponse> claimsResponse = await userClaimsReqClient.GetResponse <UserClaimsMQResponse>(new UserClaimsMQRequest()
                {
                    CorrelationId   = correlationId,
                    AccountId       = authValidationResponse.Message.AccountId,
                    ClaimsConsumers = model.ClaimsConsumers
                });

                if (claimsResponse.Message.OpSuccess != true)
                {
                    throw new Exception("Error with userClaimsReqClient request");
                }

                // Generate refresh token.
                var claims = claimsResponse.Message.GetClaims();

                var req = ControllerContext.HttpContext.Request;
                TokenAdditionalData additionalData = new TokenAdditionalData()
                {
                    DeviceInfo    = "Device info",
                    RequesterIPv4 = "ipv4",
                    RequesterIPv6 = "ipv6"
                };

                TokenResult rTokenResult = await generateRefreshTokenCommand.Execute(authValidationResponse.Message.AccountId, claims, additionalData);

                return(Created("", rTokenResult));
            }
            catch (Exception ex)
            {
                if (model != null)
                {
                    model.Password = "******";
                }

                //Log error
                _logger.LogError("RefreshTokenController.Create", "Exception was thrown", new
                {
                    CorrelationId = correlationId,
                    CredModel     = model,
                    Exception     = ex
                });

                return(BadRequest("Something went wrong"));
            }
        }
        public async Task <IActionResult> Renew([FromBody] RTokenRenewModel renewModel,
                                                [FromServices] IRequestClient <UserClaimsMQRequest> userClaimsReqClient,
                                                [FromServices] IGenerateRefreshTokenCommand generateRefreshTokenCommand,
                                                [FromServices] ITokenExtractor refreshTokenExtractor)
        {
            string correlationId = null;

            try
            {
                if (renewModel == null)
                {
                    return(BadRequest(ValueResponse <TokenResult> .GeneralError("Something went wrong")));
                }
                correlationId = Guid.NewGuid().ToString("N");

                //Old token verification and accountId retrieval goes here
                if (!refreshTokenExtractor.TryExractToken(renewModel.OldRToken, out List <Claim> oldClaims))
                {
                    return(BadRequest(ValueResponse <TokenResult> .GeneralError("Something went wrong")));
                }

                string accountId = oldClaims.Single(x => x.Type == "Account").Value;
                //Problem with dates
                DateTime expiredOld = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).AddSeconds((long.Parse(oldClaims.Single(x => x.Type == "exp").Value)));

                //Get user and generate user claims
                Response <UserClaimsMQResponse> claimsResp = await userClaimsReqClient.GetResponse <UserClaimsMQResponse>(new UserClaimsMQRequest()
                {
                    CorrelationId   = correlationId,
                    AccountId       = accountId,
                    ClaimsConsumers = renewModel.ClaimsConsumers
                });

                if (claimsResp.Message.OpSuccess != true)
                {
                    throw new Exception("Error with userClaimsReqClient request");
                }

                //Generate refresh token
                var newClaims = claimsResp.Message.GetClaims();

                var req = ControllerContext.HttpContext.Request;
                TokenAdditionalData additionalData = new TokenAdditionalData()
                {
                    DeviceInfo    = "Device info",
                    RequesterIPv4 = "ipv4",
                    RequesterIPv6 = "ipv6"
                };

                TokenResult rTokenResult = await generateRefreshTokenCommand.Execute(accountId, newClaims, additionalData, expiredOld);

                return(Created("", rTokenResult));
            }
            catch (Exception ex)
            {
                //Log error
                _logger.LogError("RefreshTokenController.Renew", "Exception was thrown", new
                {
                    CorrelationId   = correlationId,
                    TokenRenewModel = renewModel,
                    Exception       = ex
                });

                return(BadRequest(ValueResponse <TokenResult> .GeneralError("Something went wrong")));
            }
        }