mirror of
https://github.com/linuxserver/Heimdall.git
synced 2025-11-13 19:33:09 +09:00
Update to laravel 7
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace Github\HttpClient\Plugin;
|
||||
|
||||
use Github\Client;
|
||||
use Github\AuthMethod;
|
||||
use Github\Exception\RuntimeException;
|
||||
use Http\Client\Common\Plugin;
|
||||
use Http\Promise\Promise;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
@@ -12,13 +13,29 @@ use Psr\Http\Message\RequestInterface;
|
||||
*
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*/
|
||||
class Authentication implements Plugin
|
||||
final class Authentication implements Plugin
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $tokenOrLogin;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $password;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $method;
|
||||
|
||||
public function __construct($tokenOrLogin, $password, $method)
|
||||
/**
|
||||
* @param string $tokenOrLogin GitHub private token/username/client ID
|
||||
* @param string|null $password GitHub password/secret (optionally can contain $method)
|
||||
* @param string|null $method One of the AUTH_* class constants
|
||||
*/
|
||||
public function __construct(string $tokenOrLogin, ?string $password, ?string $method)
|
||||
{
|
||||
$this->tokenOrLogin = $tokenOrLogin;
|
||||
$this->password = $password;
|
||||
@@ -26,60 +43,29 @@ class Authentication implements Plugin
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return Promise
|
||||
*/
|
||||
public function handleRequest(RequestInterface $request, callable $next, callable $first)
|
||||
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
|
||||
{
|
||||
switch ($this->method) {
|
||||
case Client::AUTH_HTTP_PASSWORD:
|
||||
$request = $request->withHeader(
|
||||
'Authorization',
|
||||
sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password))
|
||||
);
|
||||
break;
|
||||
|
||||
case Client::AUTH_HTTP_TOKEN:
|
||||
$request = $request->withHeader('Authorization', sprintf('token %s', $this->tokenOrLogin));
|
||||
break;
|
||||
|
||||
case Client::AUTH_URL_CLIENT_ID:
|
||||
$uri = $request->getUri();
|
||||
$query = $uri->getQuery();
|
||||
|
||||
$parameters = [
|
||||
'client_id' => $this->tokenOrLogin,
|
||||
'client_secret' => $this->password,
|
||||
];
|
||||
|
||||
$query .= empty($query) ? '' : '&';
|
||||
$query .= utf8_encode(http_build_query($parameters, '', '&'));
|
||||
|
||||
$uri = $uri->withQuery($query);
|
||||
$request = $request->withUri($uri);
|
||||
break;
|
||||
|
||||
case Client::AUTH_URL_TOKEN:
|
||||
$uri = $request->getUri();
|
||||
$query = $uri->getQuery();
|
||||
|
||||
$parameters = ['access_token' => $this->tokenOrLogin];
|
||||
|
||||
$query .= empty($query) ? '' : '&';
|
||||
$query .= utf8_encode(http_build_query($parameters, '', '&'));
|
||||
|
||||
$uri = $uri->withQuery($query);
|
||||
$request = $request->withUri($uri);
|
||||
break;
|
||||
|
||||
case Client::AUTH_JWT:
|
||||
$request = $request->withHeader('Authorization', sprintf('Bearer %s', $this->tokenOrLogin));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new RuntimeException(sprintf('%s not yet implemented', $this->method));
|
||||
break;
|
||||
}
|
||||
$request = $request->withHeader(
|
||||
'Authorization',
|
||||
$this->getAuthorizationHeader()
|
||||
);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
private function getAuthorizationHeader(): string
|
||||
{
|
||||
switch ($this->method) {
|
||||
case AuthMethod::CLIENT_ID:
|
||||
return sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password));
|
||||
case AuthMethod::ACCESS_TOKEN:
|
||||
return sprintf('token %s', $this->tokenOrLogin);
|
||||
case AuthMethod::JWT:
|
||||
return sprintf('Bearer %s', $this->tokenOrLogin);
|
||||
default:
|
||||
throw new RuntimeException(sprintf('%s not yet implemented', $this->method));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user