Class AccountRateLimiter

java.lang.Object
com.irurueta.authserver.authorization.api.web.account.AccountRateLimiter

@Component @EnableConfigurationProperties(AccountRateLimitProperties.class) public final class AccountRateLimiter extends Object
Rate limiter of the unauthenticated account endpoints (register, verify, reset request, reset), built on Bucket4j token buckets (FixedWindowBuckets). Requests are limited twice: per client address, and per e-mail address when the request names one, so neither a single client nor many clients aiming at one address can flood the server (and the mailboxes) with account mail or guess reset tokens. Each bucket holds the limit of its key and is refilled all at once every AccountRateLimitProperties.window().

The buckets of each kind of key live in a Caffeine cache bounded to AccountRateLimitProperties.maxTrackedKeys() entries, which also drops a bucket idle for a whole window (it would be full again anyway). They live in this instance only: several instances each apply the limits on their own, until the buckets move to a shared store through a Bucket4j ProxyManager (for example bucket4j-postgresql). 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

  • Constructor Details

    • AccountRateLimiter

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

    • check

      public void check(String clientAddress, String email)
      Counts one request and rejects it once a limit is exceeded.
      Parameters:
      clientAddress - address of the calling client.
      email - e-mail address the request names, or null.
      Throws:
      AccountRateLimitExceededException - if the client or the e-mail address exceeded its limit.
    • trackedKeys

      long trackedKeys()
      Tells how many keys of each kind are tracked, after any pending eviction.
      Returns:
      the number of client keys plus the number of e-mail keys.
    • consume

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