-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
378 additions
and
4 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
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,33 @@ | ||
defmodule POABackend.Auth.BanTokensServer do | ||
@moduledoc false | ||
|
||
alias POABackend.Auth | ||
|
||
use GenServer | ||
|
||
@frequency 60 * 60 * 24 * 1000 # one day by default | ||
|
||
def start_link() do | ||
GenServer.start_link(__MODULE__, :noargs, name: __MODULE__) | ||
end | ||
|
||
def init(:noargs) do | ||
frequency = Application.get_env(:poa_backend, :purge_banned_tokens_freq, @frequency) | ||
|
||
set_purge_timer(frequency) | ||
|
||
{:ok, %{frequency: frequency}} | ||
end | ||
|
||
def handle_info(:purge, %{frequency: frequency} = state) do | ||
:ok = Auth.purge_banned_tokens() | ||
|
||
set_purge_timer(frequency) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
defp set_purge_timer(frequency) do | ||
Process.send_after(self(), :purge, frequency) | ||
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,22 @@ | ||
defmodule POABackend.Auth.Models.Token do | ||
use Ecto.Schema | ||
|
||
@moduledoc """ | ||
This module encapsulates the _Token_ model | ||
""" | ||
|
||
@primary_key {:token, :string, []} | ||
|
||
schema "bannedtokens" do | ||
field :expires, :integer | ||
|
||
timestamps() | ||
end | ||
|
||
@type t :: %__MODULE__{token: String.t, | ||
expires: Integer.t} | ||
|
||
def new(token, expires) do | ||
%__MODULE__{token: token, expires: expires} | ||
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
12 changes: 12 additions & 0 deletions
12
priv/repo/migrations/20180809170931_create_banned_tokens.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,12 @@ | ||
defmodule POABackend.Auth.Repo.Migrations.CreateBannedTokens do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create_if_not_exists table(:bannedtokens, primary_key: false) do | ||
add :token, :string, primary_key: true | ||
add :expires, :integer | ||
|
||
timestamps() | ||
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
Oops, something went wrong.