QCloudCredentialProvider.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Text;
  3. using COSXML.Utils;
  4. using COSXML.CosException;
  5. using COSXML.Common;
  6. using COSXML.Log;
  7. using COSXML.Network;
  8. using System.IO;
  9. namespace COSXML.Auth
  10. {
  11. public abstract class QCloudCredentialProvider
  12. {
  13. public virtual QCloudCredentials GetQCloudCredentials()
  14. {
  15. return null;
  16. }
  17. public abstract void Refresh();
  18. public virtual QCloudCredentials GetQCloudCredentialsWithRequest(Request request)
  19. {
  20. return null;
  21. }
  22. public QCloudCredentials GetQCloudCredentialsCompat(Request request)
  23. {
  24. QCloudCredentials credentials = GetQCloudCredentialsWithRequest(request);
  25. if (credentials == null)
  26. {
  27. credentials = GetQCloudCredentials();
  28. }
  29. return credentials;
  30. }
  31. }
  32. /// <summary>
  33. /// 直接通过永久密钥初始化
  34. /// </summary>
  35. public class DefaultQCloudCredentialProvider : QCloudCredentialProvider
  36. {
  37. private string secretId;
  38. private string secretKey;
  39. private long keyTimDuration;
  40. public DefaultQCloudCredentialProvider(string secretId, string secretKey, long keyDurationSecond)
  41. {
  42. if (secretId == null || secretId.Length == 0)
  43. throw new CosClientException((int)CosClientError.InvalidArgument, "secretId is null or length is zero");
  44. if (secretKey == null || secretKey.Length == 0)
  45. throw new CosClientException((int)CosClientError.InvalidArgument, "secretKey is null or length is zero");
  46. this.secretId = secretId.Trim();
  47. this.secretKey = secretKey.Trim();
  48. this.keyTimDuration = keyDurationSecond;
  49. }
  50. public override QCloudCredentials GetQCloudCredentials()
  51. {
  52. long keyStartTime = TimeUtils_QCloud.GetCurrentTime(TimeUnit.Seconds);
  53. long keyEndTime = keyStartTime + keyTimDuration;
  54. string keyTime = String.Format("{0};{1}", keyStartTime, keyEndTime);
  55. if (secretId == null)
  56. {
  57. throw new CosClientException((int)CosClientError.InvalidCredentials, "secretId == null");
  58. }
  59. if (secretKey == null)
  60. {
  61. throw new CosClientException((int)CosClientError.InvalidCredentials, "secretKey == null");
  62. }
  63. string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, secretKey, Encoding.UTF8);
  64. return new QCloudCredentials(secretId, signKey, keyTime);
  65. }
  66. public override void Refresh()
  67. {
  68. //TODO update value
  69. QLog.Debug("DefaultQCloudCredentialProvider", "need to update QCloudCredentials");
  70. //invoke SetSetQCloudCredential(string secretId, string secretKey, string keyTime)
  71. }
  72. }
  73. /// <summary>
  74. /// 通过腾讯云临时密钥初始化
  75. /// </summary>
  76. public class DefaultSessionQCloudCredentialProvider : QCloudCredentialProvider
  77. {
  78. private string tmpSecretId;
  79. private string tmpSecretKey;
  80. private string keyTime;
  81. private string token;
  82. public DefaultSessionQCloudCredentialProvider(string tmpSecretId, string tmpSecretKey, long tmpExpiredTime, string sessionToken)
  83. : this(tmpSecretId, tmpSecretKey, TimeUtils_QCloud.GetCurrentTime(TimeUnit.Seconds), tmpExpiredTime, sessionToken)
  84. {
  85. }
  86. public DefaultSessionQCloudCredentialProvider(string tmpSecretId, string tmpSecretKey, long keyStartTimeSecond, long tmpExpiredTime, string sessionToken)
  87. {
  88. this.tmpSecretId = tmpSecretId;
  89. this.tmpSecretKey = tmpSecretKey;
  90. this.keyTime = String.Format("{0};{1}", keyStartTimeSecond, tmpExpiredTime);
  91. this.token = sessionToken;
  92. }
  93. public override QCloudCredentials GetQCloudCredentials()
  94. {
  95. if (IsNeedUpdateNow())
  96. {
  97. Refresh();
  98. }
  99. if (tmpSecretId == null)
  100. {
  101. throw new CosClientException((int)CosClientError.InvalidCredentials, "secretId == null");
  102. }
  103. if (tmpSecretKey == null)
  104. {
  105. throw new CosClientException((int)CosClientError.InvalidCredentials, "secretKey == null");
  106. }
  107. if (keyTime == null)
  108. {
  109. throw new CosClientException((int)CosClientError.InvalidCredentials, "keyTime == null");
  110. }
  111. string signKey = DigestUtils.GetHamcSha1ToHexString(keyTime, Encoding.UTF8, tmpSecretKey, Encoding.UTF8);
  112. return new SessionQCloudCredentials(tmpSecretId, signKey, token, keyTime);
  113. }
  114. public override void Refresh()
  115. {
  116. //TODO update value
  117. QLog.Debug("DefaultSessionQCloudCredentialProvider", "need to update QCloudCredentials");
  118. //invoke SetQCloudCredential(string tmpSecretId, string tmpSecretKey, string tmpkeyTime, string sessionToken)
  119. }
  120. public bool IsNeedUpdateNow()
  121. {
  122. if (String.IsNullOrEmpty(keyTime) || String.IsNullOrEmpty(tmpSecretId) || String.IsNullOrEmpty(tmpSecretKey) || String.IsNullOrEmpty(token))
  123. {
  124. return true;
  125. }
  126. int index = keyTime.IndexOf(';');
  127. long endTime = -1L;
  128. long.TryParse(keyTime.Substring(index + 1), out endTime);
  129. long nowTime = TimeUtils_QCloud.GetCurrentTime(TimeUnit.Seconds);
  130. if (endTime <= nowTime)
  131. {
  132. return true;
  133. }
  134. return false;
  135. }
  136. /// <summary>
  137. /// 直接设置临时密钥信息
  138. /// </summary>
  139. /// <param name="tmpSecretId">临时安全证书 Id</param>
  140. /// <param name="tmpSecretKey">临时安全证书 Key</param>
  141. /// <param name="tmpkeyTime">证书有效的期间</param>
  142. /// <param name="sessionToken">token 值</param>
  143. public void SetQCloudCredential(string tmpSecretId, string tmpSecretKey, string tmpkeyTime, string sessionToken)
  144. {
  145. this.tmpSecretId = tmpSecretId;
  146. this.tmpSecretKey = tmpSecretKey;
  147. this.token = sessionToken;
  148. this.keyTime = tmpkeyTime;
  149. }
  150. }
  151. }