-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4532 from santiment/generalize-access-attempts
Generalize attempts to use resources
- Loading branch information
Showing
12 changed files
with
415 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
defmodule Sanbase.Accounts.AccessAttempt do | ||
use Ecto.Schema | ||
import Ecto.{Query, Changeset} | ||
alias Sanbase.Repo | ||
|
||
schema "access_attempts" do | ||
belongs_to(:user, Sanbase.Accounts.User) | ||
field(:ip_address, :string) | ||
field(:type, :string) | ||
|
||
timestamps() | ||
end | ||
|
||
def check_attempt_limit(type, user, remote_ip) do | ||
config = get_config(type) | ||
too_many_user_attempts? = attempts_count(type, user) > config.allowed_user_attempts | ||
too_many_ip_attempts? = attempts_count(type, remote_ip) > config.allowed_ip_attempts | ||
|
||
if too_many_user_attempts? or too_many_ip_attempts? do | ||
{:error, :too_many_attempts} | ||
else | ||
:ok | ||
end | ||
end | ||
|
||
def create(type, user, remote_ip) do | ||
%__MODULE__{} | ||
|> changeset(%{ | ||
user_id: user && user.id, | ||
ip_address: remote_ip, | ||
type: type | ||
}) | ||
|> Repo.insert() | ||
|> case do | ||
{:error, changeset} -> {:error, changeset} | ||
attempt -> attempt | ||
end | ||
end | ||
|
||
def changeset(%__MODULE__{} = attempt, attrs \\ %{}) do | ||
attempt | ||
|> cast(attrs, [:user_id, :ip_address, :type]) | ||
|> validate_required([:ip_address, :type]) | ||
|> foreign_key_constraint(:user_id) | ||
end | ||
|
||
defp attempts_count(type, remote_ip) when is_binary(remote_ip) do | ||
config = get_config(type) | ||
interval_limit = Timex.shift(Timex.now(), minutes: -config.interval_in_minutes) | ||
|
||
from(attempt in __MODULE__, | ||
where: | ||
attempt.type == ^type and | ||
attempt.ip_address == ^remote_ip and | ||
attempt.inserted_at > ^interval_limit | ||
) | ||
|> Repo.aggregate(:count, :id) | ||
end | ||
|
||
defp attempts_count(type, %{id: user_id}) do | ||
config = get_config(type) | ||
interval_limit = Timex.shift(Timex.now(), minutes: -config.interval_in_minutes) | ||
|
||
from(attempt in __MODULE__, | ||
where: | ||
attempt.type == ^type and | ||
attempt.user_id == ^user_id and | ||
attempt.inserted_at > ^interval_limit | ||
) | ||
|> Repo.aggregate(:count, :id) | ||
end | ||
|
||
defp get_config(type) do | ||
case type do | ||
"email_login" -> Sanbase.Accounts.EmailLoginAttempt.config() | ||
"coupon" -> Sanbase.Accounts.CouponAttempt.config() | ||
_ -> raise "Unknown access attempt type: #{type}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Sanbase.Accounts.AccessAttemptBehaviour do | ||
@callback config() :: %{ | ||
interval_in_minutes: pos_integer(), | ||
allowed_user_attempts: pos_integer(), | ||
allowed_ip_attempts: pos_integer() | ||
} | ||
|
||
@callback type() :: String.t() | ||
|
||
@callback check_attempt_limit(user :: term(), remote_ip :: String.t()) :: | ||
:ok | {:error, :too_many_attempts} | ||
|
||
@callback create(user :: term(), remote_ip :: String.t()) :: | ||
{:ok, term()} | {:error, term()} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Sanbase.Accounts.CouponAttempt do | ||
@behaviour Sanbase.Accounts.AccessAttemptBehaviour | ||
alias Sanbase.Accounts.AccessAttempt | ||
|
||
@impl true | ||
def type, do: "coupon" | ||
|
||
@impl true | ||
def config do | ||
%{ | ||
interval_in_minutes: 10, | ||
allowed_user_attempts: 5, | ||
allowed_ip_attempts: 20 | ||
} | ||
end | ||
|
||
@impl true | ||
def check_attempt_limit(user, remote_ip) do | ||
AccessAttempt.check_attempt_limit(type(), user, remote_ip) | ||
end | ||
|
||
@impl true | ||
def create(user, remote_ip) do | ||
AccessAttempt.create(type(), user, remote_ip) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,26 @@ | ||
defmodule Sanbase.Accounts.EmailLoginAttempt do | ||
use Ecto.Schema | ||
|
||
import Ecto.{Query, Changeset} | ||
|
||
alias Sanbase.Repo | ||
|
||
@interval_in_minutes 5 | ||
@allowed_login_attempts 5 | ||
@allowed_ip_attempts 20 | ||
|
||
schema "email_login_attempts" do | ||
belongs_to(:user, Sanbase.Accounts.User) | ||
field(:ip_address, :string) | ||
|
||
timestamps() | ||
end | ||
|
||
def has_allowed_login_attempts(user, remote_ip) do | ||
too_many_login_attempts? = login_attempts_count(user) > @allowed_login_attempts | ||
too_many_ip_attempts? = login_attempts_count(remote_ip) > @allowed_ip_attempts | ||
|
||
if too_many_login_attempts? or too_many_ip_attempts? do | ||
{:error, :too_many_login_attempts} | ||
else | ||
:ok | ||
end | ||
end | ||
|
||
def create(%{id: user_id}, remote_ip) do | ||
%__MODULE__{} | ||
|> changeset(%{user_id: user_id, ip_address: remote_ip}) | ||
|> Repo.insert() | ||
|> case do | ||
{:error, _} -> {:error, :too_many_login_attempts} | ||
attempt -> attempt | ||
end | ||
@behaviour Sanbase.Accounts.AccessAttemptBehaviour | ||
alias Sanbase.Accounts.AccessAttempt | ||
|
||
@impl true | ||
def type, do: "email_login" | ||
|
||
@impl true | ||
def config do | ||
%{ | ||
interval_in_minutes: 5, | ||
allowed_user_attempts: 5, | ||
allowed_ip_attempts: 20 | ||
} | ||
end | ||
|
||
def changeset(%__MODULE__{} = attempt, attrs \\ %{}) do | ||
attempt | ||
|> cast(attrs, [:user_id, :ip_address]) | ||
|> validate_required([:user_id, :ip_address]) | ||
|> foreign_key_constraint(:user_id) | ||
@impl true | ||
def check_attempt_limit(user, remote_ip) do | ||
AccessAttempt.check_attempt_limit(type(), user, remote_ip) | ||
end | ||
|
||
# Private | ||
defp login_attempts_count(remote_ip) when is_binary(remote_ip) do | ||
interval_limit = Timex.shift(Timex.now(), minutes: -@interval_in_minutes) | ||
|
||
from(attempt in __MODULE__, | ||
where: attempt.ip_address == ^remote_ip and attempt.inserted_at > ^interval_limit | ||
) | ||
|> Repo.aggregate(:count, :id) | ||
end | ||
|
||
defp login_attempts_count(%{id: user_id}) do | ||
interval_limit = Timex.shift(Timex.now(), minutes: -@interval_in_minutes) | ||
|
||
from(attempt in __MODULE__, | ||
where: attempt.user_id == ^user_id and attempt.inserted_at > ^interval_limit | ||
) | ||
|> Repo.aggregate(:count, :id) | ||
@impl true | ||
def create(user, remote_ip) do | ||
AccessAttempt.create(type(), user, remote_ip) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
priv/repo/migrations/20250121155544_add_access_attempts.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Sanbase.Repo.Migrations.AddAccessAttempts do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:access_attempts) do | ||
add(:user_id, references(:users), null: true) | ||
add(:ip_address, :string, null: false) | ||
add(:type, :string, null: false) | ||
|
||
timestamps() | ||
end | ||
|
||
create(index(:access_attempts, [:type, :ip_address, :inserted_at])) | ||
create(index(:access_attempts, [:type, :user_id, :inserted_at])) | ||
end | ||
end |
Oops, something went wrong.