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 Summary
FieldsModifier and TypeFieldDescriptionprivate final FixedWindowBucketsBuckets per client address.private final FixedWindowBucketsBuckets per (lower-cased) e-mail address.private final AccountRateLimitPropertiesLimits, window and bound of the tracked keys. -
Constructor Summary
ConstructorsConstructorDescriptionAccountRateLimiter(AccountRateLimitProperties properties, Clock clock) Creates the limiter. -
Method Summary
Modifier and TypeMethodDescriptionvoidCounts one request and rejects it once a limit is exceeded.private static voidconsume(FixedWindowBuckets buckets, String key, int limit) Takes one token from the bucket of a key, creating the bucket on first use.(package private) longTells how many keys of each kind are tracked, after any pending eviction.
-
Field Details
-
properties
Limits, window and bound of the tracked keys. -
clients
Buckets per client address. -
emails
Buckets per (lower-cased) e-mail address.
-
-
Constructor Details
-
AccountRateLimiter
Creates the limiter.- Parameters:
properties- limits, window and bound of the tracked keys.clock- source of the current time.
-
-
Method Details
-
check
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, ornull.- 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
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.
-