new appraoch

This commit is contained in:
Tim Bendt
2025-11-26 13:22:58 -05:00
parent de3d100844
commit c520b7df89
6760 changed files with 1009780 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
/**
* Provides generic array navigation tools.
*/
trait ArrayAccessorTrait
{
/**
* Returns a value by key using dot notation.
*
* @param array $data
* @param string $key
* @param mixed|null $default
* @return mixed
*/
private function getValueByKey(array $data, $key, $default = null)
{
if (!is_string($key) || empty($key) || !count($data)) {
return $default;
}
if (strpos($key, '.') !== false) {
$keys = explode('.', $key);
foreach ($keys as $innerKey) {
if (!is_array($data) || !array_key_exists($innerKey, $data)) {
return $default;
}
$data = $data[$innerKey];
}
return $data;
}
return array_key_exists($key, $data) ? $data[$key] : $default;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use League\OAuth2\Client\Token\AccessTokenInterface;
/**
* Enables `Bearer` header authorization for providers.
*
* @link http://tools.ietf.org/html/rfc6750 Bearer Token Usage (RFC 6750)
*/
trait BearerAuthorizationTrait
{
/**
* Returns authorization headers for the 'bearer' grant.
*
* @param AccessTokenInterface|string|null $token Either a string or an access token instance
* @return array
*/
protected function getAuthorizationHeaders($token = null)
{
return ['Authorization' => 'Bearer ' . $token];
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
/**
* Provides support for blacklisting explicit properties from the
* mass assignment behavior.
*/
trait GuardedPropertyTrait
{
/**
* The properties that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* Attempts to mass assign the given options to explicitly defined properties,
* skipping over any properties that are defined in the guarded array.
*
* @param array $options
* @return mixed
*/
protected function fillProperties(array $options = [])
{
if (isset($options['guarded'])) {
unset($options['guarded']);
}
foreach ($options as $option => $value) {
if (property_exists($this, $option) && !$this->isGuarded($option)) {
$this->{$option} = $value;
}
}
}
/**
* Returns current guarded properties.
*
* @return array
*/
public function getGuarded()
{
return $this->guarded;
}
/**
* Determines if the given property is guarded.
*
* @param string $property
* @return bool
*/
public function isGuarded($property)
{
return in_array($property, $this->getGuarded());
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;
/**
* Enables `MAC` header authorization for providers.
*
* @link http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-05 Message Authentication Code (MAC) Tokens
*/
trait MacAuthorizationTrait
{
/**
* Returns the id of this token for MAC generation.
*
* @param AccessToken $token
* @return string
*/
abstract protected function getTokenId(AccessToken $token);
/**
* Returns the MAC signature for the current request.
*
* @param string $id
* @param integer $ts
* @param string $nonce
* @return string
*/
abstract protected function getMacSignature($id, $ts, $nonce);
/**
* Returns a new random string to use as the state parameter in an
* authorization flow.
*
* @param int $length Length of the random string to be generated.
* @return string
*/
abstract protected function getRandomState($length = 32);
/**
* Returns the authorization headers for the 'mac' grant.
*
* @param AccessTokenInterface|string|null $token Either a string or an access token instance
* @return array
* @codeCoverageIgnore
*
* @todo This is currently untested and provided only as an example. If you
* complete the implementation, please create a pull request for
* https://github.com/thephpleague/oauth2-client
*/
protected function getAuthorizationHeaders($token = null)
{
if ($token === null) {
return [];
}
$ts = time();
$id = $this->getTokenId($token);
$nonce = $this->getRandomState(16);
$mac = $this->getMacSignature($id, $ts, $nonce);
$parts = [];
foreach (compact('id', 'ts', 'nonce', 'mac') as $key => $value) {
$parts[] = sprintf('%s="%s"', $key, $value);
}
return ['Authorization' => 'MAC ' . implode(', ', $parts)];
}
}

View File

@@ -0,0 +1,122 @@
<?php
namespace League\OAuth2\Client\Tool;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Uri;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
trait ProviderRedirectTrait
{
/**
* Maximum number of times to follow provider initiated redirects
*
* @var integer
*/
protected $redirectLimit = 2;
/**
* Retrieves a response for a given request and retrieves subsequent
* responses, with authorization headers, if a redirect is detected.
*
* @param RequestInterface $request
* @return ResponseInterface
* @throws BadResponseException
*/
protected function followRequestRedirects(RequestInterface $request)
{
$response = null;
$attempts = 0;
while ($attempts < $this->redirectLimit) {
$attempts++;
$response = $this->getHttpClient()->send($request, [
'allow_redirects' => false
]);
if ($this->isRedirect($response)) {
$redirectUrl = new Uri($response->getHeader('Location')[0]);
$request = $request->withUri($redirectUrl);
} else {
break;
}
}
return $response;
}
/**
* Returns the HTTP client instance.
*
* @return GuzzleHttp\ClientInterface
*/
abstract public function getHttpClient();
/**
* Retrieves current redirect limit.
*
* @return integer
*/
public function getRedirectLimit()
{
return $this->redirectLimit;
}
/**
* Determines if a given response is a redirect.
*
* @param ResponseInterface $response
*
* @return boolean
*/
protected function isRedirect(ResponseInterface $response)
{
$statusCode = $response->getStatusCode();
return $statusCode > 300 && $statusCode < 400 && $response->hasHeader('Location');
}
/**
* Sends a request instance and returns a response instance.
*
* WARNING: This method does not attempt to catch exceptions caused by HTTP
* errors! It is recommended to wrap this method in a try/catch block.
*
* @param RequestInterface $request
* @return ResponseInterface
*/
public function getResponse(RequestInterface $request)
{
try {
$response = $this->followRequestRedirects($request);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
return $response;
}
/**
* Updates the redirect limit.
*
* @param integer $limit
* @return League\OAuth2\Client\Provider\AbstractProvider
* @throws InvalidArgumentException
*/
public function setRedirectLimit($limit)
{
if (!is_int($limit)) {
throw new InvalidArgumentException('redirectLimit must be an integer.');
}
if ($limit < 1) {
throw new InvalidArgumentException('redirectLimit must be greater than or equal to one.');
}
$this->redirectLimit = $limit;
return $this;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
/**
* Provides a standard way to generate query strings.
*/
trait QueryBuilderTrait
{
/**
* Build a query string from an array.
*
* @param array $params
*
* @return string
*/
protected function buildQueryString(array $params)
{
return http_build_query($params, '', '&', \PHP_QUERY_RFC3986);
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use GuzzleHttp\Psr7\Request;
/**
* Used to produce PSR-7 Request instances.
*
* @link https://github.com/guzzle/guzzle/pull/1101
*/
class RequestFactory
{
/**
* Creates a PSR-7 Request instance.
*
* @param null|string $method HTTP method for the request.
* @param null|string $uri URI for the request.
* @param array $headers Headers for the message.
* @param string|resource|StreamInterface $body Message body.
* @param string $version HTTP protocol version.
*
* @return Request
*/
public function getRequest(
$method,
$uri,
array $headers = [],
$body = null,
$version = '1.1'
) {
return new Request($method, $uri, $headers, $body, $version);
}
/**
* Parses simplified options.
*
* @param array $options Simplified options.
*
* @return array Extended options for use with getRequest.
*/
protected function parseOptions(array $options)
{
// Should match default values for getRequest
$defaults = [
'headers' => [],
'body' => null,
'version' => '1.1',
];
return array_merge($defaults, $options);
}
/**
* Creates a request using a simplified array of options.
*
* @param null|string $method
* @param null|string $uri
* @param array $options
*
* @return Request
*/
public function getRequestWithOptions($method, $uri, array $options = [])
{
$options = $this->parseOptions($options);
return $this->getRequest(
$method,
$uri,
$options['headers'],
$options['body'],
$options['version']
);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* This file is part of the league/oauth2-client library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
* @license http://opensource.org/licenses/MIT MIT
* @link http://thephpleague.com/oauth2-client/ Documentation
* @link https://packagist.org/packages/league/oauth2-client Packagist
* @link https://github.com/thephpleague/oauth2-client GitHub
*/
namespace League\OAuth2\Client\Tool;
use BadMethodCallException;
/**
* Provides functionality to check for required parameters.
*/
trait RequiredParameterTrait
{
/**
* Checks for a required parameter in a hash.
*
* @throws BadMethodCallException
* @param string $name
* @param array $params
* @return void
*/
private function checkRequiredParameter($name, array $params)
{
if (!isset($params[$name])) {
throw new BadMethodCallException(sprintf(
'Required parameter not passed: "%s"',
$name
));
}
}
/**
* Checks for multiple required parameters in a hash.
*
* @throws InvalidArgumentException
* @param array $names
* @param array $params
* @return void
*/
private function checkRequiredParameters(array $names, array $params)
{
foreach ($names as $name) {
$this->checkRequiredParameter($name, $params);
}
}
}