42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
require_once dirname(__DIR__) . '/class/db-functions.php';
|
|
$db = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
|
|
|
$authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ?? '';
|
|
if (!preg_match('/Bearer\s+(.*)$/i', $authHeader, $m)) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized', 'reason' => 'no bearer']);
|
|
exit;
|
|
}
|
|
$bearer = trim($m[1]);
|
|
|
|
$hash = hash('sha256', explode('|', $bearer, 2)[1] ?? $bearer);
|
|
$stmt = $db->prepare("SELECT tokenable_id FROM auth_personal_access_tokens WHERE token = ? LIMIT 1");
|
|
$stmt->execute([$hash]);
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$row) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized', 'reason' => 'invalid token']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $db->prepare("SELECT * FROM auth_users WHERE id = ? LIMIT 1");
|
|
$stmt->execute([$row['tokenable_id']]);
|
|
$userRow = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$userRow) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Unauthorized', 'reason' => 'no user']);
|
|
exit;
|
|
}
|
|
|
|
$user = (object) $userRow;
|