1 | <?php |
||
21 | class PandaSigner |
||
22 | { |
||
23 | /** |
||
24 | * Panda cloud id |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $cloudId; |
||
29 | |||
30 | /** |
||
31 | * Panda Account |
||
32 | * |
||
33 | * @var Account |
||
34 | */ |
||
35 | private $account; |
||
36 | |||
37 | 15 | private function __construct() |
|
40 | |||
41 | /** |
||
42 | * Sets the cloud id. |
||
43 | * |
||
44 | * @param string $cloudId |
||
45 | */ |
||
46 | 15 | public function setCloudId($cloudId) |
|
50 | |||
51 | /** |
||
52 | * Returns the cloud id. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getCloudId() |
||
60 | |||
61 | /** |
||
62 | * Sets the Account that contains the authorization information. |
||
63 | * |
||
64 | * @param Account $account |
||
65 | */ |
||
66 | 15 | public function setAccount(Account $account) |
|
70 | |||
71 | /** |
||
72 | * Returns the Account used for generating signatures. |
||
73 | * |
||
74 | * @return Account |
||
75 | */ |
||
76 | public function getAccount() |
||
80 | |||
81 | /** |
||
82 | * Generates the signature for a given set of request parameters and add |
||
83 | * this signature to the set of parameters. |
||
84 | * |
||
85 | * @param string $method The HTTP method |
||
86 | * @param string $path The request path |
||
87 | * @param string[] $params Request parameters |
||
88 | * |
||
89 | * @return string[] The signed parameters |
||
90 | */ |
||
91 | 13 | public function signParams($method, $path, array $params = array()) |
|
100 | |||
101 | /** |
||
102 | * Generates the signature for an API requests based on its parameters. |
||
103 | * |
||
104 | * @param string $method The HTTP method |
||
105 | * @param string $path The request path |
||
106 | * @param string[] $params Request parameters |
||
107 | * |
||
108 | * @return string The generated signature |
||
109 | */ |
||
110 | 15 | public function signature($method, $path, array $params = array()) |
|
111 | { |
||
112 | 15 | $params = $this->completeParams($params); |
|
113 | |||
114 | 15 | ksort($params); |
|
115 | |||
116 | 15 | if (isset($params['file'])) { |
|
117 | unset($params['file']); |
||
118 | } |
||
119 | |||
120 | 15 | $canonicalQueryString = str_replace( |
|
121 | 15 | array('+', '%5B', '%5D'), |
|
122 | 15 | array('%20', '[', ']'), |
|
123 | 15 | http_build_query($params, '', '&') |
|
124 | ); |
||
125 | 15 | $stringToSign = sprintf( |
|
126 | 15 | "%s\n%s\n%s\n%s", |
|
127 | 15 | strtoupper($method), |
|
128 | 15 | $this->account->getApiHost(), |
|
129 | $path, |
||
130 | 15 | $canonicalQueryString |
|
131 | ); |
||
132 | 15 | $hmac = hash_hmac('sha256', $stringToSign, $this->account->getSecretKey(), true); |
|
133 | |||
134 | 15 | return base64_encode($hmac); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns a Signing instance for a Cloud. |
||
139 | * |
||
140 | * @param string $cloudId The cloud id |
||
141 | * @param Account $account The authorization details |
||
142 | * |
||
143 | * @return PandaSigner The generated Signing instance |
||
144 | */ |
||
145 | 15 | public static function getInstance($cloudId, Account $account) |
|
153 | |||
154 | 15 | private function completeParams(array $params) |
|
173 | } |
||
174 |