123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- using System;
- using System.Text;
- using COSXML.Utils;
- using COSXML.CosException;
- using COSXML.Common;
- using COSXML.Log;
- using COSXML.Network;
- using System.IO;
- namespace COSXML.Auth
- {
- public abstract class QCloudCredentialProvider
- {
- public virtual QCloudCredentials GetQCloudCredentials()
- {
- return null;
- }
- public abstract void Refresh();
- public virtual QCloudCredentials GetQCloudCredentialsWithRequest(Request request)
- {
- return null;
- }
- public QCloudCredentials GetQCloudCredentialsCompat(Request request)
- {
- QCloudCredentials credentials = GetQCloudCredentialsWithRequest(request);
- if (credentials == null)
- {
- credentials = GetQCloudCredentials();
- }
- return credentials;
- }
- }
- /// <summary>
- /// 直接通过永久密钥初始化
- /// </summary>
- public class DefaultQCloudCredentialProvider : QCloudCredentialProvider
- {
- private string secretId;
- private string secretKey;
- private long keyTimDuration;
- public DefaultQCloudCredentialProvider(string secretId, string secretKey, long keyDurationSecond)
- {
- if (secretId == null || secretId.Length == 0)
- throw new CosClientException((int)CosClientError.InvalidArgument, "secretId is null or length is zero");
- if (secretKey == null || secretKey.Length == 0)
- throw new CosClientException((int)CosClientError.InvalidArgument, "secretKey is null or length is zero");
- this.secretId = secretId.Trim();
- this.secretKey = secretKey.Trim();
- this.keyTimDuration = keyDurationSecond;
- }
- public override QCloudCredentials GetQCloudCredentials()
- {
- long keyStartTime = TimeUtils_QCloud.GetCurrentTime(TimeUnit.Seconds);
- long keyEndTime = keyStartTime + keyTimDuration;
- string keyTime = String.Format("{0};{1}", keyStartTime, keyEndTime);
- if (secretId == null)
- {
- throw new CosClientException((int)CosClientError.InvalidCredentials, "secretId == null");
- }
- if (secretKey == null)
- {
- throw new CosClientException((int)CosClientError.InvalidCredentials, "secretKey == null");
- }
- string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, secretKey, Encoding.UTF8);
- return new QCloudCredentials(secretId, signKey, keyTime);
- }
- public override void Refresh()
- {
- //TODO update value
- QLog.Debug("DefaultQCloudCredentialProvider", "need to update QCloudCredentials");
- //invoke SetSetQCloudCredential(string secretId, string secretKey, string keyTime)
- }
- }
- /// <summary>
- /// 通过腾讯云临时密钥初始化
- /// </summary>
- public class DefaultSessionQCloudCredentialProvider : QCloudCredentialProvider
- {
- private string tmpSecretId;
- private string tmpSecretKey;
- private string keyTime;
- private string token;
- public DefaultSessionQCloudCredentialProvider(string tmpSecretId, string tmpSecretKey, long tmpExpiredTime, string sessionToken)
- : this(tmpSecretId, tmpSecretKey, TimeUtils_QCloud.GetCurrentTime(TimeUnit.Seconds), tmpExpiredTime, sessionToken)
- {
- }
- public DefaultSessionQCloudCredentialProvider(string tmpSecretId, string tmpSecretKey, long keyStartTimeSecond, long tmpExpiredTime, string sessionToken)
- {
- this.tmpSecretId = tmpSecretId;
- this.tmpSecretKey = tmpSecretKey;
- this.keyTime = String.Format("{0};{1}", keyStartTimeSecond, tmpExpiredTime);
- this.token = sessionToken;
- }
- 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);
- }
- public override void Refresh()
- {
- //TODO update value
- QLog.Debug("DefaultSessionQCloudCredentialProvider", "need to update QCloudCredentials");
- //invoke SetQCloudCredential(string tmpSecretId, string tmpSecretKey, string tmpkeyTime, string sessionToken)
- }
- public bool IsNeedUpdateNow()
- {
- if (String.IsNullOrEmpty(keyTime) || String.IsNullOrEmpty(tmpSecretId) || String.IsNullOrEmpty(tmpSecretKey) || String.IsNullOrEmpty(token))
- {
- return true;
- }
- int index = keyTime.IndexOf(';');
- long endTime = -1L;
- long.TryParse(keyTime.Substring(index + 1), out endTime);
- long nowTime = TimeUtils_QCloud.GetCurrentTime(TimeUnit.Seconds);
- if (endTime <= nowTime)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 直接设置临时密钥信息
- /// </summary>
- /// <param name="tmpSecretId">临时安全证书 Id</param>
- /// <param name="tmpSecretKey">临时安全证书 Key</param>
- /// <param name="tmpkeyTime">证书有效的期间</param>
- /// <param name="sessionToken">token 值</param>
- public void SetQCloudCredential(string tmpSecretId, string tmpSecretKey, string tmpkeyTime, string sessionToken)
- {
- this.tmpSecretId = tmpSecretId;
- this.tmpSecretKey = tmpSecretKey;
- this.token = sessionToken;
- this.keyTime = tmpkeyTime;
- }
- }
- }
|