Esempio n. 1
0
        /// <summary>
        /// add authorization
        /// </summary>
        /// <param name="qcloudSignSource">QCloudSignSource</param>
        /// <param name="request"></param>
        /// <param name="credentialProvider"></param>
        private void CheckSign(IQCloudSignSource qcloudSignSource, Request request, QCloudCredentialProvider credentialProvider)
        {
            // has authorizaiton, notice: using request.Headers, otherwise, error
            if (request.Headers.ContainsKey(CosRequestHeaderKey.AUTHORIZAIION))
            {
                QLog.Debug(TAG, "has add authorizaiton in headers");

                return;
            }

            //has no authorization, but signSourceProvider == null
            if (qcloudSignSource == null)
            {
                QLog.Debug(TAG, "signSourceProvider == null");

                return;
            }

            if (credentialProvider == null)
            {
                throw new ArgumentNullException("credentialsProvider == null");
            }

            CosXmlSigner signer = new CosXmlSigner();

            signer.Sign(request, qcloudSignSource, credentialProvider.GetQCloudCredentialsCompat(request));
        }
        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);
            }
        }