- Added Oauth2 authentication. Fixed non-working bearer token also.

This commit is contained in:
Dave Mc Nicoll committed 2024-04-09 19:13:53 -04:00
1 parent f035fdf0a2
commit ce4d6cdf7c
7 files changed
+105 -32

No files matched your search

+15 -19
View File
@@ -2,6 +2,7 @@
namespace Ulmus\Api\Adapter;
use Negundo\Client\Transport\Curl;
use Ulmus\Adapter\AdapterInterface;
use Ulmus\Api\ApiRepository;
use Ulmus\Api\Common\AuthenticationEnum;
@@ -13,13 +14,9 @@ use Ulmus\Ulmus;
class Rest implements AdapterInterface
{
protected AuthenticationEnum $auth;
protected CurlAuthorization $authorization;
protected string $username;
protected string $password;
protected string $token;
protected AuthenticationEnum $authorizationMethod;
protected array $headers = [];
@@ -27,9 +24,20 @@ class Rest implements AdapterInterface
public readonly string $url;
public function setup(array $configuration): void
{
$this->url = rtrim($configuration['url'], '/');
$this->authorizationMethod = AuthenticationEnum::from($configuration['auth'] ?: AuthenticationEnum::Basic->value);
$this->authorization = ( new CurlAuthorization() )->fromAuthenticationEnum($this->authorizationMethod, $configuration);
$this->headers = $configuration['headers'] ?? [];
$this->options = $configuration['options'] ?? [];
}
public function connect(): object
{
return new CurlClient($this->headers, $this->options, ( new CurlAuthorization() )->fromAuthenticationEnum($this->auth, $this->username ?? false, $this->password ?? false, $this->token ?? false) );
return new CurlClient($this->headers, $this->options, $this->authorization);
}
public function buildDataSourceName(): string
@@ -37,18 +45,6 @@ class Rest implements AdapterInterface
return "https";
}
public function setup(array $configuration): void
{
$this->auth = AuthenticationEnum::from($configuration['auth'] ?: AuthenticationEnum::Basic->value);
$this->url = rtrim($configuration['url'], '/');
foreach([ 'username', 'password', 'token', 'headers', 'options' ] as $conf) {
if ($configuration[$conf] ?? false) {
$this->$conf = $configuration[$conf];
}
}
}
public function writableValue(mixed $value) : mixed
{
switch (true) {