RsaKeyPairCredential.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace AlibabaCloud\Credentials;
  3. use AlibabaCloud\Credentials\Providers\RsaKeyPairProvider;
  4. use AlibabaCloud\Credentials\Signature\ShaHmac1Signature;
  5. use Exception;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use InvalidArgumentException;
  8. /**
  9. * Use the RSA key pair to complete the authentication (supported only on Japanese site)
  10. */
  11. class RsaKeyPairCredential implements CredentialsInterface
  12. {
  13. /**
  14. * @var string
  15. */
  16. private $publicKeyId;
  17. /**
  18. * @var string
  19. */
  20. private $privateKey;
  21. /**
  22. * @var array
  23. */
  24. private $config;
  25. /**
  26. * RsaKeyPairCredential constructor.
  27. *
  28. * @param string $public_key_id
  29. * @param string $private_key_file
  30. * @param array $config
  31. */
  32. public function __construct($public_key_id, $private_key_file, array $config = [])
  33. {
  34. Filter::publicKeyId($public_key_id);
  35. Filter::privateKeyFile($private_key_file);
  36. $this->publicKeyId = $public_key_id;
  37. $this->config = $config;
  38. try {
  39. $this->privateKey = file_get_contents($private_key_file);
  40. } catch (Exception $exception) {
  41. throw new InvalidArgumentException($exception->getMessage());
  42. }
  43. }
  44. /**
  45. * @return array
  46. */
  47. public function getConfig()
  48. {
  49. return $this->config;
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function getOriginalAccessKeyId()
  55. {
  56. return $this->getPublicKeyId();
  57. }
  58. /**
  59. * @return string
  60. */
  61. public function getPublicKeyId()
  62. {
  63. return $this->publicKeyId;
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getOriginalAccessKeySecret()
  69. {
  70. return $this->getPrivateKey();
  71. }
  72. /**
  73. * @return mixed
  74. */
  75. public function getPrivateKey()
  76. {
  77. return $this->privateKey;
  78. }
  79. /**
  80. * @return string
  81. */
  82. public function __toString()
  83. {
  84. return "publicKeyId#$this->publicKeyId";
  85. }
  86. /**
  87. * @return ShaHmac1Signature
  88. */
  89. public function getSignature()
  90. {
  91. return new ShaHmac1Signature();
  92. }
  93. /**
  94. * @return string
  95. * @throws Exception
  96. * @throws GuzzleException
  97. */
  98. public function getAccessKeyId()
  99. {
  100. return $this->getSessionCredential()->getAccessKeyId();
  101. }
  102. /**
  103. * @return StsCredential
  104. * @throws Exception
  105. * @throws GuzzleException
  106. */
  107. protected function getSessionCredential()
  108. {
  109. return (new RsaKeyPairProvider($this))->get();
  110. }
  111. /**
  112. * @return string
  113. * @throws Exception
  114. * @throws GuzzleException
  115. */
  116. public function getAccessKeySecret()
  117. {
  118. return $this->getSessionCredential()->getAccessKeySecret();
  119. }
  120. /**
  121. * @return string
  122. * @throws Exception
  123. * @throws GuzzleException
  124. */
  125. public function getSecurityToken()
  126. {
  127. return $this->getSessionCredential()->getSecurityToken();
  128. }
  129. /**
  130. * @return int
  131. * @throws Exception
  132. * @throws GuzzleException
  133. */
  134. public function getExpiration()
  135. {
  136. return $this->getSessionCredential()->getExpiration();
  137. }
  138. }