Exemplo n.º 1
0
        public string GetActivity()
        {
            try
            {
                var queryStringValue = Request.Query;
                var userModel        = (User)RouteData.Values["UserModel"];

                if (!queryStringValue.ContainsKey("offset") || !queryStringValue.ContainsKey("limit"))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                StringValues sort;
                StringValues filter;
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_OFFSET, out var offset);
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_LIMIT, out var limit);
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER, out filter);
                }
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT, out sort);
                }
                sort = ConvertSortLog(sort);

                if (userModel != null)
                {
                    int numberData;
                    var resultLogs = _userBusiness.GetActionLog(out numberData, userModel.Id, Convert.ToInt32(offset),
                                                                Convert.ToInt32(limit), filter.ToString(), sort);
                    if (resultLogs.Status != Status.STATUS_SUCCESS)
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.DATA_NOT_FOUND));
                    }

                    var listLogs = JsonHelper.DeserializeObject <List <UserActionLog> >(resultLogs.Data);
                    return(new ReturnObject
                    {
                        Status = Status.STATUS_SUCCESS,
                        Data = new ResultList <UserActionLog>
                        {
                            List = listLogs,
                            Total = numberData
                        }.ToJson()
                    }.ToJson());
                }

                return(HelpersApi.CreateDataError(MessageApiError.DATA_NOT_FOUND));
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.LOG_LIST + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 2
0
 public string DeleteUserActivityById([FromBody] JObject value)
 {
     try
     {
         return(value.ContainsKey("Id")
             ? _userBusiness.DeleteActivityById(value["Id"].ToString()).ToJson()
             : HelpersApi.CreateDataError("ID Not exist."));
     }
     catch (Exception e)
     {
         _logger.Error(KeyLogger.LOG_DELETE + e);
         return(HelpersApi.CreateDataError(e.Message));
     }
 }
Exemplo n.º 3
0
        public string UpdateCloseAccount([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (
                    !value.ContainsKey(ParseDataKeyApi.KEY_SECURITY_UPDATE_CLOSE_ACCOUNT_STATUS) ||
                    !value.ContainsKey(ParseDataKeyApi.KEY_SECURITY_UPDATE_CLOSE_ACCOUNT_PASSWORD))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID + 1));
                }

                var code = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_SECURITY_UPDATE_CLOSE_ACCOUNT_CODE))
                {
                    code = value[ParseDataKeyApi.KEY_SECURITY_UPDATE_CLOSE_ACCOUNT_CODE].ToString();
                }

                var status = value[ParseDataKeyApi.KEY_SECURITY_UPDATE_CLOSE_ACCOUNT_STATUS];

                if (!int.TryParse((string)status, out var outStatus))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID + 2));
                }

                var password = value[ParseDataKeyApi.KEY_SECURITY_UPDATE_CLOSE_ACCOUNT_PASSWORD].ToString();

                if (!HelpersApi.ValidateSecondPass(password))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID + 3));
                }

                if (userModel.IsLockScreen != 0)
                {
                    if (!CommonHelper.Md5(password).Equals(userModel.SecondPassword))
                    {
                        return new ReturnObject
                               {
                                   Status  = Status.STATUS_ERROR,
                                   Message = "Pass is invalid"
                               }
                    }
                    .ToJson();
                }
