slowapi icon indicating copy to clipboard operation
slowapi copied to clipboard

How to rate limit based on [user id/Token]

Open alexjolig opened this issue 3 years ago • 3 comments

I need to implement the rate limitation based on JWT token. But in utils.py I can only see functions to get user's ip address. So is that possible to customize this. For example I can get the user info from request.state.user. How can I limit based on that?

alexjolig avatar Apr 21 '22 14:04 alexjolig

I think this issue https://github.com/laurentS/slowapi/issues/13 might be what you're after.

laurentS avatar Apr 21 '22 15:04 laurentS

Thanks @laurentS . So if I want to limit based on JWT token, should I do it like this?

@limiter.limit(get_jwt_token):
def some_request(request: Request):
    pass

alexjolig avatar Apr 21 '22 15:04 alexjolig

def get_limit_for_user():
    request = _request_ctx_var.get()
    key = request.cookies.get("key")

You have to pass values through cookies. In the body it will not work due to asyc.

mallorbc avatar Oct 22 '22 05:10 mallorbc