Class KeycloakTokenRepository

java.lang.Object
de.cuioss.benchmarking.common.repository.KeycloakTokenRepository
All Implemented Interfaces:
TokenProvider

public class KeycloakTokenRepository extends Object implements TokenProvider
Keycloak-based token repository for fetching real JWT tokens from a Keycloak server.

This implementation fetches actual tokens from a Keycloak authentication server, making it suitable for integration benchmarks that need to test against real authentication infrastructure.

Features:

  • Fetches tokens from Keycloak using password grant
  • Maintains a pool of tokens for rotation
  • Supports SSL verification configuration
  • Provides round-robin token distribution
Author:
Oliver Wolff
  • Constructor Details

    • KeycloakTokenRepository

      Creates a new KeycloakTokenRepository with the given configuration.
      Parameters:
      config - the configuration for connecting to Keycloak
  • Method Details

    • getNextToken

      public String getNextToken()
      Gets the next token from the provider.

      Implementations should provide round-robin or similar rotation strategy to ensure even distribution of tokens and realistic cache behavior.

      Gets the next token from the pool using round-robin rotation. This ensures even distribution and simulates cache miss scenarios. If the pool is empty, fetches a single token directly from Keycloak.

      Specified by:
      getNextToken in interface TokenProvider
      Returns:
      a valid JWT access token
    • getTokenPoolSize

      public int getTokenPoolSize()
      Returns the current size of the token pool.

      This method is useful for monitoring and capacity planning during benchmarks.

      Returns the current size of the token pool.

      Specified by:
      getTokenPoolSize in interface TokenProvider
      Returns:
      the number of tokens currently available in the pool
    • refreshTokens

      public void refreshTokens()
      Refreshes the token pool with new tokens.

      This method allows for token renewal during long-running benchmarks. Implementations may choose to:

      • Generate new tokens (mock implementations)
      • Fetch fresh tokens from authentication server (real implementations)
      • Do nothing if token refresh is not supported

      Refreshes the token pool by fetching new tokens from Keycloak. This replaces all existing tokens in the pool with fresh ones.

      Specified by:
      refreshTokens in interface TokenProvider
      Throws:
      KeycloakTokenRepository.TokenFetchException - if unable to fetch new tokens from Keycloak