Class OttRateLimiter
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final FixedWindowBucketsBuckets of the token requests per client address.private final FixedWindowBucketsBuckets of the token requests per identifier.private final OttRateLimitPropertiesLimits, window and bound of the tracked keys.private static final StringKey used when the client address is unknown.private final FixedWindowBucketsBuckets of the submissions per client address.private final FixedWindowBucketsBuckets of the submissions per identifier. -
Constructor Summary
ConstructorsConstructorDescriptionOttRateLimiter(OttRateLimitProperties properties, Clock clock) Creates the limiter. -
Method Summary
Modifier and TypeMethodDescriptionprivate FixedWindowBucketsBuilds the buckets of one kind of key.private voidcheck(FixedWindowBuckets clients, int clientLimit, FixedWindowBuckets identifiers, int identifierLimit, String clientAddress, String identifier) Applies the client and the identifier limits of one kind of request.voidcheckGeneration(String clientAddress, String identifier) Counts one request for a token and rejects it once a limit is exceeded.voidcheckVerification(String clientAddress, String identifier) Counts one submission of a token or a code 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.(package private) longTells how many keys are tracked, after any pending eviction.
-
Field Details
-
UNKNOWN_CLIENT
Key used when the client address is unknown.- See Also:
-
properties
Limits, window and bound of the tracked keys. -
generationClients
Buckets of the token requests per client address. -
generationIdentifiers
Buckets of the token requests per identifier. -
verificationClients
Buckets of the submissions per client address. -
verificationIdentifiers
Buckets of the submissions per identifier.
-
-
Constructor Details
-
OttRateLimiter
Creates the limiter.- Parameters:
properties- limits, window and bound of the tracked keys.clock- source of the current time.
-
-
Method Details
-
checkGeneration
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, ornullwhen it names none.- Throws:
OttRateLimitExceededException- if the client or the identifier exceeded its limit.
-
checkVerification
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, ornullwhen 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, ornull.- Throws:
OttRateLimitExceededException- if a limit is exceeded.
-
consume
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
Builds the buckets of one kind of key.- Parameters:
clock- source of the current time.- Returns:
- the buckets.
-