Exemplo n.º 1
0
        /// <summary>
        /// 获取规范化的头信息
        /// </summary>
        /// <param name="builder">http 请求信息</param>
        /// <returns>规范化头信息字符串</returns>
        private string GetCanonicalizedHeaderString(SdkHttpFullRequestBuilder builder)
        {
            var           headers       = builder.GetHeaders();
            var           keys          = headers.Keys;
            List <string> keysList      = keys.ToList().OrderBy(p => p.ToLower(CultureInfo.GetCultureInfo("en-US"))).ToList();
            StringBuilder stringBuilder = new StringBuilder();

            foreach (var item in keysList)
            {
                if (ShouldExcludeHeaderFromSigning(item))
                {
                    continue;
                }
                string key = item.ToLower(CultureInfo.GetCultureInfo("en-US"));

                foreach (var headerValue in headers[item])
                {
                    string headerCompactedValue = StringUtils.AppendCompactedString(key);
                    stringBuilder.Append(key).Append(":");
                    if (headerValue != null)
                    {
                        string headerValueCompactedValue = StringUtils.AppendCompactedString(headerValue);
                        stringBuilder.Append(headerValueCompactedValue);
                    }
                    stringBuilder.Append("\n");
                }
            }


            return(stringBuilder.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 签名的具体实现
        /// </summary>
        /// <param name="builder">http请求信息</param>
        /// <param name="credentials">证书信息</param>
        /// <returns>请求后的http 信息</returns>
        private SdkHttpFullRequestBuilder DoSign(SdkHttpFullRequestBuilder builder, Credentials credentials)
        {
            Credentials sanitizedCredentials = SanitizeCredentials(credentials);

            if (credentials.GetType() == typeof(SessionCredentials))
            {
                AddSessionCredentials(builder, (SessionCredentials)credentials);
            }
            //20180627T065220Z
            //_overrddenDate = new DateTime(2018,6,27,6, 52, 20);
            SignerRequestParams signerRequestParams = new SignerRequestParams(builder, _overrddenDate,
                                                                              RegionName, ServiceName, ParameterConstant.JDCLOUD2_SIGNING_ALGORITHM);

            // SignerRequestParams
            AddHostHeader(ref builder);
            builder.Header(ParameterConstant.X_JDCLOUD_DATE, signerRequestParams.FormattedSigningDateTime);
            string contentSha256 = string.Empty;

            if (builder.GetHeaders() != null && builder.GetHeaders().ContainsKey(ParameterConstant.X_JDCLOUD_CONTENT_SHA256) &&
                builder.GetHeaders()[ParameterConstant.X_JDCLOUD_CONTENT_SHA256] != null &&
                builder.GetHeaders()[ParameterConstant.X_JDCLOUD_CONTENT_SHA256].Count > 0)
            {
                contentSha256 = builder.GetHeaders()[ParameterConstant.X_JDCLOUD_CONTENT_SHA256][0];
            }
            else
            {
                contentSha256 = CalculateContentHash(builder);
            }
            string canonicalRequest = CreateCanonicalRequest(builder, contentSha256);
            string stringToSign     = CreateStringToSign(canonicalRequest, signerRequestParams);

            byte[] signingKey = deriveSigningKey(credentials, signerRequestParams);
            byte[] signature  = ComputeSignature(stringToSign, signingKey);
            builder.Header(ParameterConstant.AUTHORIZATION, BuildAuthorizationHeader(builder, signature, credentials, signerRequestParams));

            return(builder);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 生成签名头信息字符串
        /// </summary>
        /// <param name="builder">http 请求信息</param>
        /// <returns>签名头信息字符串</returns>
        private string GetSignedHeadersString(SdkHttpFullRequestBuilder builder)
        {
            var           headers       = builder.GetHeaders();
            var           keys          = headers.Keys;
            List <string> keysList      = keys.ToList().OrderBy(p => p.ToLower(CultureInfo.GetCultureInfo("en-US"))).ToList();
            StringBuilder stringBuilder = new StringBuilder();

            foreach (String header in keysList)
            {
                if (ShouldExcludeHeaderFromSigning(header))
                {
                    continue;
                }
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append(";");
                }
                stringBuilder.Append(header.ToLower(CultureInfo.GetCultureInfo("en-US")));
            }
            return(stringBuilder.ToString());
        }