forked from BassPago/leeds_backend
52 lines
1.1 KiB
PHP
52 lines
1.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Bass\Webclient\Auth\Models;
|
|
|
|
use Bass\Webclient\Infra\ModelFactory;
|
|
|
|
class ApiKeyModel
|
|
{
|
|
public function findActiveByKey(string $apiKey): array
|
|
{
|
|
$sql = "
|
|
SELECT
|
|
user_api_key as api_key,
|
|
user_api_secret as api_secret,
|
|
user_status as status,
|
|
user_id,
|
|
user_name as username
|
|
FROM users
|
|
WHERE user_api_key = :api_key
|
|
AND user_status = 1
|
|
LIMIT 1
|
|
";
|
|
|
|
try {
|
|
$stmt = ModelFactory::db()->prepare($sql);
|
|
$stmt->execute(['api_key' => $apiKey]);
|
|
$row = $stmt->fetch();
|
|
|
|
if (!$row) {
|
|
return [
|
|
false,
|
|
[
|
|
'code' => 'API_KEY_NOT_FOUND',
|
|
'message' => 'Invalid API key'
|
|
]
|
|
];
|
|
}
|
|
|
|
return [true, $row];
|
|
} catch (\Throwable $e) {
|
|
return [
|
|
false,
|
|
[
|
|
'code' => 'AUTH_DB_ERROR',
|
|
'message' => 'Failed to query auth database'
|
|
]
|
|
];
|
|
}
|
|
}
|
|
}
|