refactor: use client_id as primary key

This commit is contained in:
Ivan R. 2024-09-06 13:12:30 +05:00
parent d09bcf646e
commit ba4e90ef51
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
12 changed files with 67 additions and 44 deletions

6
lib/comfycamp/rand.ex Normal file
View file

@ -0,0 +1,6 @@
defmodule Comfycamp.Rand do
def get_random_string(length) do
:crypto.strong_rand_bytes(length)
|> Base.url_encode64()
end
end

View file

@ -2,10 +2,11 @@ defmodule Comfycamp.SSO.OIDCApp do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@derive {Phoenix.Param, key: :client_id}
@primary_key {:client_id, :string, autogenerate: false}
schema "oidc_apps" do schema "oidc_apps" do
field :enabled, :boolean, default: false field :enabled, :boolean, default: false
field :name, :string field :name, :string
field :client_id, :string
field :client_secret, :string field :client_secret, :string
timestamps(type: :utc_datetime) timestamps(type: :utc_datetime)

View file

@ -3,6 +3,7 @@ defmodule ComfycampWeb.OIDCAppController do
alias Comfycamp.SSO alias Comfycamp.SSO
alias Comfycamp.SSO.OIDCApp alias Comfycamp.SSO.OIDCApp
alias Comfycamp.Rand
def index(conn, _params) do def index(conn, _params) do
oidc_apps = SSO.list_oidc_apps() oidc_apps = SSO.list_oidc_apps()
@ -13,7 +14,10 @@ defmodule ComfycampWeb.OIDCAppController do
end end
def new(conn, _params) do def new(conn, _params) do
changeset = SSO.change_oidc_app(%OIDCApp{}) changeset = SSO.change_oidc_app(%OIDCApp{
client_id: Rand.get_random_string(20),
client_secret: Rand.get_random_string(32),
})
conn conn
|> put_layout(html: :admin) |> put_layout(html: :admin)
@ -34,16 +38,16 @@ defmodule ComfycampWeb.OIDCAppController do
end end
end end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => client_id}) do
oidc_app = SSO.get_oidc_app!(id) oidc_app = SSO.get_oidc_app!(client_id)
conn conn
|> put_layout(html: :admin) |> put_layout(html: :admin)
|> render(:show, oidc_app: oidc_app) |> render(:show, oidc_app: oidc_app)
end end
def edit(conn, %{"id" => id}) do def edit(conn, %{"id" => client_id}) do
oidc_app = SSO.get_oidc_app!(id) oidc_app = SSO.get_oidc_app!(client_id)
changeset = SSO.change_oidc_app(oidc_app) changeset = SSO.change_oidc_app(oidc_app)
conn conn
@ -51,8 +55,8 @@ defmodule ComfycampWeb.OIDCAppController do
|> render(:edit, oidc_app: oidc_app, changeset: changeset) |> render(:edit, oidc_app: oidc_app, changeset: changeset)
end end
def update(conn, %{"id" => id, "oidc_app" => oidc_app_params}) do def update(conn, %{"id" => client_id, "oidc_app" => oidc_app_params}) do
oidc_app = SSO.get_oidc_app!(id) oidc_app = SSO.get_oidc_app!(client_id)
case SSO.update_oidc_app(oidc_app, oidc_app_params) do case SSO.update_oidc_app(oidc_app, oidc_app_params) do
{:ok, oidc_app} -> {:ok, oidc_app} ->
@ -65,8 +69,8 @@ defmodule ComfycampWeb.OIDCAppController do
end end
end end
def delete(conn, %{"id" => id}) do def delete(conn, %{"id" => client_id}) do
oidc_app = SSO.get_oidc_app!(id) oidc_app = SSO.get_oidc_app!(client_id)
{:ok, _oidc_app} = SSO.delete_oidc_app(oidc_app) {:ok, _oidc_app} = SSO.delete_oidc_app(oidc_app)
conn conn

View file

@ -1,10 +1,10 @@
<div> <div>
<.header> <.header>
Edit Oidc app <%= @oidc_app.id %> Edit OpenID app "<%= @oidc_app.name %>"
<:subtitle>Use this form to manage oidc_app records in your database.</:subtitle> <:subtitle>Use this form to manage oidc_app records in your database.</:subtitle>
</.header> </.header>
<.oidc_app_form changeset={@changeset} action={~p"/admin/oidc_apps/#{@oidc_app}"} /> <.back navigate={~p"/admin/oidc_apps"}>Back to OpenID apps</.back>
<.back navigate={~p"/admin/oidc_apps"}>Back to oidc_apps</.back> <.oidc_app_form changeset={@changeset} action={~p"/admin/oidc_apps/#{@oidc_app}"} />
</div> </div>

View file

@ -1,9 +1,9 @@
<div> <div>
<.header> <.header>
Listing Oidc apps Listing OpenID Connect apps
<:actions> <:actions>
<.link href={~p"/admin/oidc_apps/new"}> <.link href={~p"/admin/oidc_apps/new"}>
<.button>New Oidc app</.button> <.button>New OpenID Connect app</.button>
</.link> </.link>
</:actions> </:actions>
</.header> </.header>

