Refactor authentication type handling

This commit is contained in:
netkas 2024-09-18 13:36:08 -04:00
parent 04a85900fd
commit 68cfed8a05
9 changed files with 23 additions and 23 deletions

1
.idea/php.xml generated
View file

@ -11,7 +11,6 @@
</component>
<component name="PhpIncludePathManager">
<include_path>
<path value="/usr/share/ncc" />
<path value="/usr/share/php" />
</include_path>
</component>

View file

@ -473,23 +473,23 @@
{
switch($authentication->getAuthenticationType())
{
case AuthenticationType::ACCESS_TOKEN->value:
case AuthenticationType::ACCESS_TOKEN:
if($authentication instanceof AccessToken)
{
$headers[] = 'Authorization: token ' . $authentication->getAccessToken();
break;
}
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()));
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()->value));
case AuthenticationType::USERNAME_PASSWORD->value:
case AuthenticationType::USERNAME_PASSWORD:
if($authentication instanceof UsernamePassword)
{
curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword());
break;
}
throw new AuthenticationException(sprintf('Invalid authentication type for Username/Password, got %s instead', $authentication->getAuthenticationType()));
throw new AuthenticationException(sprintf('Invalid authentication type for Username/Password, got %s instead', $authentication->getAuthenticationType()->value));
}
return $headers;

View file

@ -453,22 +453,22 @@
{
switch($authentication->getAuthenticationType())
{
case AuthenticationType::ACCESS_TOKEN->value:
case AuthenticationType::ACCESS_TOKEN:
if($authentication instanceof AccessToken)
{
$headers[] = 'Authorization: Bearer ' . $authentication->getAccessToken();
break;
}
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()));
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()->value));
case AuthenticationType::USERNAME_PASSWORD->value:
case AuthenticationType::USERNAME_PASSWORD:
if($authentication instanceof UsernamePassword)
{
curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword());
break;
}
throw new AuthenticationException(sprintf('Invalid authentication type for Username/Password, got %s instead', $authentication->getAuthenticationType()));
throw new AuthenticationException(sprintf('Invalid authentication type for Username/Password, got %s instead', $authentication->getAuthenticationType()->value));
}
return $headers;

View file

@ -478,7 +478,7 @@
break;
}
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()));
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()->value));
case AuthenticationType::USERNAME_PASSWORD->value:
if($authentication instanceof UsernamePassword)
@ -487,7 +487,7 @@
break;
}
throw new AuthenticationException(sprintf('Invalid authentication type for Username/Password, got %s instead', $authentication->getAuthenticationType()));
throw new AuthenticationException(sprintf('Invalid authentication type for Username/Password, got %s instead', $authentication->getAuthenticationType()->value));
}
return $headers;

View file

@ -22,12 +22,14 @@
namespace ncc\Interfaces;
use ncc\Enums\Types\AuthenticationType;
interface AuthenticationInterface extends BytecodeObjectInterface
{
/**
* @return string
* @return AuthenticationType
*/
public function getAuthenticationType(): string;
public function getAuthenticationType(): AuthenticationType;
/**
* @return string

View file

@ -177,10 +177,11 @@
$input = [];
switch($entry->getPassword()->getAuthenticationType())
{
case AuthenticationType::USERNAME_PASSWORD->value:
case AuthenticationType::USERNAME_PASSWORD:
$input = ['password' => $password];
break;
case AuthenticationType::ACCESS_TOKEN->value:
case AuthenticationType::ACCESS_TOKEN:
$input = ['token' => $password];
break;
}

View file

@ -103,7 +103,7 @@
switch($this->password->getAuthenticationType())
{
case AuthenticationType::USERNAME_PASSWORD->value:
case AuthenticationType::USERNAME_PASSWORD:
if(!($this->password instanceof UsernamePassword))
{
return false;
@ -129,7 +129,7 @@
return $username === $this->password->getUsername() && $password === $this->password->getPassword();
case AuthenticationType::ACCESS_TOKEN->value:
case AuthenticationType::ACCESS_TOKEN:
if(!($this->password instanceof AccessToken))
{
return false;

View file

@ -67,10 +67,9 @@
/**
* @inheritDoc
*/
public function getAuthenticationType(): string
public function getAuthenticationType(): AuthenticationType
{
// TODO: Could return the enum case here
return AuthenticationType::ACCESS_TOKEN->value;
return AuthenticationType::ACCESS_TOKEN;
}
/**

View file

@ -94,10 +94,9 @@
/**
* @inheritDoc
*/
public function getAuthenticationType(): string
public function getAuthenticationType(): AuthenticationType
{
// TODO: Could return the enum here
return AuthenticationType::USERNAME_PASSWORD->value;
return AuthenticationType::USERNAME_PASSWORD;
}
/**