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.
55 lines
1.5 KiB
55 lines
1.5 KiB
<?php
|
|
|
|
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
|
|
*/
|
|
public abstract 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 function(callable $handler) use ($auth) : callable {
|
|
/**
|
|
* @param \Psr\Http\Message\RequestInterface $request
|
|
* @param mixed[] $options
|
|
* @return mixed
|
|
*/
|
|
return 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]));
|
|
}
|
|
}
|
|
|