Esempio n. 1
0
        private void PrepareParameters(AntCloudClientRequest request)
        {
            request.PutParameter(SDKConstants.ParamKeys.ACCESS_KEY, _accessKey);

            request.PutParameterIfAbsent(SDKConstants.ParamKeys.SIGN_TYPE, SDKConstants.DEFAULT_SIGN_TYPE);
            request.PutParameterIfAbsent(SDKConstants.ParamKeys.REQ_MSG_ID, SdkUtils.GenerateReqMsgId());
            request.PutParameterIfAbsent(SDKConstants.ParamKeys.REQ_TIME, SdkUtils.FormatDate(DateTime.UtcNow));

            var signType = request.GetParameter(SDKConstants.ParamKeys.SIGN_TYPE);

            if (!signType.Equals(SDKConstants.DEFAULT_SIGN_TYPE, StringComparison.OrdinalIgnoreCase) &&
                !signType.Equals(SDKConstants.SIGN_TYPE_SHA256))
            {
                throw new ArgumentException("wrong sign type");
            }

            if (!string.IsNullOrWhiteSpace(_securityToken))
            {
                request.PutParameterIfAbsent(SDKConstants.ParamKeys.SECURITY_TOKEN, _securityToken);
            }

            request.PutParameterIfAbsent(SDKConstants.ParamKeys.BASE_SDK_VERSION, SDKConstants.BASE_SDK_VERSION_VALUE);

            var sign = ComputeSign(request.GetParameters(), signType, _accessSecret, SDKConstants.SIGN_CHARSET);

            request.PutParameter(SDKConstants.ParamKeys.SIGN, sign);
        }
Esempio n. 2
0
        private async Task <AntCloudClientResponse> SendRequest(AntCloudClientRequest request)
        {
            var retried = 0;

            var parameters = request.GetParameters();
            var message    = BuildRequest(_endpoint, parameters);

            while (true)
            {
                RequestSending?.Invoke(this, parameters);

                var response = await _httpClient.SendAsync(message);

                var responseString = await response.Content.ReadAsStringAsync();

                ResponseRead?.Invoke(this, responseString);

                JObject wholeJson;
                try
                {
                    wholeJson = JObject.Parse(responseString);
                }
                catch (JsonReaderException e)
                {
                    if (_enableAutoRetry && retried < _autoRetryLimit)
                    {
                        retried += 1;
                        continue;
                    }
                    throw new ClientException(SDKConstants.ResultCodes.RESPONSE_FORMAT_ERROR, e.Message);
                }

                if (wholeJson == null)
                {
                    throw new ClientException(SDKConstants.ResultCodes.TRANSPORT_ERROR, "Unexpected gateway response: " + responseString);
                }

                var responseNode = wholeJson["response"] as JObject;
                if (responseNode == null)
                {
                    throw new ClientException(SDKConstants.ResultCodes.TRANSPORT_ERROR, "Unexpected gateway response: " + responseString);
                }

                var result = AntCloudClientResponse.Create(responseNode);
                if (result.IsSuccess && _checkSign)
                {
                    var sign           = wholeJson.Value <string>(SDKConstants.ParamKeys.SIGN);
                    var target         = ExtractStringToSign(responseString);
                    var calculatedSign = ComputeSign(target, request.GetParameter(SDKConstants.ParamKeys.SIGN_TYPE), _accessSecret, SDKConstants.SIGN_CHARSET);

                    if (!calculatedSign.Equals(sign))
                    {
                        throw new ClientException(SDKConstants.ResultCodes.BAD_SIGNATURE, "Invalid signature in response");
                    }
                }

                return(result);
            }
        }