<?php
// Helpers.php - add token validation function used by middleware
require_once __DIR__ . '/bootstrap.php';
class Helpers {
  public static function isTokenValid($access_token){
    global $CONFIG;
    if (!$access_token) return false;
    $parts = explode('.', $access_token);
    if (count($parts)!==2) return false;
    $payload = base64_decode($parts[0]);
    $sig = $parts[1];
    $expect = hash_hmac('sha256', $payload, $CONFIG['jwt_secret']);
    if (!hash_equals($expect, $sig)) return false;
    $data = json_decode($payload, true);
    if (!$data) return false;
    if (isset($data['exp']) && $data['exp'] < time()) return false;
    return $data;
  }
}
?>