added password encryption

This commit is contained in:
tracer 2022-09-22 18:54:54 +02:00
parent 790176964d
commit 8ec1a2942d
1 changed files with 97 additions and 80 deletions

View File

@ -2,89 +2,106 @@
namespace App\Entity; namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use SodiumException;
/** /**
* *
*/ */
class Apikey class Apikey
{ {
private int $id;
private string $name;
private string $apiTokenPrefix;
private string $apiToken;
public function __construct(string $name, string $apiTokenPrefix, string $apiToken, int $id = 0) public function __construct(
{ private int $id = 0,
$this->id = $id; private string $name = '',
$this->name = $name; private string $apiTokenPrefix = '',
$this->apiTokenPrefix = $apiTokenPrefix; private string $apiToken = '',
$this->apiToken = $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 * @return String
*/ */
public function getApiToken(): string public function getApiToken(): string
{ {
return $this->apiToken; return $this->apiToken;
} }
/** /**
* @return string * @return string
*/ */
public function getApiTokenPrefix(): string public function getApiTokenPrefix(): string
{ {
return $this->apiTokenPrefix; return $this->apiTokenPrefix;
} }
/** /**
* @return int * @return int
*/ */
public function getId(): int public function getId(): int
{ {
return $this->id; return $this->id;
} }
/** /**
* @param int $id * @param int $id
*/ */
public function setId(int $id): void public function setId(int $id): void
{ {
$this->id = $id; $this->id = $id;
} }
/** /**
* @return String * @return String
*/ */
public function getName(): string public function getName(): string
{ {
return $this->name; return $this->name;
} }
/** /**
* @param string $apiTokenPrefix * @param string $apiTokenPrefix
*/ */
public function setApiTokenPrefix(string $apiTokenPrefix): void public function setApiTokenPrefix(string $apiTokenPrefix): void
{ {
$this->apiTokenPrefix = $apiTokenPrefix; $this->apiTokenPrefix = $apiTokenPrefix;
} }
/** /**
* @param String $apiToken * @param String $apiToken
*/ */
public function setApiToken(string $apiToken): void public function setApiToken(string $apiToken): void
{ {
$this->apiToken = $apiToken; $this->apiToken = $apiToken;
} }
/** /**
* @param String $name * @param String $name
*/ */
public function setName(string $name): void public function setName(string $name): void
{ {
$this->name = $name; $this->name = $name;
} }
} }