From 781366694cf0fc52f6945317f39f280cc5067ff2 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 26 Sep 2020 12:08:31 +0200 Subject: [PATCH] some minor cleanup, still not implemented --- Auth/OAuth2/Grant/GrantFactory.php | 8 ++-- Auth/OAuth2/Provider/GeneralProvider.php | 15 ++++-- Auth/OAuth2/Provider/GeneralResourceOwner.php | 47 +++++++++++++++++++ Auth/OAuth2/Provider/ProviderAbstract.php | 34 ++++++-------- .../Provider/ResourceOwnerInterface.php | 31 ++++++++++++ Auth/OAuth2/Token/AccessToken.php | 7 ++- 6 files changed, 114 insertions(+), 28 deletions(-) diff --git a/Auth/OAuth2/Grant/GrantFactory.php b/Auth/OAuth2/Grant/GrantFactory.php index 624c4d061..240ff99a4 100644 --- a/Auth/OAuth2/Grant/GrantFactory.php +++ b/Auth/OAuth2/Grant/GrantFactory.php @@ -34,7 +34,7 @@ class GrantFactory return $this; } - public function getGrant(string $name) : AbstractGrant + public function getGrant(string $name) : GrantAbstract { if (!isset($this->registry[$name])) { $this->registerDefaultGrant($name); @@ -45,10 +45,12 @@ class GrantFactory protected function registerDefaultGrant(string $name) : self { - $class = \str_replace(' ', '', \ucwords(\str_replace(['-', '_', ' ', $name]))); + $class = \str_replace(' ', '', \ucwords(\str_replace(['-', '_'], ' ', $name))); $class = 'phpOMS\\OAuth2\\Grant\\' . $class; - $this->checkGrant($class); + if (!\is_subclass_of($class, GrantAbstract::class)) { + throw new \Exception(); + } return $this->setGrant($name, new $class()); } diff --git a/Auth/OAuth2/Provider/GeneralProvider.php b/Auth/OAuth2/Provider/GeneralProvider.php index 9d8ff071c..b4b800762 100644 --- a/Auth/OAuth2/Provider/GeneralProvider.php +++ b/Auth/OAuth2/Provider/GeneralProvider.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace phpOMS\Auth\OAuth2\Provider; -use phpOMS\Auth\OAuth2\AccessToken; +use phpOMS\Auth\OAuth2\Token\AccessToken; /** * Provider class. @@ -70,27 +70,32 @@ class GeneralProvider extends ProviderAbstract return $this->urlAccessToken; } + public function getResourceOwnerDetailsUrl(AccessToken $token) : string + { + return $this->urlResourceOwnerDetails; + } + public function getDefaultScopes() : array { return $this->scopes; } - private function getAccessTokenMethod() : string + protected function getAccessTokenMethod() : string { return $this->accessTokenMethod ?: parent::getAccessTokenMethod(); } - private function getAccessTokenResourceOwnerId() : string + protected function getAccessTokenResourceOwnerId() : string { return $this->accessTokenResourceOwnerId ?: parent::getAccessTokenResourceOwnerId(); } - private function getScopeSeparator() : string + protected function getScopeSeparator() : string { return $this->scopeSeparator ?: parent::getScopeSeparator(); } - private function createResourceOwner(array $response, AccessToken $token) : GeneralResourceOwner + protected function createResourceOwner(array $response, AccessToken $token) : GeneralResourceOwner { return new GeneralResourceOwner($response, $this->responseResourceOwnerId); } diff --git a/Auth/OAuth2/Provider/GeneralResourceOwner.php b/Auth/OAuth2/Provider/GeneralResourceOwner.php index e69de29bb..450f823b3 100644 --- a/Auth/OAuth2/Provider/GeneralResourceOwner.php +++ b/Auth/OAuth2/Provider/GeneralResourceOwner.php @@ -0,0 +1,47 @@ +response = $response; + $this->resourceOwnerId = $resourceOwnerId; + } + + public function getId() + { + return $this->response[$this->resourceOwnerId]; + } + + public function toArray() : array + { + return $this->response; + } +} diff --git a/Auth/OAuth2/Provider/ProviderAbstract.php b/Auth/OAuth2/Provider/ProviderAbstract.php index b6f057318..6da06dae5 100644 --- a/Auth/OAuth2/Provider/ProviderAbstract.php +++ b/Auth/OAuth2/Provider/ProviderAbstract.php @@ -19,6 +19,13 @@ use phpOMS\Message\Http\HttpResponse; use phpOMS\Message\Http\RequestMethod; use phpOMS\Uri\UriFactory; use phpOMS\Utils\ArrayUtils; +use phpOMS\Auth\OAuth2\Grant\GrantFactory; +use phpOMS\Auth\OAuth2\OptionProvider\OptionProviderInterface; +use phpOMS\Auth\OAuth2\Grant\GrantAbstract; +use phpOMS\Auth\OAuth2\Token\AccessToken; +use phpOMS\Auth\OAuth2\Token\AccessTokenInterface; +use phpOMS\Message\Http\HttpRequest; +use phpOMS\Auth\OAuth2\OptionProvider\PostAuthOptionProvider; /** * Provider class. @@ -142,7 +149,7 @@ abstract class ProviderAbstract protected function getAuthorizationQuery(array $params) : string { - return \http_build_query($params, null, '&', \PHP_QUERY_RFC3986); + return \http_build_query($params, '', '&', \PHP_QUERY_RFC3986); } public function getauthorizationUrl(array $options = []) : string @@ -154,10 +161,10 @@ abstract class ProviderAbstract return UriFactory::build($base . '?' . $query); } - public function authorize(array $options = [], callable $redirectHander = null) + public function authorize(array $options = [], callable $redirectHandler = null) { $url = $this->getAuthorizationUrl($options); - if ($redirectHander !== null) { + if ($redirectHandler !== null) { return $redirectHandler($url, $this); } @@ -177,21 +184,14 @@ abstract class ProviderAbstract return static::ACCESS_TOKEN_RESOURCE_OWNER_ID; } - protected function verifyGrant($grant) : AbstractGrant - { - $this->grantFactory->checkGrant($grant); - - return $grant; - } - protected function getAccessTokenUrl(array $params) : string { $url = $this->getBaseAccessTokenUrl($params); if ($this->getAccessTokenMethod() === RequestMethod::GET) { - $query = $this->getAccessTokenQuery($params); + $query = \http_build_query($params, '', '&', \PHP_QUERY_RFC3986); - return UriFactory::build($ur . '?' . $query); + return UriFactory::build($url . '?' . $query); } return $url; @@ -209,7 +209,7 @@ abstract class ProviderAbstract // string | Grant public function getAccessToken($grant, array $options = []) : AccessTokenInterface { - $grant = \is_string($grant) ? $this->grantFactory->getGrant($grant) : $this->verifyGrant(); + $grant = \is_string($grant) ? $this->grantFactory->getGrant($grant) : $grant; $params = [ 'client_id' => $this->clientId, @@ -244,8 +244,6 @@ abstract class ProviderAbstract $response = $request->rest(); $parsed = $this->parseResponse($response); - $this->checkResponse($response, $parsed); - return $parsed; } @@ -267,8 +265,6 @@ abstract class ProviderAbstract } } - abstract protected function checkResponse(HttpResponse $response, $data) : void; - // todo: consider to make bool protected function prepareAccessTokenResponse(array $result) : array @@ -280,7 +276,7 @@ abstract class ProviderAbstract return $result; } - protected function createAccessToken(array $response, AbstractGrant $grant) : AccessTokenInterface + protected function createAccessToken(array $response, GrantAbstract $grant) : AccessTokenInterface { return new AccessToken($response); } @@ -297,7 +293,7 @@ abstract class ProviderAbstract protected function fetchResourceOwnerDetails(AccessToken $token) { $url = $this->getResourceOwnerDetailsUrl($token); - $request = $this->getAuthenticatedRequest(RequestMethod::GET, $url, $token); + $request = $this->createRequest(RequestMethod::GET, $url, $token, []); $response = $this->getParsedResponse($request); return $response; diff --git a/Auth/OAuth2/Provider/ResourceOwnerInterface.php b/Auth/OAuth2/Provider/ResourceOwnerInterface.php index e69de29bb..523db21a4 100644 --- a/Auth/OAuth2/Provider/ResourceOwnerInterface.php +++ b/Auth/OAuth2/Provider/ResourceOwnerInterface.php @@ -0,0 +1,31 @@ +expires < \time(); } + public function getValues(): array + { + return $this->vallues; + } + public function __toString() { return $this->getToken();