1. <?php
  2. /**
  3. * Collects authentication token metrics for isolated environments.
  4. */
  5. class TokenMetricsCollector
  6. {
  7. private $tokenCounts = []; // Store token counts for different types
  8. private $tokenExpirationTimes = []; // Store token expiration times
  9. private $tokenIssuanceTimes = []; // Store token issuance times
  10. /**
  11. * Registers a new token.
  12. *
  13. * @param string $token The authentication token.
  14. * @param string $type The type of token (e.g., 'jwt', 'oauth2').
  15. * @param int $expirationTime Unix timestamp of the token's expiration.
  16. * @param int $issuanceTime Unix timestamp of the token's issuance.
  17. * @return bool True on success, false on failure.
  18. */
  19. public function registerToken(string $token, string $type, int $expirationTime, int $issuanceTime): bool
  20. {
  21. $this->tokenCounts[$type] = ($this->tokenCounts[$type] ?? 0) + 1;
  22. $this->tokenExpirationTimes[$token] = $expirationTime;
  23. $this->tokenIssuanceTimes[$token] = $issuanceTime;
  24. return true;
  25. }
  26. /**
  27. * Gets the total count of tokens for a specific type.
  28. *
  29. * @param string $type The token type.
  30. * @return int|null The token count, or null if the type is not found.
  31. */
  32. public function getTokenCount(string $type): ?int
  33. {
  34. return $this->tokenCounts[$type] ?? null;
  35. }
  36. /**
  37. * Gets the expiration time of a specific token.
  38. *
  39. * @param string $token The authentication token.
  40. * @return int|null The expiration time, or null if the token is not found.
  41. */
  42. public function getTokenExpirationTime(string $token): ?int
  43. {
  44. return $this->tokenExpirationTimes[$token] ?? null;
  45. }
  46. /**
  47. * Gets the issuance time of a specific token.
  48. *
  49. * @param string $token The authentication token.
  50. * @return int|null The issuance time, or null if the token is not found.
  51. */
  52. public function getTokenIssuanceTime(string $token): ?int
  53. {
  54. return $this->tokenIssuanceTimes[$token] ?? null;
  55. }
  56. /**
  57. * Clears all collected metrics.
  58. */
  59. public function clearMetrics(): void
  60. {
  61. $this->tokenCounts = [];
  62. $this->tokenExpirationTimes = [];
  63. $this->tokenIssuanceTimes = [];
  64. }
  65. /**
  66. * Outputs collected metrics to the console (for debugging).
  67. */
  68. public function outputMetrics(): void
  69. {
  70. echo "Token Counts:\n";
  71. foreach ($this->tokenCounts as $type => $count) {
  72. echo " $type: $count\n";
  73. }
  74. echo "\nToken Expiration Times:\n";
  75. foreach ($this->tokenExpirationTimes as $token => $expiration) {
  76. echo " $token: $expiration\n";
  77. }
  78. echo "\nToken Issuance Times:\n";
  79. foreach ($this->tokenIssuanceTimes as $token => $issuance) {
  80. echo " $token: $issuance\n";
  81. }
  82. }
  83. }
  84. // Example Usage:
  85. $collector = new TokenMetricsCollector();
  86. // Simulate token registration
  87. $collector->registerToken('token123', 'jwt', time() + 3600, time());
  88. $collector->registerToken('token456', 'oauth2', time() + 7200, time());
  89. $collector->registerToken('token123', 'jwt', time() + 86400, time()); //Register same token again
  90. // Get token counts
  91. $jwtCount = $collector->getTokenCount('jwt');
  92. echo "JWT Token Count: " . $jwtCount . "\n";
  93. // Get token expiration time
  94. $expirationTime = $collector->getTokenExpirationTime('token456');
  95. echo "Token 456 Expiration Time: " . $expirationTime . "\n";
  96. $collector->outputMetrics();
  97. $collector->clearMetrics();
  98. ?>

Add your comment