Exemplo n.º 4
0
        public string GetInfo()
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                return(new ReturnObject
                {
                    Status = Status.STATUS_SUCCESS,
                    Data = JsonHelper.SerializeObject(new SecurityModel
                    {
                        TwofaOption = userModel.Verification,
                        IsEnableTwofa = userModel.IsTwoFactor
                    })
                }.ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.SECURITY_GET_INFO + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 5
0
        public string GetInfo()
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (userModel != null)
                {
                    return(new ReturnObject
                    {
                        Status = Status.STATUS_SUCCESS,
                        Data = JsonConvert.SerializeObject(new InfoApi())
                    }.ToJson());
                }

                return(HelpersApi.CreateDataError(MessageApiError.DATA_NOT_FOUND));
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.API_ACCESS_GET_INFO + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 6
0
        public string UpdateLabel([FromBody] JObject value)
        {
            try
            {
                if (!value.ContainsKey("id") || !value.ContainsKey("networkName"))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var id          = value["id"].ToString();
                var networkName = value["networkName"].ToString();

                if (!HelpersApi.ValidateCurrency(networkName))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                if (!CommonHelper.ValidateId(id))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                if (value.ContainsKey("label"))
                {
                    var label  = value["label"].ToString();
                    var result = _walletBusiness.UpdateAddress(id, networkName, label);

                    if (result.Status == Status.STATUS_SUCCESS)
                    {
                        return new ReturnObject
                               {
                                   Status = Status.STATUS_SUCCESS
                               }
                    }
                    .ToJson();
                }
Exemplo n.º 7
0
        public string GetAddresses()
        {
            try
            {
                var queryStringValue = Request.Query;


                if (!queryStringValue.ContainsKey("offset") || !queryStringValue.ContainsKey("networkName") ||
                    !queryStringValue.ContainsKey("limit"))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                StringValues sort;
                StringValues filter;
                StringValues networkName;
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_OFFSET, out var offset);
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_LIMIT, out var limit);
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER, out filter);
                }
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT, out sort);
                }

                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_NETWORK, out networkName);
                //sort = ConvertSortLog(sort);

                if (!HelpersApi.ValidateCurrency(networkName))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var userModel = (User)RouteData.Values["UserModel"];

                var wallet = _walletBusiness.FindByUserAndNetwork(userModel.Id, networkName);
                int numberData;
                var listAddresses = _walletBusiness.GetAddressesFull(out numberData, wallet.Id, networkName,
                                                                     Convert.ToInt32(offset),
                                                                     Convert.ToInt32(limit), filter.ToString(), sort);

                return(new ReturnObject()
                {
                    Status = Status.STATUS_COMPLETED,
                    Data = new ResultList <BlockchainAddress>
                    {
                        List = listAddresses,
                        Total = numberData
                    }.ToJson()
                }.ToJson());
            }
            catch (Exception e)
            {
                return(new ReturnObject()
                {
                    Status = Status.STATUS_ERROR,
                    Message = e.Message
                }.ToJson());
            }
        }
Exemplo n.º 8
0
        public string GetListApiAccess()
        {
            try
            {
                var queryStringValue = Request.Query;


                if (!queryStringValue.ContainsKey("offset") || !queryStringValue.ContainsKey("limit"))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                StringValues sort = "-updatedat";
                StringValues filter;
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_OFFSET, out var offset);
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_LIMIT, out var limit);
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER, out filter);
                }
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT, out sort);
                }

                sort = ConvertSort(sort);


                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];
                int numberData;
                var dataApiKeys =
                    _userBusiness.GetApiKeys(out numberData, userModel.Id, Convert.ToInt32(offset),
                                             Convert.ToInt32(limit), filter.ToString(), sort);

                if (dataApiKeys.Status != Status.STATUS_SUCCESS)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.DATA_NOT_FOUND));
                }
                var listApiKeys = JsonHelper.DeserializeObject <List <ResultApiAccess> >(dataApiKeys.Data);
                if (listApiKeys.Count <= 0)
                {
                    return new ReturnObject
                           {
                               Status = Status.STATUS_SUCCESS,
                               Data   = JsonHelper.SerializeObject(listApiKeys)
                           }
                }
                .ToJson();
                foreach (var listApiKey in listApiKeys)
                {
                    listApiKey.KeyApi = listApiKey.KeyApi.Substring(0, 10) + "...";
                }

                return(new ReturnObject
                {
                    Status = Status.STATUS_SUCCESS,
                    Data = new ResultList <ResultApiAccess>
                    {
                        List = listApiKeys,
                        Total = numberData
                    }.ToJson()
                }.ToJson());
            }
