-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 #30 from blockscout/np-api-keys-2
API keys management
- Loading branch information
Showing
15 changed files
with
521 additions
and
6 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
apps/block_scout_web/lib/block_scout_web/controllers/account/api_key_controller.ex
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,68 @@ | ||
defmodule BlockScoutWeb.Account.ApiKeyController do | ||
use BlockScoutWeb, :controller | ||
|
||
alias Explorer.Account.Api.Key, as: ApiKey | ||
|
||
import BlockScoutWeb.Account.AuthController, only: [authenticate!: 1] | ||
|
||
def new(conn, _params) do | ||
authenticate!(conn) | ||
|
||
render(conn, "form.html", method: :create, api_key: empty_api_key()) | ||
end | ||
|
||
def create(conn, %{"key" => api_key}) do | ||
current_user = authenticate!(conn) | ||
|
||
case ApiKey.create_api_key_changeset_and_insert(%ApiKey{}, %{name: api_key["name"], identity_id: current_user.id}) do | ||
{:ok, _} -> | ||
redirect(conn, to: api_key_path(conn, :index)) | ||
|
||
{:error, invalid_api_key} -> | ||
render(conn, "form.html", method: :create, api_key: invalid_api_key) | ||
end | ||
end | ||
|
||
def create(conn, _) do | ||
redirect(conn, to: api_key_path(conn, :index)) | ||
end | ||
|
||
def index(conn, _params) do | ||
current_user = authenticate!(conn) | ||
|
||
render(conn, "index.html", api_keys: ApiKey.get_api_keys_by_identity_id(current_user.id)) | ||
end | ||
|
||
def edit(conn, %{"id" => api_key}) do | ||
current_user = authenticate!(conn) | ||
|
||
case ApiKey.api_key_by_value_and_identity_id(api_key, current_user.id) do | ||
nil -> | ||
conn | ||
|> put_status(:not_found) | ||
|> put_view(BlockScoutWeb.PageNotFoundView) | ||
|> render("index.html", token: nil) | ||
|
||
%ApiKey{} = api_key -> | ||
render(conn, "form.html", method: :update, api_key: ApiKey.changeset(api_key)) | ||
end | ||
end | ||
|
||
def update(conn, %{"id" => api_key, "key" => %{"value" => api_key, "name" => name}}) do | ||
current_user = authenticate!(conn) | ||
|
||
ApiKey.update_api_key_name(name, current_user.id, api_key) | ||
|
||
redirect(conn, to: api_key_path(conn, :index)) | ||
end | ||
|
||
def delete(conn, %{"id" => api_key}) do | ||
current_user = authenticate!(conn) | ||
|
||
ApiKey.delete_api_key(current_user.id, api_key) | ||
|
||
redirect(conn, to: api_key_path(conn, :index)) | ||
end | ||
|
||
defp empty_api_key, do: ApiKey.changeset() | ||
end |
34 changes: 34 additions & 0 deletions
34
apps/block_scout_web/lib/block_scout_web/templates/account/api_key/form.html.eex
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,34 @@ | ||
<section class="container"> | ||
<div class="row"> | ||
<%= render BlockScoutWeb.Account.CommonView, "_nav.html", conn: @conn %> | ||
<div class="col-sm-10"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<h1 class="card-title list-title-description header-account"><%=if @method == :update, do: gettext("Update"), else: gettext("Add") %> <%= gettext "API key"%></h1> | ||
<div class="col-sm-10 card-body-account"> | ||
<% path = if @method == :update, do: api_key_path(@conn, @method, @api_key.data.value), else: api_key_path(@conn, @method) %> | ||
<%= form_for @api_key, path, fn f -> %> | ||
<%= if f.data.value do %> | ||
<div class="form-group"> | ||
<%= label f, :value, gettext("API key"), class: "control-label", style: "font-size: 14px" %> | ||
<%= text_input f, :value, class: "form-control", placeholder: gettext("API key"), readonly: true %> | ||
<%= error_tag f, :value %> | ||
</div> | ||
<% end %> | ||
<div class="form-group"> | ||
<%= label f, :name, gettext("Name"), class: "control-label", style: "font-size: 14px" %> | ||
<%= text_input f, :name, class: "form-control", placeholder: gettext("Name this API key") %> | ||
<%= error_tag f, :name %> | ||
</div> | ||
<br> | ||
<div class="form-group float-right form-input"> | ||
<a class="btn btn-line" href="<%= api_key_path(@conn, :index) %>"><%= gettext "Back to API keys (Cancel)"%></a> | ||
<%= submit gettext("Save"), class: "button button-primary button-sm ml-3" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</section> |
44 changes: 44 additions & 0 deletions
44
apps/block_scout_web/lib/block_scout_web/templates/account/api_key/index.html.eex
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,44 @@ | ||
<section class="container"> | ||
<div class="row"> | ||
<%= render BlockScoutWeb.Account.CommonView, "_nav.html", conn: @conn %> | ||
<div class="col-md"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<h1 class="card-title list-title-description header-account"><%= gettext "Api keys" %> </h1> | ||
<br> | ||
<div class="col-sm"> | ||
<div class="mb-3 row o-flow-x"> | ||
<%= if @api_keys == [] do %> | ||
<div style="min-width: 100%;"> | ||
<div class="tile tile-muted text-center" data-selector="empty-coin-balances-list"> | ||
<%= gettext "You don't have API keys yet" %> | ||
</div> | ||
</div> | ||
<h2></h2> | ||
<% else %> | ||
<table class="table mb-3 table-watchlist"> | ||
<thead style="font-size: 14px; color: #6c757d" > | ||
<tr> | ||
<th scope="col"><%= gettext "Name" %></th> | ||
<th scope="col"><%= gettext "API key" %></th> | ||
<th scope="col"></th> | ||
<th scope="col"></th> | ||
</tr> | ||
</thead> | ||
<tbody style="font-size: 15px; color: #6c757d" > | ||
<%= Enum.map(@api_keys, fn key -> | ||
render("row.html", api_key: key, conn: @conn) | ||
end) %> | ||
</tbody> | ||
</table> | ||
<% end %> | ||
</div> | ||
</div> | ||
<%= if Enum.count(@api_keys) < 3 do %> | ||
<a class="button button-primary button-sm" href="<%= api_key_path(@conn, :new) %>"><%= gettext "Add API key" %></a> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</section> |
14 changes: 14 additions & 0 deletions
14
apps/block_scout_web/lib/block_scout_web/templates/account/api_key/row.html.eex
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,14 @@ | ||
<tr> | ||
<td><%= @api_key.name %></td> | ||
<td> | ||
<span><%= @api_key.value %></span> | ||
<%= render BlockScoutWeb.CommonComponentsView, "_btn_copy.html", | ||
additional_classes: ["btn-copy-icon-small", "btn-copy-icon-custom", "btn-copy-icon-no-borders"], clipboard_text: @api_key.value, aria_label: gettext("Copy API key"), title: gettext("Copy API key"), style: "display: inline-block; vertical-align: text-bottom; position: initial; margin-top: 1px;" %> | ||
</td> | ||
<td> | ||
<%= link gettext("Remove"), to: api_key_path(@conn, :delete, @api_key.value), method: :delete %> | ||
</td> | ||
<td> | ||
<%= link gettext("Edit"), to: api_key_path(@conn, :edit, @api_key.value) %> | ||
</td> | ||
</tr> |
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
3 changes: 3 additions & 0 deletions
3
apps/block_scout_web/lib/block_scout_web/views/account/api_key_view.ex
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 BlockScoutWeb.Account.ApiKeyView do | ||
use BlockScoutWeb, :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
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.