107 lines
2.1 KiB
PHP
107 lines
2.1 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Entity;
|
|
|
|
use App\Controller\ConfigController;
|
|
use App\Controller\EncryptionController;
|
|
use Exception;
|
|
use SodiumException;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class Apikey
|
|
{
|
|
|
|
public function __construct(
|
|
private int $id = 0,
|
|
private string $name = '',
|
|
private string $apiTokenPrefix = '',
|
|
private string $apiToken = '',
|
|
private readonly string $passphrase = ''
|
|
)
|
|
{
|
|
if ($this->passphrase) {
|
|
$configController = new ConfigController();
|
|
$encryptionController = new EncryptionController();
|
|
|
|
$encryptionKey = $configController->getConfig(configKey: 'encryptionKey');
|
|
|
|
$this->apiTokenPrefix = strtok(string: $this->passphrase, token: '.');
|
|
|
|
try {
|
|
$this->apiToken = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey);
|
|
} catch (Exception|SodiumException $e) {
|
|
die($e->getMessage() . PHP_EOL);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return String
|
|
*/
|
|
public function getApiToken(): string
|
|
{
|
|
return $this->apiToken;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getApiTokenPrefix(): string
|
|
{
|
|
return $this->apiTokenPrefix;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return int
|
|
*/
|
|
public function getId(): int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
*/
|
|
public function setId(int $id): void
|
|
{
|
|
$this->id = $id;
|
|
}
|
|
|
|
/**
|
|
* @return String
|
|
*/
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* @param string $apiTokenPrefix
|
|
*/
|
|
public function setApiTokenPrefix(string $apiTokenPrefix): void
|
|
{
|
|
$this->apiTokenPrefix = $apiTokenPrefix;
|
|
}
|
|
|
|
/**
|
|
* @param String $apiToken
|
|
*/
|
|
public function setApiToken(string $apiToken): void
|
|
{
|
|
$this->apiToken = $apiToken;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param String $name
|
|
*/
|
|
public function setName(string $name): void
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
} |