|
1 | 1 | <?php |
2 | | -# Copyright (c) 2013-2023, OVH SAS. |
| 2 | +# Copyright (c) 2013-2024, OVH SAS. |
3 | 3 | # All rights reserved. |
4 | 4 | # |
5 | 5 | # Redistribution and use in source and binary forms, with or without |
|
37 | 37 | use GuzzleHttp\Psr7\Response; |
38 | 38 | use Psr\Http\Message\ResponseInterface; |
39 | 39 |
|
| 40 | +include_once('OAuth2.php'); |
| 41 | + |
40 | 42 | /** |
41 | 43 | * Wrapper to manage login and exchanges with simpliest Ovh API |
42 | 44 | * |
@@ -66,6 +68,12 @@ class Api |
66 | 68 | 'runabove-ca' => 'https://api.runabove.com/1.0', |
67 | 69 | ]; |
68 | 70 |
|
| 71 | + private static $OAUTH2_TOKEN_URLS = [ |
| 72 | + "ovh-eu" => "https://www.ovh.com/auth/oauth2/token", |
| 73 | + "ovh-ca" => "https://ca.ovh.com/auth/oauth2/token", |
| 74 | + "ovh-us" => "https://us.ovhcloud.com/auth/oauth2/token", |
| 75 | + ]; |
| 76 | + |
69 | 77 | /** |
70 | 78 | * Contain endpoint selected to choose API |
71 | 79 | * |
@@ -108,6 +116,13 @@ class Api |
108 | 116 | */ |
109 | 117 | private ?Client $http_client; |
110 | 118 |
|
| 119 | + /** |
| 120 | + * OAuth2 wrapper if built with `withOAuth2` |
| 121 | + * |
| 122 | + * @var \Ovh\OAuth2 |
| 123 | + */ |
| 124 | + private ?OAuth2 $oauth2; |
| 125 | + |
111 | 126 | /** |
112 | 127 | * Construct a new wrapper instance |
113 | 128 | * |
@@ -154,6 +169,26 @@ public function __construct( |
154 | 169 | $this->application_secret = $application_secret; |
155 | 170 | $this->http_client = $http_client; |
156 | 171 | $this->consumer_key = $consumer_key; |
| 172 | + $this->oauth2 = null; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Alternative constructor to build a client using OAuth2 |
| 177 | + * |
| 178 | + * @throws Exceptions\InvalidParameterException if one parameter is missing or with bad value |
| 179 | + * @return Ovh\Api |
| 180 | + */ |
| 181 | + public static function withOAuth2($clientId, $clientSecret, $apiEndpoint) |
| 182 | + { |
| 183 | + if (!array_key_exists($apiEndpoint, self::$OAUTH2_TOKEN_URLS)) { |
| 184 | + throw new Exceptions\InvalidParameterException( |
| 185 | + "OAuth2 authentication is not compatible with endpoint $apiEndpoint (it can only be used with ovh-eu, ovh-ca and ovh-us)" |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + $instance = new self("", "", $apiEndpoint); |
| 190 | + $instance->oauth2 = new Oauth2($clientId, $clientSecret, self::$OAUTH2_TOKEN_URLS[$apiEndpoint]); |
| 191 | + return $instance; |
157 | 192 | } |
158 | 193 |
|
159 | 194 | /** |
@@ -298,22 +333,29 @@ protected function rawCall($method, $path, $content = null, $is_authenticated = |
298 | 333 | } |
299 | 334 | $headers['Content-Type'] = 'application/json; charset=utf-8'; |
300 | 335 |
|
301 | | - $headers['X-Ovh-Application'] = $this->application_key ?? ''; |
302 | 336 | if ($is_authenticated) { |
303 | | - if (!isset($this->time_delta)) { |
304 | | - $this->calculateTimeDelta(); |
305 | | - } |
306 | | - $now = time() + $this->time_delta; |
| 337 | + if (!is_null($this->oauth2)) { |
| 338 | + $headers['Authorization'] = $this->oauth2->getAuthorizationHeader(); |
| 339 | + } else { |
| 340 | + $headers['X-Ovh-Application'] = $this->application_key ?? ''; |
| 341 | + |
| 342 | + if (!isset($this->time_delta)) { |
| 343 | + $this->calculateTimeDelta(); |
| 344 | + } |
| 345 | + $now = time() + $this->time_delta; |
307 | 346 |
|
308 | | - $headers['X-Ovh-Timestamp'] = $now; |
| 347 | + $headers['X-Ovh-Timestamp'] = $now; |
309 | 348 |
|
310 | | - if (isset($this->consumer_key)) { |
311 | | - $toSign = $this->application_secret . '+' . $this->consumer_key . '+' . $method |
312 | | - . '+' . $url . '+' . $body . '+' . $now; |
313 | | - $signature = '$1$' . sha1($toSign); |
314 | | - $headers['X-Ovh-Consumer'] = $this->consumer_key; |
315 | | - $headers['X-Ovh-Signature'] = $signature; |
| 349 | + if (isset($this->consumer_key)) { |
| 350 | + $toSign = $this->application_secret . '+' . $this->consumer_key . '+' . $method |
| 351 | + . '+' . $url . '+' . $body . '+' . $now; |
| 352 | + $signature = '$1$' . sha1($toSign); |
| 353 | + $headers['X-Ovh-Consumer'] = $this->consumer_key; |
| 354 | + $headers['X-Ovh-Signature'] = $signature; |
| 355 | + } |
316 | 356 | } |
| 357 | + } else { |
| 358 | + $headers['X-Ovh-Application'] = $this->application_key ?? ''; |
317 | 359 | } |
318 | 360 |
|
319 | 361 | /** @var Response $response */ |
|
0 commit comments