java.lang.Object
com.irurueta.authserver.authorization.api.web.ott.OttRateLimiter

@Component @EnableConfigurationProperties(OttRateLimitProperties.class) public final class OttRateLimiter extends Object
Rate limiter of the one-time token endpoints, on the pattern of the account endpoints' limiter: Bucket4j token buckets in Caffeine caches (FixedWindowBuckets). Requests for a token (generation) and submissions of a token or code (verification) are limited separately, and each twice: per client address and per identifier (the user name the request names), so a single client cannot flood the mailboxes and the SMS budget, many clients cannot aim at one account, and a code cannot be guessed faster than the limits allow, on top of the attempt counter of the code itself. The identifier is limited whether or not it belongs to an account, so the limit never tells which ones exist.

Each bucket holds the limit of its key and is refilled all at once every OttRateLimitProperties.window(). The buckets live in this instance only: several instances each apply the limits on their own. The client address is request.getRemoteAddr(), so behind a proxy server.forward-headers-strategy must be set for it to be the real client.

  • Field Details

    • UNKNOWN_CLIENT

      private static final String UNKNOWN_CLIENT
      Key used when the client address is unknown.
      See Also:
    • properties

      private final OttRateLimitProperties properties
      Limits, window and bound of the tracked keys.
    • generationClients

      private final FixedWindowBuckets generationClients
      Buckets of the token requests per client address.
    • generationIdentifiers

      private final FixedWindowBuckets generationIdentifiers
      Buckets of the token requests per identifier.
    • verificationClients

      private final FixedWindowBuckets verificationClients
      Buckets of the submissions per client address.
    • verificationIdentifiers

      private final FixedWindowBuckets verificationIdentifiers
      Buckets of the submissions per identifier.
  • Constructor Details

    • OttRateLimiter

      public OttRateLimiter(OttRateLimitProperties properties, Clock clock)
      Creates the limiter.
      Parameters:
      properties - limits, window and bound of the tracked keys.
      clock - source of the current time.
  • Method Details

    • checkGeneration

      public void checkGeneration(String clientAddress, String identifier)
      Counts one request for a token and rejects it once a limit is exceeded.
      Parameters:
      clientAddress - address of the calling client.
      identifier - identifier the request names, or null when it names none.
      Throws:
      OttRateLimitExceededException - if the client or the identifier exceeded its limit.
    • checkVerification

      public void checkVerification(String clientAddress, String identifier)
      Counts one submission of a token or a code and rejects it once a limit is exceeded.
      Parameters:
      clientAddress - address of the calling client.
      identifier - identifier the submission is for, or null when it names none (a magic link).
      Throws:
      OttRateLimitExceededException - if the client or the identifier exceeded its limit.
    • trackedKeys

      long trackedKeys()
      Tells how many keys are tracked, after any pending eviction.
      Returns:
      the number of keys of the four kinds.
    • check

      private void check(FixedWindowBuckets clients, int clientLimit, FixedWindowBuckets identifiers, int identifierLimit, String clientAddress, String identifier)
      Applies the client and the identifier limits of one kind of request.
      Parameters:
      clients - buckets per client address.
      clientLimit - requests per client address per window.
      identifiers - buckets per identifier.
      identifierLimit - requests per identifier per window.
      clientAddress - address of the calling client.
      identifier - identifier, or null.
      Throws:
      OttRateLimitExceededException - if a limit is exceeded.
    • consume

      private static void consume(FixedWindowBuckets buckets, String key, int limit)
      Takes one token from the bucket of a key.
      Parameters:
      buckets - buckets of one kind of key.
      key - the key.
      limit - requests the key may make per window.
      Throws:
      OttRateLimitExceededException - if the bucket is empty.
    • buckets

      private FixedWindowBuckets buckets(Clock clock)
      Builds the buckets of one kind of key.
      Parameters:
      clock - source of the current time.
      Returns:
      the buckets.