ryanwinchester

ryanwinchester

Is there a best practice for throttling login attempts in Phoenix?

What is everybody else doing right now?

Showing Posts 1 to 7

yurko

yurko

Check this package out GitHub - grempe/ex_rated: ExRated, the Elixir OTP GenServer with the naughty name that allows you to rate-limit calls to any service that requires it. · GitHub

We use it on one project and have our own (a genserver with simple state and some custom logic) on another.

josevalim

josevalim

Creator of Elixir

One possible approach, assuming the login is database backed, is to also store it in the database. You need a column with the attempts and the time of the first attempt. Once login succeeds, you clear those. If login fails and it is within the initial timestamp, you bump the counter. The goal is to not allow more than 5 attempts in X minutes.

dbaer

dbaer

I recommend this approach as well, then you also have an audit trail as a side effect

def check_rate(ip, email) do
    expiry = Timex.shift(Timex.now(), minutes: -15)
    {:ok, ip} = EctoNetwork.INET.cast(ip)

    query =
      from a in Attempt,
        where:
          a.ip == ^ip and a.inserted_at >= ^expiry and
            a.success == false and a.email == ^email

    {:rate, Repo.aggregate(query, :count, :ip) < 5}
  end

or similar depending on the requirements.

karlosmid

karlosmid

I implemented on registration and login form Recaptcha from Google:

https://github.com/samueljseay/recaptcha

ryanwinchester

ryanwinchester OP

Thanks, guys.

I’ll be rolling with a similar DB query version for now.

gregvaughn

gregvaughn

I’m late to the party, but we’ve been very pleased with this erlang library we’ve had in production for a few years now. We use it primarily for rate limiting our outgoing calls, but it works just as good for incoming calls. The mnesia backend it offers was our main reason to choose it for our 2 node cluster.

https://github.com/lambdaclass/throttle

ryanwinchester

ryanwinchester OP

Yeah, in the long run, an ETS (or mnesia?) based approach that doesn’t query the database with every attempt would probably be better.

Here’s what I’ve got for now. (don’t ask me why I made options even though I’ll probably never not use the defaults, I don’t know, it’s a sickness :sweat_smile:)

  def check_login_attempts(ip_address, email, opts \\ []) do
    {limit, opts} = Keyword.pop(opts, :limit, 5)

    if get_login_attempt_rate(ip_address, email, opts) < limit do
      :ok
    else
      {:error, :too_many_requests}
    end
  end

  def get_login_attempt_rate(ip_address, email, opts \\ []) do
    time_ago_opts = Keyword.get(opts, :time_ago, [minutes: -1])
    time_ago = Timex.shift(Timex.now(), time_ago_opts)

    query =
      from a in LoginAttempt,
        where: a.ip_address == ^ip_address or a.email == ^email,
        where: a.attempted_at >= ^time_ago,
        where: not a.success

    Repo.aggregate(query, :count)
  end

I didn’t limit it to just ip_address but also email address (which is a login credential). In case, for example and for whatever reason, someone is using a botnet or something to attempt to login to a single account, not only will the IPs get throttled, the account itself will be as well.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews