some minor cleanup, still not implemented

This commit is contained in:
Dennis Eichhorn 2020-09-26 12:08:31 +02:00
parent 49135aefab
commit 781366694c
6 changed files with 114 additions and 28 deletions

View File

@ -34,7 +34,7 @@ class GrantFactory
return $this; return $this;
} }
public function getGrant(string $name) : AbstractGrant public function getGrant(string $name) : GrantAbstract
{ {
if (!isset($this->registry[$name])) { if (!isset($this->registry[$name])) {
$this->registerDefaultGrant($name); $this->registerDefaultGrant($name);
@ -45,10 +45,12 @@ class GrantFactory
protected function registerDefaultGrant(string $name) : self 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; $class = 'phpOMS\\OAuth2\\Grant\\' . $class;
$this->checkGrant($class); if (!\is_subclass_of($class, GrantAbstract::class)) {
throw new \Exception();
}
return $this->setGrant($name, new $class()); return $this->setGrant($name, new $class());
} }

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace phpOMS\Auth\OAuth2\Provider; namespace phpOMS\Auth\OAuth2\Provider;
use phpOMS\Auth\OAuth2\AccessToken; use phpOMS\Auth\OAuth2\Token\AccessToken;
/** /**
* Provider class. * Provider class.
@ -70,27 +70,32 @@ class GeneralProvider extends ProviderAbstract
return $this->urlAccessToken; return $this->urlAccessToken;
} }
public function getResourceOwnerDetailsUrl(AccessToken $token) : string
{
return $this->urlResourceOwnerDetails;
}
public function getDefaultScopes() : array public function getDefaultScopes() : array
{ {
return $this->scopes; return $this->scopes;
} }
private function getAccessTokenMethod() : string protected function getAccessTokenMethod() : string
{ {
return $this->accessTokenMethod ?: parent::getAccessTokenMethod(); return $this->accessTokenMethod ?: parent::getAccessTokenMethod();
} }
private function getAccessTokenResourceOwnerId() : string protected function getAccessTokenResourceOwnerId() : string
{ {
return $this->accessTokenResourceOwnerId ?: parent::getAccessTokenResourceOwnerId(); return $this->accessTokenResourceOwnerId ?: parent::getAccessTokenResourceOwnerId();
} }
private function getScopeSeparator() : string protected function getScopeSeparator() : string
{ {
return $this->scopeSeparator ?: parent::getScopeSeparator(); 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); return new GeneralResourceOwner($response, $this->responseResourceOwnerId);
} }

View File

@ -0,0 +1,47 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Auth\OAuth2
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
* @see https://tools.ietf.org/html/rfc6749
*/
declare(strict_types=1);
namespace phpOMS\Auth\OAuth2\Provider;
/**
* Provider class.
*
* @package phpOMS\Auth\OAuth2
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class GeneralResourceOwner implements ResourceOwnerInterface
{
protected array $response;
protected string $resourceOwnerId;
public function __construct(array $response, string $resourceOwnerId)
{
$this->response = $response;
$this->resourceOwnerId = $resourceOwnerId;
}
public function getId()
{
return $this->response[$this->resourceOwnerId];
}
public function toArray() : array
{
return $this->response;
}
}

View File

@ -19,6 +19,13 @@ use phpOMS\Message\Http\HttpResponse;
use phpOMS\Message\Http\RequestMethod; use phpOMS\Message\Http\RequestMethod;
use phpOMS\Uri\UriFactory; use phpOMS\Uri\UriFactory;
use phpOMS\Utils\ArrayUtils; 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. * Provider class.
@ -142,7 +149,7 @@ abstract class ProviderAbstract
protected function getAuthorizationQuery(array $params) : string 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 public function getauthorizationUrl(array $options = []) : string
@ -154,10 +161,10 @@ abstract class ProviderAbstract
return UriFactory::build($base . '?' . $query); 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); $url = $this->getAuthorizationUrl($options);
if ($redirectHander !== null) { if ($redirectHandler !== null) {
return $redirectHandler($url, $this); return $redirectHandler($url, $this);
} }
@ -177,21 +184,14 @@ abstract class ProviderAbstract
return static::ACCESS_TOKEN_RESOURCE_OWNER_ID; return static::ACCESS_TOKEN_RESOURCE_OWNER_ID;
} }
protected function verifyGrant($grant) : AbstractGrant
{
$this->grantFactory->checkGrant($grant);
return $grant;
}
protected function getAccessTokenUrl(array $params) : string protected function getAccessTokenUrl(array $params) : string
{ {
$url = $this->getBaseAccessTokenUrl($params); $url = $this->getBaseAccessTokenUrl($params);
if ($this->getAccessTokenMethod() === RequestMethod::GET) { 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; return $url;
@ -209,7 +209,7 @@ abstract class ProviderAbstract
// string | Grant // string | Grant
public function getAccessToken($grant, array $options = []) : AccessTokenInterface 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 = [ $params = [
'client_id' => $this->clientId, 'client_id' => $this->clientId,
@ -244,8 +244,6 @@ abstract class ProviderAbstract
$response = $request->rest(); $response = $request->rest();
$parsed = $this->parseResponse($response); $parsed = $this->parseResponse($response);
$this->checkResponse($response, $parsed);
return $parsed; return $parsed;
} }
@ -267,8 +265,6 @@ abstract class ProviderAbstract
} }
} }
abstract protected function checkResponse(HttpResponse $response, $data) : void;
// todo: consider to make bool // todo: consider to make bool
protected function prepareAccessTokenResponse(array $result) : array protected function prepareAccessTokenResponse(array $result) : array
@ -280,7 +276,7 @@ abstract class ProviderAbstract
return $result; return $result;
} }
protected function createAccessToken(array $response, AbstractGrant $grant) : AccessTokenInterface protected function createAccessToken(array $response, GrantAbstract $grant) : AccessTokenInterface
{ {
return new AccessToken($response); return new AccessToken($response);
} }
@ -297,7 +293,7 @@ abstract class ProviderAbstract
protected function fetchResourceOwnerDetails(AccessToken $token) protected function fetchResourceOwnerDetails(AccessToken $token)
{ {
$url = $this->getResourceOwnerDetailsUrl($token); $url = $this->getResourceOwnerDetailsUrl($token);
$request = $this->getAuthenticatedRequest(RequestMethod::GET, $url, $token); $request = $this->createRequest(RequestMethod::GET, $url, $token, []);
$response = $this->getParsedResponse($request); $response = $this->getParsedResponse($request);
return $response; return $response;

View File

@ -0,0 +1,31 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Auth\OAuth2\OptionProvider
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
* @see https://tools.ietf.org/html/rfc6749
*/
declare(strict_types=1);
namespace phpOMS\Auth\OAuth2\Provider;
/**
* Provider class.
*
* @package phpOMS\Auth\OAuth2\OptionProvider
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
interface ResourceOwnerInterface
{
public function getId();
public function toArray() : array;
}

View File

@ -12,7 +12,7 @@
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace phpOMS\Auth\OAuth2; namespace phpOMS\Auth\OAuth2\Token;
/** /**
* Access token class. * Access token class.
@ -90,6 +90,11 @@ class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInter
return $this->expires < \time(); return $this->expires < \time();
} }
public function getValues(): array
{
return $this->vallues;
}
public function __toString() public function __toString()
{ {
return $this->getToken(); return $this->getToken();