<?php
 namespace League\OAuth2\Client\Provider; use InvalidArgumentException; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; use League\OAuth2\Client\Tool\BearerAuthorizationTrait; use Psr\Http\Message\ResponseInterface; class GenericProvider extends AbstractProvider { use BearerAuthorizationTrait; private $urlAuthorize; private $urlAccessToken; private $urlResourceOwnerDetails; private $accessTokenMethod; private $accessTokenResourceOwnerId; private $scopes = null; private $scopeSeparator; private $responseError = 'error'; private $responseCode; private $responseResourceOwnerId = 'id'; private $pkceMethod = null; public function __construct(array $options = [], array $collaborators = []) { $this->assertRequiredOptions($options); $possible = $this->getConfigurableOptions(); $configured = array_intersect_key($options, array_flip($possible)); foreach ($configured as $key => $value) { $this->$key = $value; } $options = array_diff_key($options, $configured); parent::__construct($options, $collaborators); } protected function getConfigurableOptions() { return array_merge($this->getRequiredOptions(), [ 'accessTokenMethod', 'accessTokenResourceOwnerId', 'scopeSeparator', 'responseError', 'responseCode', 'responseResourceOwnerId', 'scopes', 'pkceMethod', ]); } protected function getRequiredOptions() { return [ 'urlAuthorize', 'urlAccessToken', 'urlResourceOwnerDetails', ]; } private function assertRequiredOptions(array $options) { $missing = array_diff_key(array_flip($this->getRequiredOptions()), $options); if (!empty($missing)) { throw new InvalidArgumentException( 'Required options not defined: ' . implode(', ', array_keys($missing)) ); } } public function getBaseAuthorizationUrl() { return $this->urlAuthorize; } public function getBaseAccessTokenUrl(array $params) { return $this->urlAccessToken; } public function getResourceOwnerDetailsUrl(AccessToken $token) { return $this->urlResourceOwnerDetails; } public function getDefaultScopes() { return $this->scopes; } protected function getAccessTokenMethod() { return $this->accessTokenMethod ?: parent::getAccessTokenMethod(); } protected function getAccessTokenResourceOwnerId() { return $this->accessTokenResourceOwnerId ?: parent::getAccessTokenResourceOwnerId(); } protected function getScopeSeparator() { return $this->scopeSeparator ?: parent::getScopeSeparator(); } protected function getPkceMethod() { return $this->pkceMethod ?: parent::getPkceMethod(); } protected function checkResponse(ResponseInterface $response, $data) { if (!empty($data[$this->responseError])) { $error = $data[$this->responseError]; if (!is_string($error)) { $error = var_export($error, true); } $code = $this->responseCode && !empty($data[$this->responseCode])? $data[$this->responseCode] : 0; if (!is_int($code)) { $code = intval($code); } throw new IdentityProviderException($error, $code, $data); } } protected function createResourceOwner(array $response, AccessToken $token) { return new GenericResourceOwner($response, $this->responseResourceOwnerId); } } 