-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mix phx.gen.html Apikey #42 (comment)
- Loading branch information
Showing
13 changed files
with
525 additions
and
1 deletion.
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,64 @@ | ||
defmodule Auth.Apikey do | ||
use Ecto.Schema | ||
import Ecto.Query, warn: false | ||
import Ecto.Changeset | ||
alias Auth.Repo | ||
# https://stackoverflow.com/a/47501059/1148249 | ||
alias __MODULE__ | ||
|
||
schema "apikeys" do | ||
field :client_secret, :binary | ||
field :client_id, :binary | ||
field :description, :string | ||
field :name, :string | ||
field :url, :binary | ||
field :person_id, :id | ||
field :status, :id | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(apikey, attrs) do | ||
apikey | ||
|> cast(attrs, [:client_id, :client_secret, :name, :description, :url, :person_id]) | ||
|> validate_required([:client_secret]) | ||
end | ||
|
||
def change_apikey(%Apikey{} = apikey) do | ||
Apikey.changeset(apikey, %{}) | ||
end | ||
|
||
def create_apikey(attrs \\ %{}) do | ||
%Apikey{} | ||
|> Apikey.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
def list_apikeys_for_person(person_id) do | ||
IO.inspect(person_id, label: "person_id") | ||
query = from( | ||
a in __MODULE__, | ||
where: a.person_id == ^person_id, | ||
select: a | ||
) | ||
Repo.all(query) | ||
end | ||
|
||
@doc """ | ||
Gets a single apikey. | ||
Raises `Ecto.NoResultsError` if the Apikey does not exist. | ||
## Examples | ||
iex> get_apikey!(123) | ||
%Apikey{} | ||
iex> get_apikey!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_apikey!(id), do: Repo.get!(__MODULE__, id) | ||
|
||
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,104 @@ | ||
# defmodule Auth.Ctx do | ||
# @moduledoc """ | ||
# The Ctx context. | ||
# """ | ||
# | ||
# import Ecto.Query, warn: false | ||
# alias Auth.Repo | ||
# | ||
# alias Auth.Ctx.Apikey | ||
# | ||
# @doc """ | ||
# Returns the list of apikeys. | ||
# | ||
# ## Examples | ||
# | ||
# iex> list_apikeys() | ||
# [%Apikey{}, ...] | ||
# | ||
# """ | ||
# def list_apikeys do | ||
# Repo.all(Apikey) | ||
# end | ||
# | ||
# @doc """ | ||
# Gets a single apikey. | ||
# | ||
# Raises `Ecto.NoResultsError` if the Apikey does not exist. | ||
# | ||
# ## Examples | ||
# | ||
# iex> get_apikey!(123) | ||
# %Apikey{} | ||
# | ||
# iex> get_apikey!(456) | ||
# ** (Ecto.NoResultsError) | ||
# | ||
# """ | ||
# def get_apikey!(id), do: Repo.get!(Apikey, id) | ||
# | ||
# @doc """ | ||
# Creates a apikey. | ||
# | ||
# ## Examples | ||
# | ||
# iex> create_apikey(%{field: value}) | ||
# {:ok, %Apikey{}} | ||
# | ||
# iex> create_apikey(%{field: bad_value}) | ||
# {:error, %Ecto.Changeset{}} | ||
# | ||
# """ | ||
# def create_apikey(attrs \\ %{}) do | ||
# %Apikey{} | ||
# |> Apikey.changeset(attrs) | ||
# |> Repo.insert() | ||
# end | ||
# | ||
# @doc """ | ||
# Updates a apikey. | ||
# | ||
# ## Examples | ||
# | ||
# iex> update_apikey(apikey, %{field: new_value}) | ||
# {:ok, %Apikey{}} | ||
# | ||
# iex> update_apikey(apikey, %{field: bad_value}) | ||
# {:error, %Ecto.Changeset{}} | ||
# | ||
# """ | ||
# def update_apikey(%Apikey{} = apikey, attrs) do | ||
# apikey | ||
# |> Apikey.changeset(attrs) | ||
# |> Repo.update() | ||
# end | ||
# | ||
# @doc """ | ||
# Deletes a apikey. | ||
# | ||
# ## Examples | ||
# | ||
# iex> delete_apikey(apikey) | ||
# {:ok, %Apikey{}} | ||
# | ||
# iex> delete_apikey(apikey) | ||
# {:error, %Ecto.Changeset{}} | ||
# | ||
# """ | ||
# def delete_apikey(%Apikey{} = apikey) do | ||
# Repo.delete(apikey) | ||
# end | ||
# | ||
# @doc """ | ||
# Returns an `%Ecto.Changeset{}` for tracking apikey changes. | ||
# | ||
# ## Examples | ||
# | ||
# iex> change_apikey(apikey) | ||
# %Ecto.Changeset{source: %Apikey{}} | ||
# | ||
# """ | ||
# def change_apikey(%Apikey{} = apikey) do | ||
# Apikey.changeset(apikey, %{}) | ||
# 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,74 @@ | ||
defmodule AuthWeb.ApikeyController do | ||
use AuthWeb, :controller | ||
alias Auth.Apikey | ||
|
||
def index(conn, _params) do | ||
|
||
person_id = conn.assigns.decoded.id | ||
apikeys = Apikey.list_apikeys_for_person(person_id) | ||
render(conn, "index.html", apikeys: apikeys) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Apikey.change_apikey(%Apikey{}) | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"apikey" => apikey_params}) do | ||
IO.inspect(apikey_params, label: "apikey_params") | ||
person_id = conn.assigns.decoded.id | ||
secret = Fields.AES.encrypt(person_id) |> Base.encode64 | ||
|> IO.inspect(label: "secret") | ||
|
||
client = Fields.AES.encrypt(person_id) |> Base.encode64 | ||
|> IO.inspect(label: "client") | ||
params = Map.merge(apikey_params, %{ | ||
"client_secret" => secret, | ||
"client_id" => client, | ||
"person_id" => person_id | ||
}) | ||
case Apikey.create_apikey(params) do | ||
{:ok, apikey} -> | ||
conn | ||
|> put_flash(:info, "Apikey created successfully.") | ||
|> redirect(to: Routes.apikey_path(conn, :show, apikey)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
apikey = Apikey.get_apikey!(id) | ||
render(conn, "show.html", apikey: apikey) | ||
end | ||
|
||
# def edit(conn, %{"id" => id}) do | ||
# apikey = Ctx.get_apikey!(id) | ||
# changeset = Ctx.change_apikey(apikey) | ||
# render(conn, "edit.html", apikey: apikey, changeset: changeset) | ||
# end | ||
|
||
# def update(conn, %{"id" => id, "apikey" => apikey_params}) do | ||
# apikey = Ctx.get_apikey!(id) | ||
# | ||
# case Ctx.update_apikey(apikey, apikey_params) do | ||
# {:ok, apikey} -> | ||
# conn | ||
# |> put_flash(:info, "Apikey updated successfully.") | ||
# |> redirect(to: Routes.apikey_path(conn, :show, apikey)) | ||
# | ||
# {:error, %Ecto.Changeset{} = changeset} -> | ||
# render(conn, "edit.html", apikey: apikey, changeset: changeset) | ||
# end | ||
# end | ||
# | ||
# def delete(conn, %{"id" => id}) do | ||
# apikey = Ctx.get_apikey!(id) | ||
# {:ok, _apikey} = Ctx.delete_apikey(apikey) | ||
# | ||
# conn | ||
# |> put_flash(:info, "Apikey deleted successfully.") | ||
# |> redirect(to: Routes.apikey_path(conn, :index)) | ||
# 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>Edit Apikey</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.apikey_path(@conn, :update, @apikey)) %> | ||
|
||
<span><%= link "Back", to: Routes.apikey_path(@conn, :index) %></span> |
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,23 @@ | ||
<%= form_for @changeset, @action, fn f -> %> | ||
<%= if @changeset.action do %> | ||
<div class="alert alert-danger"> | ||
<p>Oops, something went wrong! Please check the errors below.</p> | ||
</div> | ||
<% end %> | ||
|
||
<%= label f, :name %> | ||
<%= text_input f, :name %> | ||
<%= error_tag f, :name %> | ||
|
||
<%= label f, :description %> | ||
<%= textarea f, :description %> | ||
<%= error_tag f, :description %> | ||
|
||
<%= label f, :url %> | ||
<%= text_input f, :url %> | ||
<%= error_tag f, :url %> | ||
|
||
<div> | ||
<%= submit "Save" %> | ||
</div> | ||
<% 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,32 @@ | ||
<h1>API Keys</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>DWYL_API_KEY</th> | ||
<th>Name</th> | ||
<th>Description</th> | ||
<th>Url</th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for apikey <- @apikeys do %> | ||
<tr> | ||
<td><%= apikey.client_id %><%= apikey.client_secret %></td> | ||
<td><%= apikey.name %></td> | ||
<td><%= apikey.description %></td> | ||
<td><%= apikey.url %></td> | ||
|
||
<td> | ||
<span><%= link "Show", to: Routes.apikey_path(@conn, :show, apikey) %></span> | ||
<span><%= link "Edit", to: Routes.apikey_path(@conn, :edit, apikey) %></span> | ||
<span><%= link "Delete", to: Routes.apikey_path(@conn, :delete, apikey), method: :delete, data: [confirm: "Are you sure?"] %></span> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<span><%= link "New Apikey", to: Routes.apikey_path(@conn, :new) %></span> |
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,5 @@ | ||
<h1>New Apikey</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.apikey_path(@conn, :create)) %> | ||
|
||
<span><%= link "Back", to: Routes.apikey_path(@conn, :index) %></span> |
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 @@ | ||
<h1>Show Apikey</h1> | ||
|
||
<div> | ||
<p> | ||
<strong>DWYL_API_KEY:</strong> | ||
<div style="width:420px; font-size: 1.2em; | ||
overflow-wrap: break-word; font-family: monospace; | ||
border: 1px solid black; padding: 10px; background-color: #EBEDEF;"> | ||
<%= @apikey.client_id %><%= @apikey.client_secret %> | ||
</div> | ||
</p> | ||
|
||
<p> | ||
<strong>Name:</strong> | ||
<%= @apikey.name %> | ||
</p> | ||
|
||
<p> | ||
<strong>Description:</strong> | ||
<%= @apikey.description %> | ||
</p> | ||
|
||
<p> | ||
<strong>Url:</strong> | ||
<%= @apikey.url %> | ||
</p> | ||
|
||
</div> | ||
|
||
<span><%= link "Edit", to: Routes.apikey_path(@conn, :edit, @apikey) %></span> | ||
<span><%= link "Back", to: Routes.apikey_path(@conn, :index) %></span> |
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,3 @@ | ||
defmodule AuthWeb.ApikeyView do | ||
use AuthWeb, :view | ||
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,20 @@ | ||
defmodule Auth.Repo.Migrations.CreateApikeys do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:apikeys) do | ||
add :client_id, :binary | ||
add :client_secret, :binary | ||
add :name, :string | ||
add :description, :text | ||
add :url, :binary | ||
add :person_id, references(:people, on_delete: :nothing) | ||
add :status, references(:status, on_delete: :nothing) | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:apikeys, [:person_id]) | ||
create index(:apikeys, [:status]) | ||
end | ||
end |
Oops, something went wrong.