Beispiel #1
0
        public IActionResult Verify(string partnerCode, string input)
        {
            var signed = Request.Headers["signed"];

            _encrypt.SetKey(partnerCode);
            var result = _encrypt.DecryptData(signed, input);

            return(Ok(result));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private Tuple <int, string> CheckBasicAuthenForPartner(HttpRequest request)
        {
            StringBuilder       log    = new StringBuilder();
            Tuple <int, string> result = new Tuple <int, string>(1, "success");

            try
            {
                try
                {
                    log.AppendLine(request.Path.Value);
                    log.AppendLine(JsonConvert.SerializeObject(request.Headers));
                    var task = Task.Run(() => ReadRequestBody(request)).GetAwaiter();
                    log.AppendLine(task.GetResult());
                }
                catch (Exception)
                {
                    log.AppendLine("null body");
                }

                var keys = new[] {
                    "f936792f71344a6eabf773f18e2694e4",
                    "99793bb9137042a3a7f15950f1215950",// khuê
                    "bkt.partner"
                };

                long   timestampReq = long.Parse(request.Query["timestamp"].ToString());
                string keyReq       = request.Query["partner_code"].ToString();
                string checksumReq  = request.Query["hash"].ToString();

                // A kiểm tra lời gọi api có phải xuất phát từ B (đã đăng ký liên kết từ trước) hay không
                if (!keys.Any(x => x.Equals(keyReq)))
                {
                    return(new Tuple <int, string>(400, "partner_code invalid"));
                }

                // A kiểm tra xem lời gọi này là mới hay là thông tin cũ đã quá hạn
                long timestamp = ((DateTimeOffset)DateTime.UtcNow.AddMinutes(-180)).ToUnixTimeSeconds();
                if (timestamp > timestampReq)
                {
                    return(new Tuple <int, string>(400, "timestamp expired"));
                }

                // Check toàn vẹn dữ liệu
                if (request.Method.Equals("POST"))
                {
                    if (request.Path.Value.ToLower().Contains("api/transactions/receive_external".ToLower()))
                    {
                        var infoPartner = _linkingBank.GetLinkingBankById(new Models.Filters.LinkingBankFilter()
                        {
                            Code = keyReq
                        });
                        if (infoPartner == null)
                        {
                            return(new Tuple <int, string>(500, "internal server error"));
                        }
                        var    task      = Task.Run(() => ReadRequestBody(request)).GetAwaiter();
                        var    temp      = task.GetResult();
                        var    obj       = JsonConvert.DeserializeObject <TransferMoneyRequest>(temp);
                        string secretKey = infoPartner.SecretKey;
                        string input     = $"{keyReq}|{timestampReq}|{obj.from_account_number}|{obj.to_account_number}|{(int)obj.amount}|{obj.message}";

                        if (!Encrypting.HMD5Verify(input, checksumReq, secretKey))
                        {
                            log.Append("Hash: false");
                            return(new Tuple <int, string>(400, "hash invalid"));
                        }

                        // Nếu là controller partners thì check thêm mã hóa bất đối xứng
                        string encrypt = request.Query["signature"].ToString();
                        if (!string.IsNullOrWhiteSpace(encrypt))
                        {
                            string hash = Encrypting.HMD5Hash(input, secretKey);
                            _encrypt.SetKey(keyReq);
                            if (_encrypt.DecryptData(encrypt, hash))
                            {
                                return(result);
                            }
                            else
                            {
                                log.Append("DecryptData: false");
                                return(new Tuple <int, string>(400, "signature invalid"));
                            }
                        }
                        else
                        {
                            log.Append("DecryptData: false");
                            return(new Tuple <int, string>(400, "signature invalid"));
                        }
                    }
                    else if (request.Path.Value.ToLower().Contains("api/transactions/query_info".ToLower()))
                    {
                        var infoPartner = _linkingBank.GetLinkingBankById(new Models.Filters.LinkingBankFilter()
                        {
                            Code = keyReq
                        });
                        if (infoPartner == null)
                        {
                            return(new Tuple <int, string>(500, "internal server error"));
                        }

                        var    task      = Task.Run(() => ReadRequestBody(request)).GetAwaiter();
                        var    temp      = task.GetResult();
                        var    obj       = JsonConvert.DeserializeObject <InfoUserRequest>(temp);
                        string secretKey = infoPartner.SecretKey;
                        string hash      = $"{keyReq}|{timestampReq}|{obj.account_number}";

                        if (!Encrypting.HMD5Verify(hash, checksumReq, secretKey))
                        {
                            log.Append("Hash: false");
                            return(new Tuple <int, string>(400, "hash invalid"));
                        }
                    }
                    else
                    {
                        return(new Tuple <int, string>(400, "invalid url"));
                    }
                }
            }
            catch (Exception ex)
            {
                log.Append(ex.Message);
                return(new Tuple <int, string>(500, "internal server error"));
            }
            finally
            {
                LoggingTxt.InsertLog(log.ToString());
            }
            return(result);
        }