| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the league/oauth2-client library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
* @link http://thephpleague.com/oauth2-client/ Documentation |
| 12 |
* @link https://packagist.org/packages/league/oauth2-client Packagist |
| 13 |
* @link https://github.com/thephpleague/oauth2-client GitHub |
| 14 |
*/ |
| 15 |
namespace YoastSEO_Vendor\League\OAuth2\Client\Provider; |
| 16 |
|
| 17 |
use InvalidArgumentException; |
| 18 |
use YoastSEO_Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
| 19 |
use YoastSEO_Vendor\League\OAuth2\Client\Token\AccessToken; |
| 20 |
use YoastSEO_Vendor\League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
| 21 |
use YoastSEO_Vendor\Psr\Http\Message\ResponseInterface; |
| 22 |
/** |
| 23 |
* Represents a generic service provider that may be used to interact with any |
| 24 |
* OAuth 2.0 service provider, using Bearer token authentication. |
| 25 |
*/ |
| 26 |
class GenericProvider extends \YoastSEO_Vendor\League\OAuth2\Client\Provider\AbstractProvider |
| 27 |
{ |
| 28 |
use BearerAuthorizationTrait; |
| 29 |
/** |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $urlAuthorize; |
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $urlAccessToken; |
| 37 |
/** |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private $urlResourceOwnerDetails; |
| 41 |
/** |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
private $accessTokenMethod; |
| 45 |
/** |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
private $accessTokenResourceOwnerId; |
| 49 |
/** |
| 50 |
* @var array|null |
| 51 |
*/ |
| 52 |
private $scopes = null; |
| 53 |
/** |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
private $scopeSeparator; |
| 57 |
/** |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
private $responseError = 'error'; |
| 61 |
/** |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
private $responseCode; |
| 65 |
/** |
| 66 |
* @var string |
| 67 |
*/ |
| 68 |
private $responseResourceOwnerId = 'id'; |
| 69 |
/** |
| 70 |
* @var string|null |
| 71 |
*/ |
| 72 |
private $pkceMethod = null; |
| 73 |
/** |
| 74 |
* @param array $options |
| 75 |
* @param array $collaborators |
| 76 |
*/ |
| 77 |
public function __construct(array $options = [], array $collaborators = []) |
| 78 |
{ |
| 79 |
$this->assertRequiredOptions($options); |
| 80 |
$possible = $this->getConfigurableOptions(); |
| 81 |
$configured = \array_intersect_key($options, \array_flip($possible)); |
| 82 |
foreach ($configured as $key => $value) { |
| 83 |
$this->{$key} = $value; |
| 84 |
} |
| 85 |
// Remove all options that are only used locally |
| 86 |
$options = \array_diff_key($options, $configured); |
| 87 |
parent::__construct($options, $collaborators); |
| 88 |
} |
| 89 |
/** |
| 90 |
* Returns all options that can be configured. |
| 91 |
* |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
protected function getConfigurableOptions() |
| 95 |
{ |
| 96 |
return \array_merge($this->getRequiredOptions(), ['accessTokenMethod', 'accessTokenResourceOwnerId', 'scopeSeparator', 'responseError', 'responseCode', 'responseResourceOwnerId', 'scopes', 'pkceMethod']); |
| 97 |
} |
| 98 |
/** |
| 99 |
* Returns all options that are required. |
| 100 |
* |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
protected function getRequiredOptions() |
| 104 |
{ |
| 105 |
return ['urlAuthorize', 'urlAccessToken', 'urlResourceOwnerDetails']; |
| 106 |
} |
| 107 |
/** |
| 108 |
* Verifies that all required options have been passed. |
| 109 |
* |
| 110 |
* @param array $options |
| 111 |
* @return void |
| 112 |
* @throws InvalidArgumentException |
| 113 |
*/ |
| 114 |
private function assertRequiredOptions(array $options) |
| 115 |
{ |
| 116 |
$missing = \array_diff_key(\array_flip($this->getRequiredOptions()), $options); |
| 117 |
if (!empty($missing)) { |
| 118 |
throw new \InvalidArgumentException('Required options not defined: ' . \implode(', ', \array_keys($missing))); |
| 119 |
} |
| 120 |
} |
| 121 |
/** |
| 122 |
* @inheritdoc |
| 123 |
*/ |
| 124 |
public function getBaseAuthorizationUrl() |
| 125 |
{ |
| 126 |
return $this->urlAuthorize; |
| 127 |
} |
| 128 |
/** |
| 129 |
* @inheritdoc |
| 130 |
*/ |
| 131 |
public function getBaseAccessTokenUrl(array $params) |
| 132 |
{ |
| 133 |
return $this->urlAccessToken; |
| 134 |
} |
| 135 |
/** |
| 136 |
* @inheritdoc |
| 137 |
*/ |
| 138 |
public function getResourceOwnerDetailsUrl(\YoastSEO_Vendor\League\OAuth2\Client\Token\AccessToken $token) |
| 139 |
{ |
| 140 |
return $this->urlResourceOwnerDetails; |
| 141 |
} |
| 142 |
/** |
| 143 |
* @inheritdoc |
| 144 |
*/ |
| 145 |
public function getDefaultScopes() |
| 146 |
{ |
| 147 |
return $this->scopes; |
| 148 |
} |
| 149 |
/** |
| 150 |
* @inheritdoc |
| 151 |
*/ |
| 152 |
protected function getAccessTokenMethod() |
| 153 |
{ |
| 154 |
return $this->accessTokenMethod ?: parent::getAccessTokenMethod(); |
| 155 |
} |
| 156 |
/** |
| 157 |
* @inheritdoc |
| 158 |
*/ |
| 159 |
protected function getAccessTokenResourceOwnerId() |
| 160 |
{ |
| 161 |
return $this->accessTokenResourceOwnerId ?: parent::getAccessTokenResourceOwnerId(); |
| 162 |
} |
| 163 |
/** |
| 164 |
* @inheritdoc |
| 165 |
*/ |
| 166 |
protected function getScopeSeparator() |
| 167 |
{ |
| 168 |
return $this->scopeSeparator ?: parent::getScopeSeparator(); |
| 169 |
} |
| 170 |
/** |
| 171 |
* @inheritdoc |
| 172 |
*/ |
| 173 |
protected function getPkceMethod() |
| 174 |
{ |
| 175 |
return $this->pkceMethod ?: parent::getPkceMethod(); |
| 176 |
} |
| 177 |
/** |
| 178 |
* @inheritdoc |
| 179 |
*/ |
| 180 |
protected function checkResponse(\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response, $data) |
| 181 |
{ |
| 182 |
if (!empty($data[$this->responseError])) { |
| 183 |
$error = $data[$this->responseError]; |
| 184 |
if (!\is_string($error)) { |
| 185 |
$error = \var_export($error, \true); |
| 186 |
} |
| 187 |
$code = $this->responseCode && !empty($data[$this->responseCode]) ? $data[$this->responseCode] : 0; |
| 188 |
if (!\is_int($code)) { |
| 189 |
$code = \intval($code); |
| 190 |
} |
| 191 |
throw new \YoastSEO_Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException($error, $code, $data); |
| 192 |
} |
| 193 |
} |
| 194 |
/** |
| 195 |
* @inheritdoc |
| 196 |
*/ |
| 197 |
protected function createResourceOwner(array $response, \YoastSEO_Vendor\League\OAuth2\Client\Token\AccessToken $token) |
| 198 |
{ |
| 199 |
return new \YoastSEO_Vendor\League\OAuth2\Client\Provider\GenericResourceOwner($response, $this->responseResourceOwnerId); |
| 200 |
} |
| 201 |
} |
| 202 |
|