lthn.io/app/CorePlug/Concern/ManagesTokens.php
Claude 41a90cbff8
feat: lthn.io API serving live chain data
Fixed: basePath self→static binding, namespace detection, event wiring,
SQLite cache, file cache driver. All Mod Boot classes converted to
$listens pattern for lifecycle event discovery.

Working endpoints:
- /v1/explorer/info — live chain height, difficulty, aliases
- /v1/explorer/stats — formatted chain statistics
- /v1/names/directory — alias directory grouped by type
- /v1/names/available/{name} — name availability check
- /v1/names/lookup/{name} — name details

Co-Authored-By: Charon <charon@lethean.io>
2026-04-03 17:17:42 +01:00

99 lines
2 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Plug\Concern;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
/**
* Token management helpers for Plug operations.
*/
trait ManagesTokens
{
protected array $token = [];
/**
* Set the access token to use for API requests.
*/
public function withToken(array $token): static
{
$this->token = $token;
return $this;
}
/**
* Get the current access token.
*
* @throws Exception If no token is configured
*/
public function getToken(): array
{
if (empty($this->token)) {
throw new Exception('No access token configured.');
}
return $this->token;
}
/**
* Get the access token string.
*/
protected function accessToken(): string
{
return Arr::get($this->getToken(), 'access_token', '');
}
/**
* Check if a refresh token is available.
*/
public function hasRefreshToken(): bool
{
return ! empty(Arr::get($this->token, 'refresh_token'));
}
/**
* Get the refresh token if available.
*/
protected function refreshToken(): ?string
{
return Arr::get($this->token, 'refresh_token');
}
/**
* Check if the token expires soon.
*
* @param int $minutes Buffer time in minutes (default 12)
*/
public function tokenExpiresSoon(int $minutes = 12): bool
{
$expiresIn = $this->token['expires_in'] ?? null;
if (! $expiresIn) {
return false;
}
$expiresAt = Carbon::createFromTimestamp($expiresIn, 'UTC');
return $expiresAt->lte(Carbon::now('UTC')->addMinutes($minutes));
}
/**
* Check if the token has expired.
*/
public function tokenExpired(): bool
{
$expiresIn = $this->token['expires_in'] ?? null;
if (! $expiresIn) {
return false;
}
$expiresAt = Carbon::createFromTimestamp($expiresIn, 'UTC');
return $expiresAt->lte(Carbon::now('UTC'));
}
}