Compare commits
3 Commits
30b9b5da1d
...
108bafe299
Author | SHA1 | Date |
---|---|---|
tracer | 108bafe299 | |
tracer | 3363414aa0 | |
tracer | ed5fe80edc |
|
@ -1,3 +1,4 @@
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
/vendor/
|
/vendor/
|
||||||
|
/config.json
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
#!/usr/bin/keyhelp-php81
|
#!/usr/bin/keyhelp-php81
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
if (php_sapi_name() !== 'cli') {
|
if (php_sapi_name() !== 'cli') {
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
require dirname(path: __DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
use App\Controller\BindAPI;
|
use App\Controller\BindAPI;
|
||||||
|
|
||||||
// read config
|
$configFile = dirname(path: __DIR__) ."/config.json";
|
||||||
$configFile = __DIR__ ."/../config.json";
|
|
||||||
$configJSON = file_get_contents($configFile);
|
$configJSON = file_get_contents($configFile);
|
||||||
$config = json_decode($configJSON, associative: true);
|
$config = json_decode($configJSON, associative: true);
|
||||||
|
|
||||||
|
|
|
@ -20,13 +20,13 @@ class ApiUsers
|
||||||
*/
|
*/
|
||||||
public function findAll(): bool|array
|
public function findAll(): bool|array
|
||||||
{
|
{
|
||||||
$statement = "
|
$sql = "
|
||||||
SELECT id, api_token_prefix, api_token
|
SELECT id, api_token_prefix, api_token
|
||||||
FROM user";
|
FROM user";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$statement = $this->dbConnection->query($statement);
|
$statement = $this->dbConnection->query($sql);
|
||||||
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
exit($e->getMessage());
|
exit($e->getMessage());
|
||||||
}
|
}
|
||||||
|
@ -40,40 +40,46 @@ class ApiUsers
|
||||||
*/
|
*/
|
||||||
public function findByID(Int $id): bool|array
|
public function findByID(Int $id): bool|array
|
||||||
{
|
{
|
||||||
$statement = "
|
$sql = "
|
||||||
SELECT api_token_prefix, api_token
|
SELECT api_token_prefix, api_token
|
||||||
FROM user
|
FROM user
|
||||||
WHERE id = :id;
|
WHERE id = :id;
|
||||||
";
|
";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$statement = $this->dbConnection->prepare($statement);
|
$statement = $this->dbConnection->prepare($sql);
|
||||||
$statement->bindParam(':id', $id);
|
$statement->bindParam(param: ':id', var: $id);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
exit($e->getMessage());
|
exit($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param String $prefix
|
||||||
|
*
|
||||||
|
* @return bool|array
|
||||||
|
*/
|
||||||
public function findByPrefix(String $prefix): bool|array
|
public function findByPrefix(String $prefix): bool|array
|
||||||
{
|
{
|
||||||
$statement = "
|
$sql = "
|
||||||
SELECT api_token
|
SELECT api_token
|
||||||
FROM user
|
FROM user
|
||||||
WHERE api_token_prefix = :prefix;
|
WHERE api_token_prefix = :prefix";
|
||||||
";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$statement = $this->dbConnection->prepare($statement);
|
$statement = $this->dbConnection->prepare($sql);
|
||||||
$statement->bindParam(':prefix', $prefix);
|
$statement->bindParam(param: ':prefix', var: $prefix);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
return $statement->fetch(PDO::FETCH_ASSOC);
|
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
exit($e->getMessage());
|
exit($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|void
|
* @return array|void
|
||||||
*/
|
*/
|
||||||
|
@ -82,22 +88,22 @@ class ApiUsers
|
||||||
$tokenPrefix = uniqid();
|
$tokenPrefix = uniqid();
|
||||||
$result['tokenPrefix'] = $tokenPrefix;
|
$result['tokenPrefix'] = $tokenPrefix;
|
||||||
try {
|
try {
|
||||||
$key = bin2hex(random_bytes(24));
|
$key = bin2hex(random_bytes(length: 24));
|
||||||
$result['key'] = $key;
|
$result['key'] = $key;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
echo $e->getMessage() . PHP_EOL;
|
echo $e->getMessage() . PHP_EOL;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
$token = password_hash($tokenPrefix . '.' . $key, PASSWORD_ARGON2ID);
|
$token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID);
|
||||||
|
|
||||||
$statement = "
|
$sql = "
|
||||||
INSERT INTO user (api_token_prefix, api_token)
|
INSERT INTO user (api_token_prefix, api_token)
|
||||||
VALUES (:token_prefix, :token)";
|
VALUES (:token_prefix, :token)";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$statement = $this->dbConnection->prepare($statement);
|
$statement = $this->dbConnection->prepare($sql);
|
||||||
$statement->bindParam(':token_prefix', $tokenPrefix);
|
$statement->bindParam(param: ':token_prefix', var: $tokenPrefix);
|
||||||
$statement->bindParam(':token', $token);
|
$statement->bindParam(param: ':token', var: $token);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
$result['row'] = $this->dbConnection->lastInsertId();
|
$result['row'] = $this->dbConnection->lastInsertId();
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -114,13 +120,13 @@ class ApiUsers
|
||||||
*/
|
*/
|
||||||
public function delete($id): int
|
public function delete($id): int
|
||||||
{
|
{
|
||||||
$statement = "
|
$sql = "
|
||||||
DELETE FROM user
|
DELETE FROM user
|
||||||
WHERE id = :id";
|
WHERE id = :id";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$statement = $this->dbConnection->prepare($statement);
|
$statement = $this->dbConnection->prepare($sql);
|
||||||
$statement->bindParam('id', $id);
|
$statement->bindParam(param: 'id', var: $id);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
return $statement->rowCount();
|
return $statement->rowCount();
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
|
@ -128,5 +134,4 @@ class ApiUsers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue