コード例 #1
0
            /// <summary>
            /// 初始化临时账号
            /// </summary>
            /// <param name="fileKey">操作文件 key,可以为空,删除资源的时候不能为空</param>
            public async Task Init(string fileKey)
            {
                using (await AsyncLocker.LockAsync()) {
                    // 防止重复初始化
                    if (!IsTimeout())
                    {
                        return;
                    }
                    var rest = await fetchTmpCred(fileKey);

                    if (!HttpRestService.ForData(rest, out var cred))
                    {
                        throw new RestFailedException(rest.Message);
                    }
                    TmpCred = cred;
                    var    keyTime = $"{TimeHelper.GetTimestampSec(cred.StartTime)};{TimeHelper.GetTimestampSec(cred.ExpiredTime)}";
                    string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, cred.TmpSecretKey, Encoding.UTF8);
                    cloudCredentials = new SessionQCloudCredentials(
                        cred.TmpSecretId,
                        signKey,
                        cred.SessionToken,
                        keyTime
                        );
                }
            }
コード例 #2
0
        public override QCloudCredentials GetQCloudCredentials()
        {
            if (IsNeedUpdateNow())
            {
                Refresh();
            }

            if (tmpSecretId == null)
            {
                throw new CosClientException((int)CosClientError.InvalidCredentials, "secretId == null");
            }

            if (tmpSecretKey == null)
            {
                throw new CosClientException((int)CosClientError.InvalidCredentials, "secretKey == null");
            }

            if (keyTime == null)
            {
                throw new CosClientException((int)CosClientError.InvalidCredentials, "keyTime == null");
            }

            string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, tmpSecretKey, Encoding.UTF8);

            return(new SessionQCloudCredentials(tmpSecretId, signKey, token, keyTime));
        }
コード例 #3
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);
            }
        }
コード例 #4
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());
        }
コード例 #5
0
        public override QCloudCredentials GetQCloudCredentials()
        {
            long   keyStartTime = TimeUtils.GetCurrentTime(TimeUnit.SECONDS);
            long   keyEndTime   = keyStartTime + keyTimDuration;
            string keyTime      = String.Format("{0};{1}", keyStartTime, keyEndTime);

            if (secretId == null)
            {
                throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretId == null");
            }
            if (secretKey == null)
            {
                throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretKey == null");
            }
            string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, secretKey, Encoding.UTF8);

            return(new QCloudCredentials(secretId, signKey, keyTime));
        }
コード例 #6
0
        public override QCloudCredentials GetQCloudCredentials()
        {
            if (IsNeedUpdateNow())
            {
                Refresh();
            }
            if (secretId == null)
            {
                throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretId == null");
            }
            if (secretKey == null)
            {
                throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "secretKey == null");
            }
            if (keyTime == null)
            {
                throw new CosClientException((int)CosClientError.INVALID_CREDENTIALS, "keyTime == null");
            }
            string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, secretKey, Encoding.UTF8);

            return(new QCloudCredentials(secretId, signKey, keyTime));
        }