Exemplo n.º 9
0
        public string VerifyCodeTransaction([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];


                var code   = "";
                var codeGG = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_TRANSACTION_SMS))
                {
                    code = value[ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_TRANSACTION_SMS].ToString();
                }
                if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_TRANSACTION_2FA))
                {
                    codeGG = value[ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_TRANSACTION_2FA].ToString();
                }

                bool isVerify = false;

                switch (userModel.IsTwoFactor)
                {
                case 1:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_TRANSACTION_SMS))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, codeGG);
                    break;

                case 2:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_TRANSACTION_SMS))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    var secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);
                    if (string.IsNullOrEmpty(secretAuthToken.SendTransaction))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                    }

                    isVerify = HelpersApi.CheckCodeSms(secretAuthToken.SendTransaction, code, userModel);
                    break;

                case 0:
                    isVerify = true;
                    break;
                }

                if (!isVerify)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                }

                // userModel.Verification = (int) option;

                // su ly data gui len
                //to do

                var request = value.ToObject <SendTransaction>();

                var userRequest = new UserSendTransaction()
                {
                    UserId = userModel.Id,
                    Type   = "send",
                    To     = request.Detail.SendByAd
                        ? request.Detail.RecipientWalletAddress
                        : request.Detail.RecipientEmailAddress,
                    SendByBlockchainAddress = request.Detail.SendByAd,
                    Amount       = request.Detail.VkcAmount,
                    PricePerCoin = request.Detail.PricePerCoin,
                    Currency     = request.NetworkName,
                    Description  = request.Detail.VkcNote,
                };
                ReturnObject result = null;
                result = AddSendTransaction(userRequest);

                return(JsonHelper.SerializeObject(result));
            }
            catch (Exception e)
            {
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 10
0
        public string GetConfirmedDevices()
        {
            try
            {
                var queryStringValue = Request.Query;

                if (!queryStringValue.ContainsKey("offset") || !queryStringValue.ContainsKey("limit"))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                StringValues sort;
                StringValues filter;
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_OFFSET, out var offset);
                queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_LIMIT, out var limit);
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_FILTER, out filter);
                }
                if (queryStringValue.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT))
                {
                    queryStringValue.TryGetValue(ParseDataKeyApi.KEY_PASS_DATA_GET_SORT, out sort);
                }

                sort = ConvertSortDevice(sort);

                var ip = HelpersApi.GetIp(Request);

                var checkConfirmedDevices = new ConfirmedDevices();

                if (!string.IsNullOrEmpty(ip))
                {
                    var browser = HelpersApi.GetBrowser(Request);

                    var search = new Dictionary <string, string> {
                        { "Ip", ip }, { "Browser", browser }
                    };

                    //save web session
                    checkConfirmedDevices = _userBusiness.GetConfirmedDevices(search);
                }

                var userModel = (User)RouteData.Values["UserModel"];

                int numberData;
                var resultDevice = _userBusiness.GetListConfirmedDevices(out numberData, userModel.Id,
                                                                         checkConfirmedDevices,
                                                                         Convert.ToInt32(offset),
                                                                         Convert.ToInt32(limit), sort, filter);

                if (resultDevice.Status != Status.STATUS_SUCCESS)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.DATA_NOT_FOUND));
                }

                var listDevice = JsonHelper.DeserializeObject <List <ConfirmedDevices> >(resultDevice.Data);
                return(new ReturnObject
                {
                    Status = Status.STATUS_SUCCESS,
                    Data = new ResultList <ConfirmedDevices>
                    {
                        List = listDevice,
                        Total = numberData
                    }.ToJson()
                }.ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.DEVICE_LIST + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 11
0
        public string UpdateOption([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var code = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION_CODE))
                {
                    code = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION_CODE].ToString();
                }


                bool isVerify = false;

                switch (userModel.IsTwoFactor)
                {
                case 1:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    break;

                case 2:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    var secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);
                    if (string.IsNullOrEmpty(secretAuthToken.UpdateOptionVerification))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                    }

                    isVerify = HelpersApi.CheckCodeSms(secretAuthToken.UpdateOptionVerification, code, userModel);
                    break;

                case 0:
                    isVerify = true;
                    break;
                }


                if (!isVerify)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                }

                var option = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION];

                userModel.Verification = (int)option;

                _userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                           ActionLog.UPDATE_OPTION_VETIFY,
                                           HelpersApi.GetIp(Request));

                return(_userBusiness.UpdateProfile(userModel).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.TWOFA_OPTION_UPDATE + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 12
0
        public string SendCode([FromBody] JObject value)
        {
            try
            {
                if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_SEND_CODE_ACTION))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];


                var code = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                {
                    code = value[ParseDataKeyApi.KEY_PASS_DATA_GET_CODE].ToString();
                }

                if (userModel.IsTwoFactor == 1)
                {
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    if (!HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }
                }


                var    action = value[ParseDataKeyApi.KEY_TWO_FA_SEND_CODE_ACTION].ToString();
                string secret;
                var    time = 30;

                switch (action)
                {
                case ActionLog.TWOFA_ENABLE:
                    secret = ActionLog.TWOFA_ENABLE;
                    break;

                case ActionLog.CUSTOM_TWOFA:
                    secret = ActionLog.CUSTOM_TWOFA;
                    break;

                case ActionLog.AVATAR:
                    secret = ActionLog.AVATAR;
                    break;

                case ActionLog.UPDATE_PREFERENCES:
                    secret = ActionLog.UPDATE_PREFERENCES;
                    break;

                case ActionLog.UPDATE_OPTION_VETIFY:
                    secret = ActionLog.UPDATE_OPTION_VETIFY;
                    break;

                case ActionLog.UPDATE_PROFILE:
                    secret = ActionLog.UPDATE_PROFILE;
                    break;

                case ActionLog.TWOFA_DISABLE:
                    secret = ActionLog.TWOFA_DISABLE;
                    break;

                case ActionLog.LOCK_SCREEN:
                    secret = ActionLog.LOCK_SCREEN;
                    break;

                case ActionLog.SEND_TRANSACTION:
                    secret = ActionLog.SEND_TRANSACTION;
                    break;

                case ActionLog.API_ACCESS_ADD:
                    secret = ActionLog.API_ACCESS_ADD;
                    time   = 120;
                    break;

                case ActionLog.API_ACCESS_EDIT:
                    secret = ActionLog.API_ACCESS_EDIT;
                    time   = 120;
                    break;

                case ActionLog.API_ACCESS_STATUS:
                    secret = ActionLog.API_ACCESS_STATUS;
                    break;

                case ActionLog.API_ACCESS_DELETE:
                    secret = ActionLog.API_ACCESS_DELETE;
                    break;

                default:

                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }


                var checkSecret = HelpersApi.CheckToken(userModel, secret);

                if (checkSecret == null)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_ERROR));
                }

                if (checkSecret.NewSecret == null)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_ERROR));
                }

                if (checkSecret.Secret == null)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_ERROR));
                }

                userModel.SecretAuthToken = checkSecret.NewSecret;
                var resultUpdate = _userBusiness.UpdateProfile(userModel);

                return(resultUpdate.Status == Status.STATUS_ERROR
                    ? resultUpdate.ToJson()
                    : _userBusiness.SendSms(userModel, HelpersApi.SendCodeSms(checkSecret.Secret, time)).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.TWOFA_REQUIRED_SEND_CODE + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 13
0
        public string VerifyCodeEnable([FromBody] JObject value = null)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (userModel.IsTwoFactor == 2)
                {
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    var code          = value[ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_CODE].ToString();
                    var authenticator = new TwoStepsAuthenticator.TimeAuthenticator();

                    var secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);

                    if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                    }

                    var isOk = authenticator.CheckCode(secretAuthToken.CustomTwofa, code, userModel);

                    if (!isOk)
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                    }
                }
                else if (userModel.IsTwoFactor == 0)
                {
                    if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }
                }


                var google = new GoogleAuthen.TwoFactorAuthenticator();

                var secretKey = CommonHelper.RandomString(32);

                var startSetup = google.GenerateSetupCode(userModel.Email, secretKey, 300, 300);

                userModel.TwoFactorSecret = secretKey;
                Console.WriteLine(secretKey);
                var resultUpdate = _userBusiness.UpdateProfile(userModel);

                if (resultUpdate.Status == Status.STATUS_ERROR)
                {
                    return(resultUpdate.ToJson());
                }

                return(new ReturnObject
                {
                    Status = Status.STATUS_SUCCESS,
                    Data = startSetup.ManualEntryKey
                }.ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.TWOFA_ENABLE_VERIFY + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }
