mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
move oauth to new branch
This commit is contained in:
parent
f5d41b0d81
commit
4d0215580c
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Grant;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class AuthorizationCode extends GrantAbstract
|
||||
{
|
||||
protected function getName() : string
|
||||
{
|
||||
return 'authorization_code';
|
||||
}
|
||||
|
||||
protected function getRequiredRequestParameters() : array
|
||||
{
|
||||
return ['code'];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Grant;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class ClientCredentials extends GrantAbstract
|
||||
{
|
||||
protected function getName() : string
|
||||
{
|
||||
return 'client_credentials';
|
||||
}
|
||||
|
||||
protected function getRequiredRequestParameters() : array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Grant;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract class GrantAbstract
|
||||
{
|
||||
abstract protected function getName() : string;
|
||||
|
||||
abstract protected function getRequiredRequestParameters() : array;
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getName();
|
||||
}
|
||||
|
||||
public function prepareRequestParamters(array $defaults, array $options) : array
|
||||
{
|
||||
$defaullts['grant_type'] = $this->getName();
|
||||
|
||||
$required = $this->getRequiredRequestParameters();
|
||||
$provided = \array_merge($defaults, $options);
|
||||
|
||||
foreach ($required as $name) {
|
||||
if (!isset($provided[$name])) {
|
||||
throw new \Exception();
|
||||
}
|
||||
}
|
||||
|
||||
return $provided;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Grant;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class GrantFactory
|
||||
{
|
||||
protected array $registry = [];
|
||||
|
||||
public function setGrant(string $name, GrantAbstract $grant) : self
|
||||
{
|
||||
$this->registry[$name] = $grant;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGrant(string $name) : GrantAbstract
|
||||
{
|
||||
if (!isset($this->registry[$name])) {
|
||||
$this->registerDefaultGrant($name);
|
||||
}
|
||||
|
||||
return $this->registry[$name];
|
||||
}
|
||||
|
||||
protected function registerDefaultGrant(string $name) : self
|
||||
{
|
||||
$class = \str_replace(' ', '', \ucwords(\str_replace(['-', '_'], ' ', $name)));
|
||||
$class = 'phpOMS\\Auth\\OAuth2\\Grant\\' . $class;
|
||||
|
||||
if (!\is_subclass_of($class, GrantAbstract::class)) {
|
||||
throw new \Exception();
|
||||
}
|
||||
|
||||
return $this->setGrant($name, new $class());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Grant;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Password extends GrantAbstract
|
||||
{
|
||||
protected function getName() : string
|
||||
{
|
||||
return 'password';
|
||||
}
|
||||
|
||||
protected function getRequiredRequestParameters() : array
|
||||
{
|
||||
return [
|
||||
'username',
|
||||
'password',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Grant;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Grant
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class RefreshToken extends GrantAbstract
|
||||
{
|
||||
protected function getName() : string
|
||||
{
|
||||
return 'refresh_token';
|
||||
}
|
||||
|
||||
protected function getRequiredRequestParameters() : array
|
||||
{
|
||||
return ['refresh_token'];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\OptionProvider
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\OptionProvider;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\OptionProvider
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class HttpBasicAuthOptionProvider extends PostAuthOptionProvider
|
||||
{
|
||||
public function getAccessTokenOptions(string $method, array $params) : array
|
||||
{
|
||||
if (!isset($params['client_id'], $params['client_secret'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$encoded = \base64_encode($params['client_id'] . ':' . $params['client_secret']);
|
||||
unset($params['client_id'], $params['client_secret']);
|
||||
|
||||
$options = parent::getAccessTokenOptions($method, $params);
|
||||
$options['headers']['Authorization'] = 'Basic ' . $encoded;
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\OptionProvider
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\OptionProvider;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\OptionProvider
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
interface OptionProviderInterface
|
||||
{
|
||||
public function getAccessTokenOptions(string $method, array $params) : array;
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\OptionProvider
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\OptionProvider;
|
||||
|
||||
use phpOMS\Message\Http\RequestMethod;
|
||||
use phpOMS\System\MimeType;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\OptionProvider
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class PostAuthOptionProvider implements OptionProviderInterface
|
||||
{
|
||||
public function getAccessTokenOptions(string $method, array $params) : array
|
||||
{
|
||||
$options = [
|
||||
'headers' => ['content-type' => MimeType::M_POST],
|
||||
];
|
||||
|
||||
if ($method === RequestMethod::POST) {
|
||||
$options['body'] = $this->getAccessTokenBody($params);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
protected function getAccessTokenBody(array $params) : string
|
||||
{
|
||||
return \http_build_query($params, '', '&', \PHP_QUERY_RFC3986);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Provider
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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;
|
||||
|
||||
use phpOMS\Auth\OAuth2\Token\AccessToken;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Provider
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class GeneralProvider extends ProviderAbstract
|
||||
{
|
||||
private string $urlAuthorize;
|
||||
|
||||
private string $urlAccessToken;
|
||||
|
||||
private string $urlResourceOwnerDetails;
|
||||
|
||||
private string $accessTokenMethod;
|
||||
|
||||
private string $accessTokenResourceOwnerId;
|
||||
|
||||
private ?array $scopes = null;
|
||||
|
||||
private string $scopeSeparator;
|
||||
|
||||
private string $responseCode;
|
||||
|
||||
private string $responseResourceOwnerId = 'id';
|
||||
|
||||
public function __construct(array $options = [], array $collaborators = [])
|
||||
{
|
||||
if (!isset($options['urlAuthorize'], $options['urlAccessToken'], $options['urlResourceOwnerDetails'])) {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
foreach ($options as $key => $option) {
|
||||
if (\property_exists($this, $key)) {
|
||||
$this->{$key} = $option;
|
||||
}
|
||||
}
|
||||
|
||||
parent::__construct([], $collaborators);
|
||||
}
|
||||
|
||||
public function getBaseAuthorizationUrl() : string
|
||||
{
|
||||
return $this->urlAuthorize;
|
||||
}
|
||||
|
||||
public function getBaseAccessTokenUrl(array $params = []) : string
|
||||
{
|
||||
return $this->urlAccessToken;
|
||||
}
|
||||
|
||||
public function getResourceOwnerDetailsUrl(AccessToken $token) : string
|
||||
{
|
||||
return $this->urlResourceOwnerDetails;
|
||||
}
|
||||
|
||||
public function getDefaultScopes() : array
|
||||
{
|
||||
return $this->scopes;
|
||||
}
|
||||
|
||||
protected function getAccessTokenMethod() : string
|
||||
{
|
||||
return $this->accessTokenMethod ?: parent::getAccessTokenMethod();
|
||||
}
|
||||
|
||||
protected function getAccessTokenResourceOwnerId() : string
|
||||
{
|
||||
return $this->accessTokenResourceOwnerId ?: parent::getAccessTokenResourceOwnerId();
|
||||
}
|
||||
|
||||
protected function getScopeSeparator() : string
|
||||
{
|
||||
return $this->scopeSeparator ?: parent::getScopeSeparator();
|
||||
}
|
||||
|
||||
protected function createResourceOwner(array $response, AccessToken $token) : GeneralResourceOwner
|
||||
{
|
||||
return new GeneralResourceOwner($response, $this->responseResourceOwnerId);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Provider
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Provider
|
||||
* @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() : string
|
||||
{
|
||||
return $this->response[$this->resourceOwnerId];
|
||||
}
|
||||
|
||||
public function toArray() : array
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,319 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Provider
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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;
|
||||
|
||||
use phpOMS\Auth\OAuth2\Grant\GrantAbstract;
|
||||
use phpOMS\Auth\OAuth2\Grant\GrantFactory;
|
||||
use phpOMS\Auth\OAuth2\OptionProvider\OptionProviderInterface;
|
||||
use phpOMS\Auth\OAuth2\OptionProvider\PostAuthOptionProvider;
|
||||
use phpOMS\Auth\OAuth2\Token\AccessToken;
|
||||
use phpOMS\Auth\OAuth2\Token\AccessTokenInterface;
|
||||
use phpOMS\Message\Http\HttpRequest;
|
||||
use phpOMS\Message\Http\HttpResponse;
|
||||
use phpOMS\Message\Http\RequestMethod;
|
||||
use phpOMS\Uri\UriFactory;
|
||||
use phpOMS\Utils\ArrayUtils;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Provider
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract class ProviderAbstract
|
||||
{
|
||||
protected const ACCESS_TOKEN_RESOURCE_OWNER_ID = null;
|
||||
|
||||
protected string $clientId;
|
||||
|
||||
protected string $clientSecret;
|
||||
|
||||
protected string $redirectUri;
|
||||
|
||||
protected string $state;
|
||||
|
||||
protected GrantFactory $grantFactory;
|
||||
|
||||
protected ReuqestFactory $requestFactory;
|
||||
|
||||
protected OptionProviderInterface $optionProvider;
|
||||
|
||||
public function __construct(array $options = [], array $collaborators = [])
|
||||
{
|
||||
foreach ($options as $key => $option) {
|
||||
if (\property_exists($this, $key)) {
|
||||
$this->{$key} = $option;
|
||||
}
|
||||
}
|
||||
|
||||
$this->setGrantFactory($collaborators['grantFactory'] ?? new GrantFactory());
|
||||
$this->setRequestFactory($collaborators['requestFactory'] ?? new RequestFactory());
|
||||
$this->setOptionProvider($collaborators['optionProvider'] ?? new PostAuthOptionProvider());
|
||||
}
|
||||
|
||||
public function setGrantFactory(GrantFactory $factory) : self
|
||||
{
|
||||
$this->grantFactory = $factory;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGrantFactory() : GrantFactory
|
||||
{
|
||||
return $this->grantFactory;
|
||||
}
|
||||
|
||||
public function setRequestFactory(RequestFactory $factory) : self
|
||||
{
|
||||
$this->requestFactory = $factory;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRequestFactory() : RequestFactory
|
||||
{
|
||||
return $this->requestFactory;
|
||||
}
|
||||
|
||||
public function setOptionProvider(OptionProviderInterface $provider) : self
|
||||
{
|
||||
$this->optionProvider = $provider;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getOptionProvider() : OptionProviderInterface
|
||||
{
|
||||
return $this->optionProvider;
|
||||
}
|
||||
|
||||
public function getState() : string
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
abstract public function getBaseAuthorizationUrl() : string;
|
||||
|
||||
abstract public function getBaseAccessTokenUrl(array $params = []) : string;
|
||||
|
||||
abstract public function getResourceOwnerDetailsUrl(AccessToken $token) : string;
|
||||
|
||||
protected function getRandomState(int $length = 32) : string
|
||||
{
|
||||
return \bin2hex(\random_bytes($length / 2));
|
||||
}
|
||||
|
||||
abstract protected function getDefaultScopes() : array;
|
||||
|
||||
protected function getScopeSeparator() : string
|
||||
{
|
||||
return ',';
|
||||
}
|
||||
|
||||
protected function getAuthorizationParameters(array $options) : array
|
||||
{
|
||||
$options['state'] ??= $this->getRandomState();
|
||||
$options['scope'] ??= $this->getDefaultScopes();
|
||||
|
||||
$this->state = $options['state'];
|
||||
|
||||
$options += [
|
||||
'response_type' => 'code',
|
||||
'approval_prompt' => 'auto',
|
||||
];
|
||||
|
||||
if (\is_array($options['scope'])) {
|
||||
$options['scope'] = \implode($this->getScopeSeparator(), $options['scope']);
|
||||
}
|
||||
|
||||
$options['redirect_uri'] ??= $this->redirectUri;
|
||||
$options['client_id'] = $this->clientId;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
protected function getAuthorizationQuery(array $params) : string
|
||||
{
|
||||
return \http_build_query($params, '', '&', \PHP_QUERY_RFC3986);
|
||||
}
|
||||
|
||||
public function getauthorizationUrl(array $options = []) : string
|
||||
{
|
||||
$base = $this->getBaseAuthorizationUrl();
|
||||
$params = $this->getAuthorizationParameters($options);
|
||||
$query = $this->getAuthorizationQuery($params);
|
||||
|
||||
return UriFactory::build($base . '?' . $query);
|
||||
}
|
||||
|
||||
public function authorize(array $options = [], callable $redirectHandler = null)
|
||||
{
|
||||
$url = $this->getAuthorizationUrl($options);
|
||||
if ($redirectHandler !== null) {
|
||||
return $redirectHandler($url, $this);
|
||||
}
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
\header('Location: ' . $url);
|
||||
exit;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
protected function getAccessTokenMethod() : string
|
||||
{
|
||||
return RequestMethod::POST;
|
||||
}
|
||||
|
||||
protected function getAccessTokenResourceOwnerId() : ?string
|
||||
{
|
||||
return static::ACCESS_TOKEN_RESOURCE_OWNER_ID;
|
||||
}
|
||||
|
||||
protected function getAccessTokenUrl(array $params) : string
|
||||
{
|
||||
$url = $this->getBaseAccessTokenUrl($params);
|
||||
|
||||
if ($this->getAccessTokenMethod() === RequestMethod::GET) {
|
||||
$query = \http_build_query($params, '', '&', \PHP_QUERY_RFC3986);
|
||||
|
||||
return UriFactory::build($url . '?' . $query);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
protected function getAccessTokenRequest(array $params) : HttpRequest
|
||||
{
|
||||
$method = $this->getAccessTokenMethod();
|
||||
$url = $this->getAccessTokenUrl($params);
|
||||
$options = $this->getoptionProvider->getAccessTokenOptions($this->getAccessTokenMethod(), $params);
|
||||
|
||||
return $this->createRequest($method, $url, null, $options);
|
||||
}
|
||||
|
||||
// string | Grant
|
||||
public function getAccessToken($grant, array $options = []) : AccessTokenInterface
|
||||
{
|
||||
$grant = \is_string($grant) ? $this->grantFactory->getGrant($grant) : $grant;
|
||||
|
||||
$params = [
|
||||
'client_id' => $this->clientId,
|
||||
'client_secret' => $this->clientSecret,
|
||||
'redirect_uri' => $this->redirectUri,
|
||||
];
|
||||
|
||||
$params = $grant->prepareRequestParameters($params, $options);
|
||||
$request = $this->getAccessTokenRequest($params);
|
||||
$response = $this->getParsedResponse($request);
|
||||
|
||||
$prepared = $this->prepareAccessTokenResponse($response);
|
||||
$token = $this->createAccessToken($prepared, $grant);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
public function createRequest(string $method, string $url, $token, array $options) : HttpRequest
|
||||
{
|
||||
$defaults = [
|
||||
'headers' => $this->getHeaders($token),
|
||||
];
|
||||
|
||||
$options = \array_merge_recursive($defaults, $options);
|
||||
$factory = $this->getRequestFactory();
|
||||
|
||||
return $factory->getRequestWithOptions($method, $url, $options);
|
||||
}
|
||||
|
||||
public function getParsedResponse(HttpRequest $request)
|
||||
{
|
||||
$response = $request->rest();
|
||||
$parsed = $this->parseResponse($response);
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
protected function parseResponse(HttpResponse $response) : array
|
||||
{
|
||||
$content = $response->getBody();
|
||||
$type = \implode(';', (array) $response->header->get('Content-Type'));
|
||||
|
||||
if (\stripos($type, 'urlencoded') !== false) {
|
||||
\parse_str($content, $parsed);
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
try {
|
||||
return \json_decode($content, true);
|
||||
} catch (\Throwable $t) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// todo: consider to make bool
|
||||
|
||||
protected function prepareAccessTokenResponse(array $result) : array
|
||||
{
|
||||
if (($id = $this->getAccesstokenResourceOwnerId()) !== null) {
|
||||
$result['resource_owner_id'] = ArrayUtils::getArray($id, $result, '.');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function createAccessToken(array $response, GrantAbstract $grant) : AccessTokenInterface
|
||||
{
|
||||
return new AccessToken($response);
|
||||
}
|
||||
|
||||
abstract protected function createResourceOwner(array $response, AccessToken $token) : ResourceOwnerInterface;
|
||||
|
||||
public function getResourceOwner(AccessToken $token) : ResourceOwnerInterface
|
||||
{
|
||||
$response = $this->fetchResourceOwnerDetails($token);
|
||||
|
||||
return $this->createResourceOwner($response, $token);
|
||||
}
|
||||
|
||||
protected function fetchResourceOwnerDetails(AccessToken $token)
|
||||
{
|
||||
$url = $this->getResourceOwnerDetailsUrl($token);
|
||||
$request = $this->createRequest(RequestMethod::GET, $url, $token, []);
|
||||
$response = $this->getParsedResponse($request);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
protected function getDefaultHeaders() : array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function getAuthorizationHeaders($token = null) : array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getHeaders($token = null) : array
|
||||
{
|
||||
return $token === null
|
||||
? $this->getDefaultHeaders()
|
||||
: \array_merge($this->getDefaultHeaders(), $this->getAuthorizationHeaders());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Provider
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Provider
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
interface ResourceOwnerInterface
|
||||
{
|
||||
/**
|
||||
* Get id
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function getId() : string;
|
||||
|
||||
/**
|
||||
* Serialize as array
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function toArray() : array;
|
||||
}
|
||||
|
|
@ -1,123 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Token
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Auth\OAuth2\Token;
|
||||
|
||||
/**
|
||||
* Access token class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Token
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInterface
|
||||
{
|
||||
protected string $accessToken;
|
||||
|
||||
protected int $expires = -1;
|
||||
|
||||
protected ?string $refreshToken = null;
|
||||
|
||||
protected ?string $resourceOwnerId = null;
|
||||
|
||||
protected array $values = [];
|
||||
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!isset($options['access_token'])) {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
$this->accessToken = $options['access_token'];
|
||||
|
||||
if (isset($options['resource_owner_id'])) {
|
||||
$this->resourceOwnerId = $options['resource_owner_id'];
|
||||
}
|
||||
|
||||
if (isset($options['refresh_token'])) {
|
||||
$this->refreshToken = $options['refresh_token'];
|
||||
}
|
||||
|
||||
if (isset($options['expires_in'])) {
|
||||
$this->expires = $options['expires_in'] !== 0 ? \time() + $options['expires_in'] : 0;
|
||||
} elseif (!empty($options['expires'])) {
|
||||
$this->expires = $options['expires'];
|
||||
}
|
||||
|
||||
$this->values = \array_diff_key($options, \array_flip([
|
||||
'access_token',
|
||||
'resource_owner_id',
|
||||
'refresh_token',
|
||||
'expires_in',
|
||||
'expires',
|
||||
]));
|
||||
}
|
||||
|
||||
public function getToken() : string
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
public function getExpires() : int
|
||||
{
|
||||
return $this->expires;
|
||||
}
|
||||
|
||||
public function getRefreshToken() : ?string
|
||||
{
|
||||
return $this->refreshToken;
|
||||
}
|
||||
|
||||
public function getResourceOwnerId() : ?string
|
||||
{
|
||||
return $this->resourceOwnerId;
|
||||
}
|
||||
|
||||
public function hasExpired() : bool
|
||||
{
|
||||
return $this->expires > 0 && $this->expires < \time();
|
||||
}
|
||||
|
||||
public function getValues() : array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getToken();
|
||||
}
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
$params = $this->values;
|
||||
$params['access_token'] = $this->accessToken;
|
||||
|
||||
if ($this->refreshToken !== null) {
|
||||
$params['refresh_token'] = $this->refreshToken;
|
||||
}
|
||||
|
||||
if ($this->expires > 0) {
|
||||
$params['expires'] = $this->expires;
|
||||
}
|
||||
|
||||
if ($this->resourceOwnerId !== null) {
|
||||
$params['resource_owner_id'] = $this->resourceOwnerId;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Token
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Token;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Token
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
interface AccessTokenInterface extends \JsonSerializable
|
||||
{
|
||||
public function getToken() : string;
|
||||
|
||||
public function getRefreshToken() : ?string;
|
||||
|
||||
public function getExpires() : int;
|
||||
|
||||
public function hasExpired() : bool;
|
||||
|
||||
public function getValues() : array;
|
||||
|
||||
public function __toString();
|
||||
|
||||
public function jsonSerialize();
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Token
|
||||
* @copyright Dennis Eichhorn
|
||||
* @copyright MIT - Copyright (c) 2013-2018 Alex Bilbie <hello@alexbilbie.com> - thephpleague/oauth2-client
|
||||
* @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\Token;
|
||||
|
||||
/**
|
||||
* Provider class.
|
||||
*
|
||||
* @package phpOMS\Auth\OAuth2\Token
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
interface ResourceOwnerAccessTokenInterface extends AccessTokenInterface
|
||||
{
|
||||
public function getResourceOwnerId() : ?string;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user