<?php
/**
* Collects authentication token metrics for isolated environments.
*/
class TokenMetricsCollector
{
private $tokenCounts = []; // Store token counts for different types
private $tokenExpirationTimes = []; // Store token expiration times
private $tokenIssuanceTimes = []; // Store token issuance times
/**
* Registers a new token.
*
* @param string $token The authentication token.
* @param string $type The type of token (e.g., 'jwt', 'oauth2').
* @param int $expirationTime Unix timestamp of the token's expiration.
* @param int $issuanceTime Unix timestamp of the token's issuance.
* @return bool True on success, false on failure.
*/
public function registerToken(string $token, string $type, int $expirationTime, int $issuanceTime): bool
{
$this->tokenCounts[$type] = ($this->tokenCounts[$type] ?? 0) + 1;
$this->tokenExpirationTimes[$token] = $expirationTime;
$this->tokenIssuanceTimes[$token] = $issuanceTime;
return true;
}
/**
* Gets the total count of tokens for a specific type.
*
* @param string $type The token type.
* @return int|null The token count, or null if the type is not found.
*/
public function getTokenCount(string $type): ?int
{
return $this->tokenCounts[$type] ?? null;
}
/**
* Gets the expiration time of a specific token.
*
* @param string $token The authentication token.
* @return int|null The expiration time, or null if the token is not found.
*/
public function getTokenExpirationTime(string $token): ?int
{
return $this->tokenExpirationTimes[$token] ?? null;
}
/**
* Gets the issuance time of a specific token.
*
* @param string $token The authentication token.
* @return int|null The issuance time, or null if the token is not found.
*/
public function getTokenIssuanceTime(string $token): ?int
{
return $this->tokenIssuanceTimes[$token] ?? null;
}
/**
* Clears all collected metrics.
*/
public function clearMetrics(): void
{
$this->tokenCounts = [];
$this->tokenExpirationTimes = [];
$this->tokenIssuanceTimes = [];
}
/**
* Outputs collected metrics to the console (for debugging).
*/
public function outputMetrics(): void
{
echo "Token Counts:\n";
foreach ($this->tokenCounts as $type => $count) {
echo " $type: $count\n";
}
echo "\nToken Expiration Times:\n";
foreach ($this->tokenExpirationTimes as $token => $expiration) {
echo " $token: $expiration\n";
}
echo "\nToken Issuance Times:\n";
foreach ($this->tokenIssuanceTimes as $token => $issuance) {
echo " $token: $issuance\n";
}
}
}
// Example Usage:
$collector = new TokenMetricsCollector();
// Simulate token registration
$collector->registerToken('token123', 'jwt', time() + 3600, time());
$collector->registerToken('token456', 'oauth2', time() + 7200, time());
$collector->registerToken('token123', 'jwt', time() + 86400, time()); //Register same token again
// Get token counts
$jwtCount = $collector->getTokenCount('jwt');
echo "JWT Token Count: " . $jwtCount . "\n";
// Get token expiration time
$expirationTime = $collector->getTokenExpirationTime('token456');
echo "Token 456 Expiration Time: " . $expirationTime . "\n";
$collector->outputMetrics();
$collector->clearMetrics();
?>
Add your comment