- First working version of ulmus-api package; at working state only

This commit is contained in:
mcndave committed 2023-10-26 17:22:52 +00:00
1 parent 0f9495e07a
commit 7e69a09e7f
27 files changed
+1406 -101

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
<?php
namespace Ulmus\Api\Transport;
use Ulmus\Api\Common\AuthenticationEnum;
class CurlAuthorization
{
public array $headers = [];
public array $options = [];
public function fromAuthenticationEnum(AuthenticationEnum $auth, string|false $username, #[\SensitiveParameter] string|false $password, #[\SensitiveParameter] string|false $token,) : static
{
switch($auth) {
case AuthenticationEnum::Bearer:
$this->bearer($token);
break;
case AuthenticationEnum::Key:
$this->key($token);
break;
case AuthenticationEnum::Token:
$this->token($token);
break;
case AuthenticationEnum::Basic:
$this->basic($username, $password);
break;
case AuthenticationEnum::NTLM:
$this->ntlm($username, $password);
break;
case AuthenticationEnum::Negotiate:
$this->ntlm($username, $password);
break;
}
return $this;
}
public function bearer(#[\SensitiveParameter] string $token) : void
{
$this->options[CURLOPT_XOAUTH2_BEARER] = $token;
}
public function apikey(#[\SensitiveParameter] string $token) : void
{
$this->customToken('apikey', $token);
}
public function key(#[\SensitiveParameter] string $token) : void
{
$this->customToken('key', $token);
}
public function token(#[\SensitiveParameter] string $token) : void
{
$this->customToken('token', $token);
}
public function customToken(string $key, #[\SensitiveParameter] string $token) : void
{
$this->headers['Authorization'] = "$key {$token}";
}
public function basic(string $username, #[\SensitiveParameter] string $password) : void
{
$this->userpass(CURLAUTH_BASIC, $username, $password);
}
public function digest(string $username, #[\SensitiveParameter] string $password) : void
{
$this->userpass(CURLAUTH_DIGEST, $username, $password);
}
public function ntlm(string $username, #[\SensitiveParameter] string $password) : void
{
$this->userpass(CURLAUTH_NTLM, $username, $password);
}
public function negotiate(string $username, #[\SensitiveParameter] string $password) : void
{
$this->userpass(CURLAUTH_NEGOTIATE, $username, $password);
}
protected function userpass(int $authType, string $username, #[\SensitiveParameter] $password) : void
{
$this->options[CURLOPT_HTTPAUTH] = $authType;
$this->options[CURLOPT_USERNAME] = $username;
$this->options[CURLOPT_PASSWORD] = $password;
}
}