bindAPI/src/Controller/ApiKeys.php

140 lines
3.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2022-01-18 19:14:24 +01:00
namespace App\Controller;
error_reporting(error_level: E_ALL);
2022-01-20 10:37:34 +01:00
use Exception;
2022-01-18 19:14:24 +01:00
use PDO;
use PDOException;
/**
*
*/
class ApiKeys
2022-01-18 19:14:24 +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 = "
SELECT id, name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_USER;
2022-01-18 19:14:24 +01:00
try {
$statement = $this->databaseConnection->getConnection()->query(statement: $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 = "
SELECT name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_USER . "
2022-01-18 19:14:24 +01:00
WHERE id = :id;
";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $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 = "
SELECT name, api_token
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 {
$statement = $this->databaseConnection->getConnection()->prepare(query: $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
*/
public function create(String $name = '')
2022-01-18 19:14:24 +01:00
{
$tokenPrefix = uniqid();
$result['tokenPrefix'] = $tokenPrefix;
try {
$key = bin2hex(string: 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 = "
INSERT INTO " . DatabaseConnection::TABLE_USER . " (name, api_token_prefix, api_token)
VALUES (:name, :token_prefix, :token)";
2022-01-18 19:14:24 +01:00
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
2022-01-22 16:43:06 +01:00
$statement->bindParam(param: ':token_prefix', var: $tokenPrefix);
$statement->bindParam(param: ':token', var: $token);
$statement->bindParam(param: ':name', var: $name);
2022-01-18 19:14:24 +01:00
$statement->execute();
$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 = "
DELETE FROM " . DatabaseConnection::TABLE_USER . "
2022-01-18 19:14:24 +01:00
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $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());
}
}
}