renamed api_token to apikey

This commit is contained in:
tracer 2022-10-08 10:56:45 +02:00
parent 9985e2e896
commit 28a9e4ac08
1 changed files with 47 additions and 51 deletions

View File

@ -4,8 +4,8 @@ namespace App\Repository;
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
use App\Controller\DatabaseConnection; use App\Controller\DatabaseConnection;
use App\Controller\EncryptionController;
use App\Entity\Apikey; use App\Entity\Apikey;
use Exception;
use PDO; use PDO;
use PDOException; use PDOException;
@ -14,7 +14,7 @@ use PDOException;
*/ */
class ApikeyRepository class ApikeyRepository
{ {
public function __construct(private readonly DatabaseConnection $databaseConnection) public function __construct(private readonly DatabaseConnection $databaseConnection, EncryptionController $encryptionController)
{} {}
@ -23,8 +23,9 @@ class ApikeyRepository
*/ */
public function findAll(): bool|array public function findAll(): bool|array
{ {
$sql = " $sql = "
SELECT id, name, api_token_prefix, api_token SELECT id, name, apikey_prefix, apikey
FROM " . DatabaseConnection::TABLE_APIKEYS; FROM " . DatabaseConnection::TABLE_APIKEYS;
try { try {
@ -34,7 +35,7 @@ class ApikeyRepository
$apikeys = []; $apikeys = [];
while ($result = $statement->fetch()) { while ($result = $statement->fetch()) {
$apikey = new Apikey(name: $result['name'], apiTokenPrefix: $result['api_token_prefix'], apiToken: $result['api_token'], id: $result['id']); $apikey = new Apikey(id: $result['id'], name: $result['name'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix']);
$apikeys[] = $apikey; $apikeys[] = $apikey;
} }
return $apikeys; return $apikeys;
@ -47,12 +48,12 @@ class ApikeyRepository
/** /**
* @param Int $id * @param Int $id
* *
* @return \App\Entity\Apikey|bool * @return Apikey|bool
*/ */
public function findByID(Int $id): Apikey|bool public function findByID(Int $id): Apikey|bool
{ {
$sql = " $sql = "
SELECT id, name, api_token_prefix, api_token SELECT id, name, apikey_prefix, apikey
FROM " . DatabaseConnection::TABLE_APIKEYS . " FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE id = :id; WHERE id = :id;
"; ";
@ -62,7 +63,7 @@ class ApikeyRepository
$statement->bindParam(param: ':id', var: $id); $statement->bindParam(param: ':id', var: $id);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Apikey(name: $result['name'], apiTokenPrefix: $result['api_token_prefix'], apiToken: $result['api_token'], id: $result['id']); return new Apikey(id: $result['id'], name: $result['name'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix']);
} else { } else {
return false; return false;
} }
@ -75,21 +76,21 @@ class ApikeyRepository
/** /**
* @param String $prefix * @param String $prefix
* *
* @return \App\Entity\Apikey|bool * @return Apikey|bool
*/ */
public function findByPrefix(String $prefix): Apikey|bool public function findByPrefix(String $prefix): Apikey|bool
{ {
$sql = " $sql = "
SELECT id, name, api_token_prefix, api_token SELECT id, name, apikey_prefix, apikey
FROM " . DatabaseConnection::TABLE_APIKEYS . " FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE api_token_prefix = :prefix"; WHERE apikey_prefix = :prefix";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':prefix', var: $prefix); $statement->bindParam(param: ':prefix', var: $prefix);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Apikey(name: $result['name'], apiTokenPrefix: $result['api_token_prefix'], apiToken: $result['api_token'], id: $result['id']); return new Apikey(id: $result['id'], name: $result['name'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix']);
} else { } else {
return false; return false;
} }
@ -100,51 +101,46 @@ class ApikeyRepository
/** /**
* @return array|void * @param Apikey $apikey
* @return int
*/ */
public function create(String $name = '') public function insert(ApiKey $apikey): int
{ {
$tokenPrefix = uniqid();
$result['tokenPrefix'] = $tokenPrefix; $name = $apikey->getName();
try { $apikeyPrefix = $apikey->getApikeyPrefix();
$key = bin2hex(string: random_bytes(length: 24)); $apikeyValue = $apikey->getApikey();
$result['key'] = $key;
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
}
$token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID);
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_APIKEYS . " (name, api_token_prefix, api_token) INSERT INTO " . DatabaseConnection::TABLE_APIKEYS . " (name, apikey_prefix, apikey)
VALUES (:name, :token_prefix, :token)"; VALUES (:name, :apikey_prefix, :apikey)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':token_prefix', var: $tokenPrefix);
$statement->bindParam(param: ':token', var: $token);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':apikey_prefix', var: $apikeyPrefix);
$statement->bindParam(param: ':apikey', var: $apikeyValue);
$statement->execute(); $statement->execute();
$result['row'] = $this->databaseConnection->getConnection()->lastInsertId(); return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
return $result;
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param Int $id * @param Apikey $apikey
* @param String $name
*
* @return false|int * @return false|int
*/ */
public function update(int $id, string $name): bool|int public function update(Apikey $apikey): bool|int
{ {
$id = $apikey->getId();
$name = $apikey->getName();
$current = $this->findByID(id: $id); $current = $this->findByID(id: $id);
if (empty($name)) { if (empty($name)) {
$name = $current['name']; $name = $current->getName();
} }
$sql = " $sql = "