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;
}
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());
}

View File

@ -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);
}

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\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;

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