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>
<component name="PhpIncludePathManager"> <component name="PhpIncludePathManager">
<include_path> <include_path>
<path value="/usr/share/ncc" />
<path value="/usr/share/php" /> <path value="/usr/share/php" />
</include_path> </include_path>
</component> </component>

View file

@ -473,23 +473,23 @@
{ {
switch($authentication->getAuthenticationType()) switch($authentication->getAuthenticationType())
{ {
case AuthenticationType::ACCESS_TOKEN->value: case AuthenticationType::ACCESS_TOKEN:
if($authentication instanceof AccessToken) if($authentication instanceof AccessToken)
{ {
$headers[] = 'Authorization: token ' . $authentication->getAccessToken(); $headers[] = 'Authorization: token ' . $authentication->getAccessToken();
break; 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) if($authentication instanceof UsernamePassword)
{ {
curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword()); curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword());
break; 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; return $headers;

View file

@ -453,22 +453,22 @@
{ {
switch($authentication->getAuthenticationType()) switch($authentication->getAuthenticationType())
{ {
case AuthenticationType::ACCESS_TOKEN->value: case AuthenticationType::ACCESS_TOKEN:
if($authentication instanceof AccessToken) if($authentication instanceof AccessToken)
{ {
$headers[] = 'Authorization: Bearer ' . $authentication->getAccessToken(); $headers[] = 'Authorization: Bearer ' . $authentication->getAccessToken();
break; 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) if($authentication instanceof UsernamePassword)
{ {
curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword()); curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword());
break; 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; return $headers;

View file

@ -478,7 +478,7 @@
break; 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->value:
if($authentication instanceof UsernamePassword) if($authentication instanceof UsernamePassword)
@ -487,7 +487,7 @@
break; 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; return $headers;

View file

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

View file

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

View file

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

View file

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

View file

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