|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Graze\Gigya\Auth; |
| 4 | + |
| 5 | +use GuzzleHttp\Event\BeforeEvent; |
| 6 | +use GuzzleHttp\Event\RequestEvents; |
| 7 | + |
| 8 | +class GigyaHttpsAuth implements GigyaAuthInterface |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var string |
| 12 | + */ |
| 13 | + private $apiKey; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + private $secret; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var null|string |
| 22 | + */ |
| 23 | + private $userKey; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param string $apiKey |
| 27 | + * @param string $secret |
| 28 | + * @param string|null $userKey |
| 29 | + */ |
| 30 | + public function __construct($apiKey, $secret, $userKey = null) |
| 31 | + { |
| 32 | + $this->apiKey = $apiKey; |
| 33 | + $this->secret = $secret; |
| 34 | + $this->userKey = $userKey; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns an array of event names this subscriber wants to listen to. |
| 39 | + * |
| 40 | + * @return array |
| 41 | + */ |
| 42 | + public function getEvents() |
| 43 | + { |
| 44 | + return ['before' => ['sign', RequestEvents::SIGN_REQUEST]]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Add the authentication params to the request. |
| 49 | + * |
| 50 | + * @param BeforeEvent $event |
| 51 | + */ |
| 52 | + public function sign(BeforeEvent $event) |
| 53 | + { |
| 54 | + $request = $event->getRequest(); |
| 55 | + if ($request->getScheme() == 'https' && $request->getConfig()->get('auth') == 'gigya') { |
| 56 | + $query = $request->getQuery(); |
| 57 | + $query['apiKey'] = $this->apiKey; |
| 58 | + $query['secret'] = $this->secret; |
| 59 | + if ($this->userKey) { |
| 60 | + $query['userKey'] = $this->userKey; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + public function getApiKey() |
| 69 | + { |
| 70 | + return $this->apiKey; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return string |
| 75 | + */ |
| 76 | + public function getSecret() |
| 77 | + { |
| 78 | + return $this->secret; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return string |
| 83 | + */ |
| 84 | + public function getUserKey() |
| 85 | + { |
| 86 | + return $this->userKey; |
| 87 | + } |
| 88 | +} |
0 commit comments