File size: 1,155 Bytes
0dff816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
header('Content-Type: application/json');
require_once '../models/TokenManager.php';

$tokenManager = new TokenManager();

// Get token from Authorization header
$headers = getallheaders();
$auth_header = $headers['Authorization'] ?? '';
$token = '';

if (preg_match('/Bearer\s+(.*)$/i', $auth_header, $matches)) {
    $token = $matches[1];
} else {
    $token = $_GET['token'] ?? '';
}

if (empty($token)) {
    http_response_code(401);
    echo json_encode(['error' => 'No token provided']);
    exit;
}

// Get request details
$endpoint = $_SERVER['REQUEST_URI'];
$ip_address = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$method = $_SERVER['REQUEST_METHOD'];

// Validate token
$result = $tokenManager->validateAndLogUsage($token, $endpoint, $ip_address, $user_agent, $method);

if ($result['valid']) {
    echo json_encode([
        'valid' => true,
        'user' => $result['username'],
        'permissions' => $result['permissions'],
        'timestamp' => date('c')
    ]);
} else {
    http_response_code(401);
    echo json_encode([
        'valid' => false,
        'error' => $result['error']
    ]);
}
?>