Part-DB-server/src/Services/InfoProviderSystem/Providers/TMEClient.php

169 lines
5.8 KiB
PHP
Raw Normal View History

2023-07-15 01:01:20 +02:00
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Services\InfoProviderSystem\Providers;
use App\Settings\InfoProviderSystem\TMESettings;
use Psr\Cache\CacheItemPoolInterface;
2023-07-15 01:01:20 +02:00
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class TMEClient
{
public const BASE_URI = 'https://api.tme.eu';
private const CACHE_KEY_PREFIX = 'tme_oauth_token_';
2026-06-27 19:01:04 +02:00
private ?string $accessToken = null;
private ?string $refreshToken = null;
private int $tokenExpiry = 0;
private bool $loadedFromCache = false;
2026-06-27 19:01:04 +02:00
public function __construct(
private readonly HttpClientInterface $tmeClient,
private readonly TMESettings $settings,
private readonly CacheItemPoolInterface $infoProviderCache,
)
{
}
/**
* The cache key is derived from the configured credentials, so changing the
* token/secret in the settings automatically invalidates any previously cached token.
*/
private function getCacheKey(): string
2023-07-15 01:01:20 +02:00
{
return self::CACHE_KEY_PREFIX . hash('xxh3', $this->settings->apiToken . ':' . $this->settings->apiSecret);
}
/**
* Loads a previously persisted token (if any) into the in-memory properties, so it can be
* reused across requests instead of always starting a fresh client_credentials flow.
*/
private function loadFromCache(): void
{
if ($this->loadedFromCache) {
return;
}
$this->loadedFromCache = true;
$item = $this->infoProviderCache->getItem($this->getCacheKey());
if ($item->isHit()) {
$data = $item->get();
$this->accessToken = $data['access_token'];
$this->refreshToken = $data['refresh_token'];
$this->tokenExpiry = $data['expiry'];
}
}
private function saveToCache(): void
{
$item = $this->infoProviderCache->getItem($this->getCacheKey());
$item->set([
'access_token' => $this->accessToken,
'refresh_token' => $this->refreshToken,
'expiry' => $this->tokenExpiry,
]);
// Keep the refresh token available in the cache for a while after the access token itself expired
$item->expiresAfter(max($this->tokenExpiry - time(), 0) + 60 * 60 * 24 * 7);
$this->infoProviderCache->save($item);
2023-07-15 01:01:20 +02:00
}
2026-06-27 19:01:04 +02:00
private function getAccessToken(): string
2023-07-15 01:01:20 +02:00
{
if ($this->accessToken === null) {
$this->loadFromCache();
}
2026-06-27 19:01:04 +02:00
// Return cached token if still valid (30-second safety margin before expiry)
if ($this->accessToken !== null && time() < $this->tokenExpiry - 30) {
return $this->accessToken;
}
2023-07-15 01:01:20 +02:00
2026-06-27 19:01:04 +02:00
// Try refreshing before falling back to a full client_credentials flow
if ($this->refreshToken !== null) {
try {
$this->fetchToken('refresh_token', ['refresh_token' => $this->refreshToken]);
return $this->accessToken;
} catch (\Throwable) {
$this->refreshToken = null;
}
}
2023-07-15 01:01:20 +02:00
2026-06-27 19:01:04 +02:00
$this->fetchToken('client_credentials');
return $this->accessToken;
2023-07-15 01:01:20 +02:00
}
2026-06-27 19:01:04 +02:00
private function fetchToken(string $grantType, array $extraParams = []): void
{
2026-06-27 19:01:04 +02:00
$credentials = base64_encode($this->settings->apiToken . ':' . $this->settings->apiSecret);
$response = $this->tmeClient->request('POST', self::BASE_URI . '/auth/token', [
'headers' => [
'Authorization' => 'Basic ' . $credentials,
],
'body' => array_merge(['grant_type' => $grantType], $extraParams),
]);
$data = $response->toArray();
$this->accessToken = $data['access_token'];
$this->tokenExpiry = time() + (int) $data['expires_in'];
$this->refreshToken = $data['refresh_token'] ?? null;
$this->saveToCache();
}
2023-07-15 01:01:20 +02:00
/**
2026-06-27 19:01:04 +02:00
* Makes an authenticated GET request to the given v2 endpoint.
*
* @param string $endpoint Path relative to BASE_URI, e.g. 'products/search'
* @param array $queryParams Query parameters; arrays are serialised as PHP-style brackets by Symfony's HTTP client
2023-07-15 01:01:20 +02:00
*/
2026-06-27 19:01:04 +02:00
public function makeRequest(string $endpoint, array $queryParams = []): ResponseInterface
2023-07-15 01:01:20 +02:00
{
2026-06-27 19:01:04 +02:00
$token = $this->getAccessToken();
return $this->tmeClient->request('GET', self::BASE_URI . '/' . $endpoint, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Accept-Language' => $this->settings->language,
],
'query' => $queryParams,
]);
2023-07-15 01:01:20 +02:00
}
2026-06-27 19:01:04 +02:00
public function isUsable(): bool
2023-07-15 01:01:20 +02:00
{
2026-06-27 19:01:04 +02:00
return !($this->settings->apiToken === null || $this->settings->apiSecret === null);
2023-07-15 01:01:20 +02:00
}
2026-06-27 19:01:04 +02:00
/**
* In API v2 all tokens are private (50-char token + 20-char secret); kept for
* backwards-compatibility with code that checks this flag.
*/
public function isUsingPrivateToken(): bool
2023-07-15 01:01:20 +02:00
{
2026-06-27 19:01:04 +02:00
return strlen($this->settings->apiToken ?? '') >= 50;
2023-07-15 01:01:20 +02:00
}
2025-07-13 20:06:38 +02:00
}