View file

@ -1,10 +1,10 @@
<div> <div>
<.header> <.header>
New Oidc app New OpenID Connect app
<:subtitle>Use this form to manage oidc_app records in your database.</:subtitle> <:subtitle>Use this form to manage oidc_app records in your database.</:subtitle>
</.header> </.header>
<.oidc_app_form changeset={@changeset} action={~p"/admin/oidc_apps"} /> <.back navigate={~p"/admin/oidc_apps"}>Back to OpenID apps</.back>
<.back navigate={~p"/admin/oidc_apps"}>Back to oidc_apps</.back> <.oidc_app_form changeset={@changeset} action={~p"/admin/oidc_apps"} />
</div> </div>

View file

@ -3,9 +3,9 @@
<.error :if={@changeset.action}> <.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below. Oops, something went wrong! Please check the errors below.
</.error> </.error>
<.input field={f[:client_id]} type="text" label="Client ID" readonly />
<.input field={f[:client_secret]} type="password" label="Client secret" readonly />
<.input field={f[:name]} type="text" label="Name" /> <.input field={f[:name]} type="text" label="Name" />
<.input field={f[:client_id]} type="text" label="Client" />
<.input field={f[:client_secret]} type="text" label="Client secret" />
<.input field={f[:enabled]} type="checkbox" label="Enabled" /> <.input field={f[:enabled]} type="checkbox" label="Enabled" />
<:actions> <:actions>
<.button>Save Oidc app</.button> <.button>Save Oidc app</.button>

View file

@ -1,10 +1,10 @@
<div> <div>
<.header> <.header>
Oidc app <%= @oidc_app.id %> OpenID app "<%= @oidc_app.name %>"
<:subtitle>This is a oidc_app record from your database.</:subtitle> <:subtitle>This is a oidc_app record from your database.</:subtitle>
<:actions> <:actions>
<.link href={~p"/admin/oidc_apps/#{@oidc_app}/edit"}> <.link href={~p"/admin/oidc_apps/#{@oidc_app}/edit"}>
<.button>Edit oidc_app</.button> <.button>Edit OpenID app</.button>
</.link> </.link>
</:actions> </:actions>
</.header> </.header>
@ -16,5 +16,5 @@
<:item title="Enabled"><%= @oidc_app.enabled %></:item> <:item title="Enabled"><%= @oidc_app.enabled %></:item>
</.list> </.list>
<.back navigate={~p"/admin/oidc_apps"}>Back to oidc_apps</.back> <.back navigate={~p"/admin/oidc_apps"}>Back to OpenID apps</.back>
</div> </div>

View file

@ -0,0 +1,12 @@
defmodule Comfycamp.Repo.Migrations.AlterOidcAppId do
use Ecto.Migration
def change do
drop(constraint("oidc_apps", "oidc_apps_pkey"))
alter table(:oidc_apps) do
modify(:client_id, :string, primary_key: true)
remove :id
end
end
end

View file

@ -17,7 +17,7 @@ defmodule Comfycamp.SSOTest do
test "get_oidc_app!/1 returns the oidc_app with given id" do test "get_oidc_app!/1 returns the oidc_app with given id" do
oidc_app = oidc_app_fixture() oidc_app = oidc_app_fixture()
assert SSO.get_oidc_app!(oidc_app.id) == oidc_app assert SSO.get_oidc_app!(oidc_app.client_id) == oidc_app
end end
test "create_oidc_app/1 with valid data creates a oidc_app" do test "create_oidc_app/1 with valid data creates a oidc_app" do
@ -59,13 +59,13 @@ defmodule Comfycamp.SSOTest do
test "update_oidc_app/2 with invalid data returns error changeset" do test "update_oidc_app/2 with invalid data returns error changeset" do
oidc_app = oidc_app_fixture() oidc_app = oidc_app_fixture()
assert {:error, %Ecto.Changeset{}} = SSO.update_oidc_app(oidc_app, @invalid_attrs) assert {:error, %Ecto.Changeset{}} = SSO.update_oidc_app(oidc_app, @invalid_attrs)
assert oidc_app == SSO.get_oidc_app!(oidc_app.id) assert oidc_app == SSO.get_oidc_app!(oidc_app.client_id)
end end
test "delete_oidc_app/1 deletes the oidc_app" do test "delete_oidc_app/1 deletes the oidc_app" do
oidc_app = oidc_app_fixture() oidc_app = oidc_app_fixture()
assert {:ok, %OIDCApp{}} = SSO.delete_oidc_app(oidc_app) assert {:ok, %OIDCApp{}} = SSO.delete_oidc_app(oidc_app)
assert_raise Ecto.NoResultsError, fn -> SSO.get_oidc_app!(oidc_app.id) end assert_raise Ecto.NoResultsError, fn -> SSO.get_oidc_app!(oidc_app.client_id) end
end end
test "change_oidc_app/1 returns a oidc_app changeset" do test "change_oidc_app/1 returns a oidc_app changeset" do

