Ejemplo n.º 1
0
        public void Sign(Request request, IQCloudSignSource qcloudSignSource, QCloudCredentials qcloudCredentials)
        {
            if (request == null)
            {
                throw new ArgumentNullException("Request == null");
            }

            if (qcloudCredentials == null)
            {
                throw new ArgumentNullException("QCloudCredentials == null");
            }

            if (qcloudSignSource == null || !(qcloudSignSource is CosXmlSignSourceProvider))
            {
                throw new ArgumentNullException("CosXmlSourceProvider == null");
            }

            CosXmlSignSourceProvider cosXmlSourceProvider = (CosXmlSignSourceProvider)qcloudSignSource;


            string signTime = cosXmlSourceProvider.GetSignTime();

            if (signTime == null)
            {
                signTime = qcloudCredentials.KeyTime;
                cosXmlSourceProvider.SetSignTime(signTime);
            }

            string        signature   = DigestUtils.GetHamcSha1ToHexString(cosXmlSourceProvider.Source(request), Encoding.UTF8, qcloudCredentials.SignKey, Encoding.UTF8);
            StringBuilder signBuilder = new StringBuilder();


            signBuilder.Append(CosAuthConstants.Q_SIGN_ALGORITHM).Append('=').Append(CosAuthConstants.SHA1).Append('&')
            .Append(CosAuthConstants.Q_AK).Append('=').Append(qcloudCredentials.SecretId).Append('&')
            .Append(CosAuthConstants.Q_SIGN_TIME).Append('=').Append(signTime).Append('&')
            .Append(CosAuthConstants.Q_KEY_TIME).Append('=').Append(qcloudCredentials.KeyTime).Append('&')
            .Append(CosAuthConstants.Q_HEADER_LIST).Append('=').Append(cosXmlSourceProvider.GetHeaderList()).Append('&')
            .Append(CosAuthConstants.Q_URL_PARAM_LIST).Append('=').Append(cosXmlSourceProvider.GetParameterList()).Append('&')
            .Append(CosAuthConstants.Q_SIGNATURE).Append('=').Append(signature);
            string sign = signBuilder.ToString();

            request.AddHeader(CosRequestHeaderKey.AUTHORIZAIION, sign);

            if (qcloudCredentials is SessionQCloudCredentials)
            {
                request.AddHeader(CosRequestHeaderKey.COS_SESSION_TOKEN, ((SessionQCloudCredentials)qcloudCredentials).Token);
            }

            if (cosXmlSourceProvider.onGetSign != null)
            {
                cosXmlSourceProvider.onGetSign(request, sign);
            }
        }
Ejemplo n.º 2
0
        public static string GenerateSign(string method, string path, Dictionary <string, string> queryParameters, Dictionary <string, string> headers, string signTime, QCloudCredentials qcloudCredentials)
        {
            if (qcloudCredentials == null)
            {
                throw new ArgumentNullException("QCloudCredentials == null");
            }

            CosXmlSignSourceProvider cosXmlSourceProvider = new CosXmlSignSourceProvider();

            if (signTime == null)
            {
                signTime = qcloudCredentials.KeyTime;
            }

            cosXmlSourceProvider.SetSignTime(signTime);

            if (headers != null)
            {
                foreach (string key in headers.Keys)
                {
                    cosXmlSourceProvider.AddHeaderKey(key);
                }
            }

            if (queryParameters != null)
            {
                foreach (string key in queryParameters.Keys)
                {
                    cosXmlSourceProvider.AddParameterKey(key);
                }
            }

            string signature = DigestUtils.GetHamcSha1ToHexString(cosXmlSourceProvider.GenerateSource(method, path, queryParameters, headers), Encoding.UTF8,
                                                                  qcloudCredentials.SignKey, Encoding.UTF8);

            StringBuilder signBuilder = new StringBuilder();

            signBuilder.Append(CosAuthConstants.Q_SIGN_ALGORITHM).Append('=').Append(CosAuthConstants.SHA1).Append('&')
            .Append(CosAuthConstants.Q_AK).Append('=').Append(qcloudCredentials.SecretId).Append('&')
            .Append(CosAuthConstants.Q_SIGN_TIME).Append('=').Append(cosXmlSourceProvider.GetSignTime()).Append('&')
            .Append(CosAuthConstants.Q_KEY_TIME).Append('=').Append(qcloudCredentials.KeyTime).Append('&')
            .Append(CosAuthConstants.Q_HEADER_LIST).Append('=').Append(cosXmlSourceProvider.GetHeaderList()).Append('&')
            .Append(CosAuthConstants.Q_URL_PARAM_LIST).Append('=').Append(cosXmlSourceProvider.GetParameterList()).Append('&')
            .Append(CosAuthConstants.Q_SIGNATURE).Append('=').Append(signature);

            if (qcloudCredentials is SessionQCloudCredentials)
            {
                signBuilder.Append("&").Append(CosRequestHeaderKey.COS_SESSION_TOKEN).Append("=").Append(((SessionQCloudCredentials)qcloudCredentials).Token);
            }

            return(signBuilder.ToString());
        }