Class RotateSigningKeysUseCase

java.lang.Object
com.irurueta.authserver.authorization.application.keys.RotateSigningKeysUseCase

@Service @Transactional public class RotateSigningKeysUseCase extends Object
Use case that keeps the signing keys rotating. One call, made at start-up and then on a schedule, does whatever is due: it creates the active and next keys when they are missing, promotes next to active and retires the old active key once the rotation interval has elapsed, always leaves a fresh next key behind, and deletes keys retired for longer than the effective retention (see SigningKeyRetention). A second call right after the first changes nothing, and the whole call runs under the repository's rotation lock, so several instances can call it safely. executeForced() is the administrator's path: it replaces the active key immediately, whatever its age.
  • Field Details

    • signingKeyRepository

      private final SigningKeyRepository signingKeyRepository
      Port that stores the keys.
    • signingKeyGenerator

      private final SigningKeyGenerator signingKeyGenerator
      Port that generates keys.
    • clock

      private final Clock clock
      Source of the current time.
    • rotationInterval

      @Value("${authserver.keys.rotation-interval:P30D}") private final Duration rotationInterval
      How long a key signs before it is replaced (authserver.keys.rotation-interval, default 30 days).
    • signingKeyRetention

      private final SigningKeyRetention signingKeyRetention
      Decides how long a retired key stays published.
  • Constructor Details

    • RotateSigningKeysUseCase

      public RotateSigningKeysUseCase()
  • Method Details

    • execute

      public void execute()
      Does whatever the key lifecycle requires at the current instant: the scheduled path, which only rotates a key that has signed for the whole rotation interval.
    • executeForced

      public void executeForced()
      Rotates now, whatever the age of the active key: the active key is retired, the next key promoted and a new next key generated (for example after a suspected key compromise). Expired retired keys are deleted too.
    • run

      private void run(boolean force)
      Applies the lifecycle transitions while holding the rotation lock.
      Parameters:
      force - whether the active key is replaced even when it is not due.
    • rotate

      private void rotate(Instant now, boolean force, Duration retention)
      Applies the lifecycle transitions that are due, or forced.
      Parameters:
      now - current instant.
      force - whether the active key is replaced even when it is not due.
      retention - how long a retired key stays published.
    • first

      private static SigningKey first(List<SigningKey> keys, SigningKeyStatus status)
      Finds the first key in a state.
      Parameters:
      keys - the keys.
      status - the state.
      Returns:
      the key, or null when none is in that state.
    • isDue

      private boolean isDue(SigningKey active, Instant now)
      Tells whether the active key has signed for the whole rotation interval.
      Parameters:
      active - the active key.
      now - current instant.
      Returns:
      true when it must be replaced.
    • isExpired

      private static boolean isExpired(SigningKey retired, Instant now, Duration retention)
      Tells whether a retired key has been retired for longer than the retention.
      Parameters:
      retired - the retired key.
      now - current instant.
      retention - how long a retired key stays published.
      Returns:
      true when it can be deleted.