1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Credentials\Providers; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\SDK; |
6
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
7
|
|
|
use AlibabaCloud\Client\Result\Result; |
8
|
|
|
use AlibabaCloud\Client\Request\Request; |
9
|
|
|
use AlibabaCloud\Client\Credentials\StsCredential; |
10
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
11
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
12
|
|
|
use AlibabaCloud\Client\Credentials\AccessKeyCredential; |
13
|
|
|
use AlibabaCloud\Client\Signature\ShaHmac256WithRsaSignature; |
14
|
|
|
use AlibabaCloud\Client\Credentials\Requests\GenerateSessionAccessKey; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class RsaKeyPairProvider |
18
|
|
|
* |
19
|
|
|
* @package AlibabaCloud\Client\Credentials\Providers |
20
|
|
|
*/ |
21
|
|
|
class RsaKeyPairProvider extends Provider |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get credential. |
26
|
|
|
* |
27
|
|
|
* @param int $timeout |
28
|
|
|
* @param int $connectTimeout |
29
|
|
|
* |
30
|
|
|
* @return StsCredential |
31
|
|
|
* @throws ClientException |
32
|
|
|
* @throws ServerException |
33
|
|
|
*/ |
34
|
8 |
|
public function get($timeout = Request::TIMEOUT, $connectTimeout = Request::CONNECT_TIMEOUT) |
35
|
|
|
{ |
36
|
8 |
|
$credential = $this->getCredentialsInCache(); |
37
|
|
|
|
38
|
8 |
|
if ($credential === null) { |
39
|
5 |
|
$result = $this->request($timeout, $connectTimeout); |
40
|
|
|
|
41
|
4 |
|
if (!isset($result['SessionAccessKey']['SessionAccessKeyId'], |
42
|
4 |
|
$result['SessionAccessKey']['SessionAccessKeySecret'])) { |
43
|
1 |
|
throw new ServerException($result, $this->error, SDK::INVALID_CREDENTIAL); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
$credential = $result['SessionAccessKey']; |
47
|
3 |
|
$this->cache($credential); |
|
|
|
|
48
|
3 |
|
} |
49
|
|
|
|
50
|
6 |
|
return new StsCredential( |
51
|
6 |
|
$credential['SessionAccessKeyId'], |
52
|
6 |
|
$credential['SessionAccessKeySecret'] |
53
|
6 |
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get credentials by request. |
58
|
|
|
* |
59
|
|
|
* @param $timeout |
60
|
|
|
* @param $connectTimeout |
61
|
|
|
* |
62
|
|
|
* @return Result |
63
|
|
|
* @throws ClientException |
64
|
|
|
* @throws ServerException |
65
|
|
|
*/ |
66
|
5 |
|
private function request($timeout, $connectTimeout) |
67
|
|
|
{ |
68
|
5 |
|
$clientName = __CLASS__ . \uniqid('rsa', true); |
69
|
5 |
|
$credential = $this->client->getCredential(); |
70
|
|
|
|
71
|
5 |
|
AlibabaCloud::client( |
72
|
5 |
|
new AccessKeyCredential( |
73
|
5 |
|
$credential->getPublicKeyId(), |
|
|
|
|
74
|
5 |
|
$credential->getPrivateKey() |
|
|
|
|
75
|
5 |
|
), |
76
|
5 |
|
new ShaHmac256WithRsaSignature() |
77
|
5 |
|
)->name($clientName); |
78
|
|
|
|
79
|
5 |
|
return (new GenerateSessionAccessKey($credential->getPublicKeyId())) |
80
|
5 |
|
->client($clientName) |
81
|
5 |
|
->timeout($timeout) |
82
|
5 |
|
->connectTimeout($connectTimeout) |
83
|
5 |
|
->debug($this->client->isDebug()) |
84
|
5 |
|
->request(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|