You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.6 KiB
57 lines
1.6 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace authkit2\Oidc\Authentication;
|
|
|
|
/**
|
|
* Abstract provider for signing/authenticating http requests.
|
|
*/
|
|
abstract class Authentication
|
|
{
|
|
/**
|
|
* Authenticate the passed in HTTP request.
|
|
*
|
|
* @param \Psr\Http\Message\RequestInterface $request request to authenticate
|
|
* @return \Psr\Http\Message\RequestInterface authenticated request
|
|
*/
|
|
abstract public function authenticate(\Psr\Http\Message\RequestInterface $request): \Psr\Http\Message\RequestInterface;
|
|
|
|
/**
|
|
* Wrap this provider up as a middleware appropriate for use directly with
|
|
* Guzzle's handler stack.
|
|
*
|
|
* @return callable
|
|
*/
|
|
public function getMiddleware(): callable
|
|
{
|
|
$auth = $this;
|
|
return static function(callable $handler) use ($auth): callable {
|
|
/**
|
|
* @param \Psr\Http\Message\RequestInterface $request
|
|
* @param mixed[] $options
|
|
* @return mixed
|
|
*/
|
|
return static function(\Psr\Http\Message\RequestInterface $request, array $options) use ($handler, $auth) {
|
|
return $handler(
|
|
$auth->authenticate($request),
|
|
$options
|
|
);
|
|
};
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Fetch a guzzle client with the authentication middleware included.
|
|
*
|
|
* @param mixed[] $options options to pass through to the guzzle client
|
|
* @return \GuzzleHttp\Client
|
|
*/
|
|
public function getClient(array $options = []): \GuzzleHttp\Client
|
|
{
|
|
$stack = new \GuzzleHttp\HandlerStack();
|
|
$stack->setHandler(new \GuzzleHttp\Handler\CurlHandler());
|
|
$stack->push($this->getMiddleware());
|
|
return new \GuzzleHttp\Client(array_merge($options, ['handler' => $stack]));
|
|
}
|
|
}
|
|
|