Exemplo n.º 14
0
        public string CustomTwo([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];
                if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_STATUS))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var status = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_STATUS];


                var code = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                {
                    code = value[ParseDataKeyApi.KEY_PASS_DATA_GET_CODE].ToString();
                }
                var token = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_GOOGLE_TOKEN))
                {
                    token = value[ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_GOOGLE_TOKEN].ToString();
                }


                bool       isVerify = false;
                ActionCode secretAuthToken;

                switch (userModel.IsTwoFactor)
                {
                case 1:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }


                    if ((int)status == 2)
                    {
                        secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);

                        if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                        }


                        isVerify = HelpersApi.CheckCodeSms(secretAuthToken.CustomTwofa, code, userModel);
                    }
                    else if ((int)status == 0)
                    {
                        isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    }

                    break;

                case 2:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    if ((int)status == 0)
                    {
                        secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);

                        if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                        }

                        isVerify = HelpersApi.CheckCodeSms(secretAuthToken.CustomTwofa, code, userModel);
                    }
                    else if ((int)status == 1)
                    {
                        isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    }

                    break;

                case 0:
                    if ((int)status == 1)
                    {
                        if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                        }
                        Console.WriteLine(userModel.TwoFactorSecret);

                        isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    }
                    else if ((int)status == 2)
                    {
                        if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                        }

                        secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);
                        if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa))
                        {
                            return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                        }

                        isVerify = HelpersApi.CheckCodeSms(secretAuthToken.CustomTwofa, code, userModel);
                    }

                    break;
                }

                Console.WriteLine(isVerify);

                if (!isVerify)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                }

                userModel.IsTwoFactor = (int)status;

                _userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                           ActionLog.TWOFA_ENABLE,
                                           HelpersApi.GetIp(Request));

                return(_userBusiness.UpdateProfile(userModel).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.TWOFA_ENABLE_UPDATE + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }