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);
use App\Controller\DatabaseConnection;
use App\Controller\EncryptionController;
use App\Entity\Apikey;
use Exception;
use PDO;
use PDOException;
@ -14,7 +14,7 @@ use PDOException;
*/
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
{
$sql = "
SELECT id, name, api_token_prefix, api_token
SELECT id, name, apikey_prefix, apikey
FROM " . DatabaseConnection::TABLE_APIKEYS;
try {
@ -34,7 +35,7 @@ class ApikeyRepository
$apikeys = [];
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;
}
return $apikeys;
@ -47,12 +48,12 @@ class ApikeyRepository
/**
* @param Int $id
*
* @return \App\Entity\Apikey|bool
* @return Apikey|bool
*/
public function findByID(Int $id): Apikey|bool
{
$sql = "
SELECT id, name, api_token_prefix, api_token
SELECT id, name, apikey_prefix, apikey
FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE id = :id;
";
@ -62,7 +63,7 @@ class ApikeyRepository
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
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 {
return false;
}
@ -75,21 +76,21 @@ class ApikeyRepository
/**
* @param String $prefix
*
* @return \App\Entity\Apikey|bool
* @return Apikey|bool
*/
public function findByPrefix(String $prefix): Apikey|bool
{
$sql = "
SELECT id, name, api_token_prefix, api_token
SELECT id, name, apikey_prefix, apikey
FROM " . DatabaseConnection::TABLE_APIKEYS . "
WHERE api_token_prefix = :prefix";
WHERE apikey_prefix = :prefix";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':prefix', var: $prefix);
$statement->execute();
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 {
return false;
}
@ -97,56 +98,51 @@ class ApikeyRepository
exit($e->getMessage());
}
}
/**
* @return array|void
*/
public function create(String $name = '')
{
$tokenPrefix = uniqid();
$result['tokenPrefix'] = $tokenPrefix;
try {
$key = bin2hex(string: random_bytes(length: 24));
$result['key'] = $key;
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
}
$token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID);
/**
* @param Apikey $apikey
* @return int
*/
public function insert(ApiKey $apikey): int
{
$name = $apikey->getName();
$apikeyPrefix = $apikey->getApikeyPrefix();
$apikeyValue = $apikey->getApikey();
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_APIKEYS . " (name, api_token_prefix, api_token)
VALUES (:name, :token_prefix, :token)";
INSERT INTO " . DatabaseConnection::TABLE_APIKEYS . " (name, apikey_prefix, apikey)
VALUES (:name, :apikey_prefix, :apikey)";
try {
$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();
$result['row'] = $this->databaseConnection->getConnection()->lastInsertId();
return $result;
return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
* @param String $name
*
* @return false|int
*/
public function update(int $id, string $name): bool|int
/**
* @param Apikey $apikey
* @return false|int
*/
public function update(Apikey $apikey): bool|int
{
$current = $this->findByID(id: $id);
if (empty($name)) {
$name = $current['name'];
}
$id = $apikey->getId();
$name = $apikey->getName();
$current = $this->findByID(id: $id);
if (empty($name)) {
$name = $current->getName();
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_APIKEYS . " SET
name = :name