private InitiateAuthRequest CreateRefreshTokenAuthRequest(AuthFlowType authFlowType)
        {
            EnsureUserAuthenticated();

            if (authFlowType != AuthFlowType.REFRESH_TOKEN && authFlowType != AuthFlowType.REFRESH_TOKEN_AUTH)
            {
                throw new ArgumentException("authFlowType must be either \"REFRESH_TOKEN\" or \"REFRESH_TOKEN_AUTH\"", "authFlowType");
            }

            InitiateAuthRequest initiateAuthRequest = new InitiateAuthRequest()
            {
                AuthFlow       = authFlowType,
                ClientId       = ClientID,
                AuthParameters = new Dictionary <string, string>()
                {
                    { CognitoConstants.ChlgParamUsername, Username },
                    { CognitoConstants.ChlgParamRefreshToken, SessionTokens.RefreshToken }
                }
            };

            if (Device != null && !string.IsNullOrEmpty(Device.DeviceKey))
            {
                initiateAuthRequest.AuthParameters.Add(CognitoConstants.ChlgParamDeviceKey, Device.DeviceKey);
            }

            if (!string.IsNullOrEmpty(SecretHash))
            {
                initiateAuthRequest.AuthParameters.Add(CognitoConstants.ChlgParamSecretHash, SecretHash);
            }

            return(initiateAuthRequest);
        }
        private async Task <SignInResponse> SignIn(AuthFlowType type, SignInRequestBase request,
                                                   Dictionary <string, string> authParameters = null, CancellationToken cancellationToken = default)
        {
            Check.NotNull(request, nameof(request));

            var authRequest = new InitiateAuthRequest()
            {
                ClientId        = clientId,
                AuthFlow        = type,
                UserContextData = new UserContextDataType
                {
                    EncodedData =
                        $"IP:{request.IpAddress};ServerPath:{request.ServerPath};ServerName:{request.ServerName}"
                }
            };

            authRequest.AuthParameters.Add("USERNAME", request.UserName.ToLower());

            if (authParameters != null)
            {
                foreach (var(key, value) in authParameters)
                {
                    authRequest.AuthParameters.Add(key, value);
                }
            }

            try
            {
                var response = await base.CreateDefaultRetryAsyncPolicy().ExecuteAsync(async() =>
                                                                                       await CognitoidentityProvider.InitiateAuthAsync(authRequest, cancellationToken));

                if (response.AuthenticationResult != null)
                {
                    return new SignInResponse
                           {
                               IdToken      = response.AuthenticationResult.IdToken,
                               AccessToken  = response.AuthenticationResult.AccessToken,
                               ExpiresIn    = response.AuthenticationResult.ExpiresIn,
                               TokenType    = response.AuthenticationResult.TokenType,
                               RefreshToken = response.AuthenticationResult.RefreshToken,
                               SignInType   = "USER_PASSWORD_AUTH"
                           }
                }
                ;

                return(new SignInResponse
                {
                    Session = response.Session,
                    ChallengeName = response.ChallengeName.Value,
                    ChallengeParameters = response.ChallengeParameters
                });
            }
            catch (NotAuthorizedException)
            {
                throw new BusinessException(ErrorCode.NotAuthorized);
            }
            catch (Exception ex)
            {
                throw CatchException(ex);
            }
        }