Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add RevokeToken * Checkpoint UUID ApiToken * Passing tests * github restore cache * Fix on_delete behavior
- Loading branch information
Showing
15 changed files
with
295 additions
and
9 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[codespell] | ||
skip = ./vendor,./omnibus,*.json,yarn.lock,seeds.exs,./docs/node_modules,./deps,./priv/static,./priv/plts,./**/priv/static,./.git,./docs/build,./_build | ||
skip = ./cover,./vendor,./omnibus,*.json,yarn.lock,seeds.exs,./docs/node_modules,./deps,./priv/static,./priv/plts,./**/priv/static,./.git,./docs/build,./_build | ||
ignore-words-list = keypair,keypairs,iif,statics,wee |
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,104 @@ | ||
defmodule FzHttp.ApiTokens do | ||
@moduledoc """ | ||
The ApiTokens context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias FzHttp.Repo | ||
|
||
alias FzHttp.ApiTokens.ApiToken | ||
|
||
@doc """ | ||
Returns the list of api_tokens. | ||
## Examples | ||
iex> list_api_tokens() | ||
[%ApiToken{}, ...] | ||
""" | ||
def list_api_tokens do | ||
Repo.all(ApiToken) | ||
end | ||
|
||
@doc """ | ||
Gets a single api_token. | ||
Raises `Ecto.NoResultsError` if the Api token does not exist. | ||
## Examples | ||
iex> get_api_token!(123) | ||
%ApiToken{} | ||
iex> get_api_token!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_api_token!(id), do: Repo.get!(ApiToken, id) | ||
|
||
@doc """ | ||
Creates a api_token. | ||
## Examples | ||
iex> create_api_token(%{field: value}) | ||
{:ok, %ApiToken{}} | ||
iex> create_api_token(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_api_token(attrs \\ %{}) do | ||
%ApiToken{} | ||
|> ApiToken.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a api_token. | ||
## Examples | ||
iex> update_api_token(api_token, %{field: new_value}) | ||
{:ok, %ApiToken{}} | ||
iex> update_api_token(api_token, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_api_token(%ApiToken{} = api_token, attrs) do | ||
api_token | ||
|> ApiToken.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a api_token. | ||
## Examples | ||
iex> delete_api_token(api_token) | ||
{:ok, %ApiToken{}} | ||
iex> delete_api_token(api_token) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_api_token(%ApiToken{} = api_token) do | ||
Repo.delete(api_token) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking api_token changes. | ||
## Examples | ||
iex> change_api_token(api_token) | ||
%Ecto.Changeset{data: %ApiToken{}} | ||
""" | ||
def change_api_token(%ApiToken{} = api_token, attrs \\ %{}) do | ||
ApiToken.changeset(api_token, attrs) | ||
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,31 @@ | ||
defmodule FzHttp.ApiTokens.ApiToken do | ||
@moduledoc """ | ||
Stores API Token metadata to check for revocation. | ||
""" | ||
|
||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias FzHttp.Users.User | ||
|
||
@primary_key {:id, :binary_id, autogenerate: true} | ||
|
||
schema "api_tokens" do | ||
field :revoked_at, :utc_datetime_usec | ||
|
||
belongs_to :user, User | ||
|
||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
@doc false | ||
def changeset(api_token, attrs) do | ||
api_token | ||
|> cast(attrs, [ | ||
:user_id, | ||
:revoked_at | ||
]) | ||
|> validate_required(:user_id) | ||
|> assoc_constraint(:user) | ||
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
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
apps/fz_http/priv/repo/migrations/20221126044850_create_api_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,16 @@ | ||
defmodule FzHttp.Repo.Migrations.CreateApiTokens do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:api_tokens, primary_key: false) do | ||
add(:id, :uuid, primary_key: true) | ||
add(:revoked_at, :utc_datetime_usec) | ||
add(:user_id, references(:users, on_delete: :delete_all), null: false) | ||
|
||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
create(index(:api_tokens, [:revoked_at])) | ||
create(index(:api_tokens, [:user_id])) | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
apps/fz_http/priv/repo/migrations/20221129002233_update_on_delete_behavior.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,33 @@ | ||
defmodule FzHttp.Repo.Migrations.UpdateOnDeleteBehavior do | ||
use Ecto.Migration | ||
|
||
def change do | ||
drop(constraint(:oidc_connections, "oidc_connections_user_id_fkey")) | ||
|
||
alter table(:oidc_connections) do | ||
modify( | ||
:user_id, | ||
references(:users, on_delete: :delete_all), | ||
null: false, | ||
from: { | ||
references(:users, on_delete: :nothing), | ||
null: false | ||
} | ||
) | ||
end | ||
|
||
drop(constraint(:mfa_methods, "mfa_methods_user_id_fkey")) | ||
|
||
alter table(:mfa_methods) do | ||
modify( | ||
:user_id, | ||
references(:users, on_delete: :delete_all), | ||
null: false, | ||
from: { | ||
references(:users, on_delete: :nothing), | ||
null: false | ||
} | ||
) | ||
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,63 @@ | ||
defmodule FzHttp.ApiTokensTest do | ||
use FzHttp.DataCase | ||
|
||
alias FzHttp.ApiTokens | ||
|
||
describe "api_tokens" do | ||
alias FzHttp.ApiTokens.ApiToken | ||
|
||
import FzHttp.ApiTokensFixtures | ||
import FzHttp.UsersFixtures | ||
|
||
@invalid_attrs %{user_id: nil} | ||
|
||
test "list_api_tokens/0 returns all api_tokens" do | ||
api_token = api_token_fixture() | ||
assert ApiTokens.list_api_tokens() == [api_token] | ||
end | ||
|
||
test "get_api_token!/1 returns the api_token with given id" do | ||
api_token = api_token_fixture() | ||
assert ApiTokens.get_api_token!(api_token.id) == api_token | ||
end | ||
|
||
test "create_api_token/1 with valid data creates a api_token" do | ||
valid_attrs = %{ | ||
user_id: user().id, | ||
revoked_at: ~U[2022-11-25 04:48:00.000000Z] | ||
} | ||
|
||
assert {:ok, %ApiToken{} = api_token} = ApiTokens.create_api_token(valid_attrs) | ||
assert api_token.revoked_at == ~U[2022-11-25 04:48:00.000000Z] | ||
end | ||
|
||
test "create_api_token/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = ApiTokens.create_api_token(@invalid_attrs) | ||
end | ||
|
||
test "update_api_token/2 with valid data updates the api_token" do | ||
api_token = api_token_fixture() | ||
update_attrs = %{revoked_at: ~U[2022-11-26 04:48:00.000000Z]} | ||
|
||
assert {:ok, %ApiToken{} = api_token} = ApiTokens.update_api_token(api_token, update_attrs) | ||
assert api_token.revoked_at == ~U[2022-11-26 04:48:00.000000Z] | ||
end | ||
|
||
test "update_api_token/2 with invalid data returns error changeset" do | ||
api_token = api_token_fixture() | ||
assert {:error, %Ecto.Changeset{}} = ApiTokens.update_api_token(api_token, @invalid_attrs) | ||
assert api_token == ApiTokens.get_api_token!(api_token.id) | ||
end | ||
|
||
test "delete_api_token/1 deletes the api_token" do | ||
api_token = api_token_fixture() | ||
assert {:ok, %ApiToken{}} = ApiTokens.delete_api_token(api_token) | ||
assert_raise Ecto.NoResultsError, fn -> ApiTokens.get_api_token!(api_token.id) end | ||
end | ||
|
||
test "change_api_token/1 returns a api_token changeset" do | ||
api_token = api_token_fixture() | ||
assert %Ecto.Changeset{} = ApiTokens.change_api_token(api_token) | ||
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,30 @@ | ||
defmodule FzHttp.ApiTokensFixtures do | ||
@moduledoc """ | ||
This module defines test helpers for creating | ||
entities via the `FzHttp.ApiTokens` context. | ||
""" | ||
|
||
@doc """ | ||
Generate a api_token. | ||
""" | ||
def api_token_fixture(attrs \\ %{}) do | ||
user_id = | ||
Map.get_lazy( | ||
attrs, | ||
:user_id, | ||
fn -> | ||
FzHttp.UsersFixtures.user().id | ||
end | ||
) | ||
|
||
{:ok, api_token} = | ||
attrs | ||
|> Enum.into(%{ | ||
user_id: user_id, | ||
revoked_at: ~U[2022-11-25 04:48:00.000000Z] | ||
}) | ||
|> FzHttp.ApiTokens.create_api_token() | ||
|
||
api_token | ||
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