View file

@ -4,16 +4,16 @@ defmodule ComfycampWeb.OIDCAppControllerTest do
import Comfycamp.SSOFixtures import Comfycamp.SSOFixtures
@create_attrs %{ @create_attrs %{
enabled: true, client_id: "some_client_id",
client_secret: "some client_secret",
name: "some name", name: "some name",
client_id: "some client_id", enabled: true
client_secret: "some client_secret"
} }
@update_attrs %{ @update_attrs %{
enabled: false, client_id: "some_client_id",
client_secret: "some updated client_secret",
name: "some updated name", name: "some updated name",
client_id: "some updated client_id", enabled: false
client_secret: "some updated client_secret"
} }
@invalid_attrs %{enabled: nil, name: nil, client_id: nil, client_secret: nil} @invalid_attrs %{enabled: nil, name: nil, client_id: nil, client_secret: nil}
@ -22,7 +22,7 @@ defmodule ComfycampWeb.OIDCAppControllerTest do
test "lists all oidc_apps", %{conn: conn} do test "lists all oidc_apps", %{conn: conn} do
conn = get(conn, ~p"/admin/oidc_apps") conn = get(conn, ~p"/admin/oidc_apps")
assert html_response(conn, 200) =~ "Listing Oidc apps" assert html_response(conn, 200) =~ "Listing OpenID Connect apps"
end end
end end
@ -31,7 +31,7 @@ defmodule ComfycampWeb.OIDCAppControllerTest do
test "renders form", %{conn: conn} do test "renders form", %{conn: conn} do
conn = get(conn, ~p"/admin/oidc_apps/new") conn = get(conn, ~p"/admin/oidc_apps/new")
assert html_response(conn, 200) =~ "New Oidc app" assert html_response(conn, 200) =~ "New OpenID Connect app"
end end
end end
@ -41,16 +41,16 @@ defmodule ComfycampWeb.OIDCAppControllerTest do
test "redirects to show when data is valid", %{conn: conn} do test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, ~p"/admin/oidc_apps", oidc_app: @create_attrs) conn = post(conn, ~p"/admin/oidc_apps", oidc_app: @create_attrs)
assert %{id: id} = redirected_params(conn) assert %{id: client_id} = redirected_params(conn)
assert redirected_to(conn) == ~p"/admin/oidc_apps/#{id}" assert redirected_to(conn) == ~p"/admin/oidc_apps/#{client_id}"
conn = get(conn, ~p"/admin/oidc_apps/#{id}") conn = get(conn, ~p"/admin/oidc_apps/#{client_id}")
assert html_response(conn, 200) =~ "Oidc app #{id}" assert html_response(conn, 200) =~ "OpenID app"
end end
test "renders errors when data is invalid", %{conn: conn} do test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, ~p"/admin/oidc_apps", oidc_app: @invalid_attrs) conn = post(conn, ~p"/admin/oidc_apps", oidc_app: @invalid_attrs)
assert html_response(conn, 200) =~ "New Oidc app" assert html_response(conn, 200) =~ "New OpenID Connect app"
end end
end end
@ -59,7 +59,7 @@ defmodule ComfycampWeb.OIDCAppControllerTest do
test "renders form for editing chosen oidc_app", %{conn: conn, oidc_app: oidc_app} do test "renders form for editing chosen oidc_app", %{conn: conn, oidc_app: oidc_app} do
conn = get(conn, ~p"/admin/oidc_apps/#{oidc_app}/edit") conn = get(conn, ~p"/admin/oidc_apps/#{oidc_app}/edit")
assert html_response(conn, 200) =~ "Edit Oidc app" assert html_response(conn, 200) =~ "Edit OpenID app"
end end
end end
@ -76,7 +76,7 @@ defmodule ComfycampWeb.OIDCAppControllerTest do
test "renders errors when data is invalid", %{conn: conn, oidc_app: oidc_app} do test "renders errors when data is invalid", %{conn: conn, oidc_app: oidc_app} do
conn = put(conn, ~p"/admin/oidc_apps/#{oidc_app}", oidc_app: @invalid_attrs) conn = put(conn, ~p"/admin/oidc_apps/#{oidc_app}", oidc_app: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Oidc app" assert html_response(conn, 200) =~ "Edit OpenID app"
end end
end end

View file

@ -11,10 +11,10 @@ defmodule Comfycamp.SSOFixtures do
{:ok, oidc_app} = {:ok, oidc_app} =
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
client_id: "some client_id", client_id: "some_client_id",
client_secret: "some client_secret", client_secret: "some client_secret",
enabled: true, name: "some name",
name: "some name" enabled: true
}) })
|> Comfycamp.SSO.create_oidc_app() |> Comfycamp.SSO.create_oidc_app()