2022-01-18 19:14:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-20 10:37:34 +01:00
|
|
|
use Exception;
|
2022-01-18 19:14:24 +01:00
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2022-01-23 16:36:04 +01:00
|
|
|
class ApiKeys
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-22 18:43:51 +01:00
|
|
|
public function __construct(private DatabaseConnection $databaseConnection)
|
2022-01-20 10:37:34 +01:00
|
|
|
{}
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function findAll(): bool|array
|
|
|
|
{
|
2022-01-22 16:43:06 +01:00
|
|
|
$sql = "
|
2022-01-23 16:36:04 +01:00
|
|
|
SELECT id, name, api_token_prefix, api_token
|
2022-01-22 18:43:51 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_USER;
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
try {
|
2022-01-22 18:43:51 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->query($sql);
|
2022-01-22 16:43:06 +01:00
|
|
|
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
2022-01-18 19:14:24 +01:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Int $id
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function findByID(Int $id): bool|array
|
|
|
|
{
|
2022-01-22 16:43:06 +01:00
|
|
|
$sql = "
|
2022-01-23 16:36:04 +01:00
|
|
|
SELECT name, api_token_prefix, api_token
|
2022-01-22 18:43:51 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_USER . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id;
|
|
|
|
";
|
|
|
|
|
|
|
|
try {
|
2022-01-22 18:43:51 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 16:43:06 +01:00
|
|
|
$statement->bindParam(param: ':id', var: $id);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 16:43:06 +01:00
|
|
|
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
2022-01-20 10:37:34 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:43:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $prefix
|
|
|
|
*
|
|
|
|
* @return bool|array
|
|
|
|
*/
|
2022-01-18 19:14:24 +01:00
|
|
|
public function findByPrefix(String $prefix): bool|array
|
|
|
|
{
|
2022-01-22 16:43:06 +01:00
|
|
|
$sql = "
|
2022-01-23 16:36:04 +01:00
|
|
|
SELECT name, api_token
|
2022-01-22 18:43:51 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_USER . "
|
2022-01-22 16:43:06 +01:00
|
|
|
WHERE api_token_prefix = :prefix";
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
try {
|
2022-01-22 18:43:51 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 16:43:06 +01:00
|
|
|
$statement->bindParam(param: ':prefix', var: $prefix);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 16:43:06 +01:00
|
|
|
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
2022-01-20 10:37:34 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:43:06 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
/**
|
|
|
|
* @return array|void
|
|
|
|
*/
|
2022-01-23 16:36:04 +01:00
|
|
|
public function create(String $name = '')
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
|
|
|
$tokenPrefix = uniqid();
|
|
|
|
$result['tokenPrefix'] = $tokenPrefix;
|
|
|
|
try {
|
2022-01-22 16:43:06 +01:00
|
|
|
$key = bin2hex(random_bytes(length: 24));
|
2022-01-18 19:14:24 +01:00
|
|
|
$result['key'] = $key;
|
2022-01-20 10:37:34 +01:00
|
|
|
} catch (Exception $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
echo $e->getMessage() . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-22 16:43:06 +01:00
|
|
|
$token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID);
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-22 16:43:06 +01:00
|
|
|
$sql = "
|
2022-01-23 16:36:04 +01:00
|
|
|
INSERT INTO " . DatabaseConnection::TABLE_USER . " (name, api_token_prefix, api_token)
|
2022-01-24 19:01:32 +01:00
|
|
|
VALUES (:name, :token_prefix, :token)";
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
try {
|
2022-01-22 18:43:51 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 16:43:06 +01:00
|
|
|
$statement->bindParam(param: ':token_prefix', var: $tokenPrefix);
|
|
|
|
$statement->bindParam(param: ':token', var: $token);
|
2022-01-23 16:36:04 +01:00
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 18:43:51 +01:00
|
|
|
$result['row'] = $this->databaseConnection->getConnection()->lastInsertId();
|
2022-01-18 19:14:24 +01:00
|
|
|
return $result;
|
2022-01-20 10:37:34 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
*
|
2022-01-20 10:37:34 +01:00
|
|
|
* @return int
|
2022-01-18 19:14:24 +01:00
|
|
|
*/
|
2022-01-20 10:37:34 +01:00
|
|
|
public function delete($id): int
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-22 16:43:06 +01:00
|
|
|
$sql = "
|
2022-01-22 18:43:51 +01:00
|
|
|
DELETE FROM " . DatabaseConnection::TABLE_USER . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
2022-01-22 18:43:51 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare($sql);
|
2022-01-22 16:43:06 +01:00
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
|
|
|
return $statement->rowCount();
|
2022-01-20 10:37:34 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|