diff --git a/lib/comfycamp/rand.ex b/lib/comfycamp/rand.ex index ae4e27d..6bc1a1a 100644 --- a/lib/comfycamp/rand.ex +++ b/lib/comfycamp/rand.ex @@ -1,6 +1,9 @@ defmodule Comfycamp.Rand do + @chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |> String.codepoints() + def get_random_string(length) do - :crypto.strong_rand_bytes(length) - |> Base.url_encode64() + 1..length + |> Enum.map(fn _i -> Enum.random(@chars) end) + |> Enum.join("") end end diff --git a/lib/comfycamp/sso.ex b/lib/comfycamp/sso.ex index b4e6174..8cb5090 100644 --- a/lib/comfycamp/sso.ex +++ b/lib/comfycamp/sso.ex @@ -5,6 +5,7 @@ defmodule Comfycamp.SSO do import Ecto.Query, warn: false alias Comfycamp.Repo + alias Comfycamp.Rand alias Comfycamp.SSO.OIDCApp @@ -50,7 +51,12 @@ defmodule Comfycamp.SSO do """ def create_oidc_app(attrs \\ %{}) do - %OIDCApp{} + app = %OIDCApp{ + client_id: Rand.get_random_string(20), + client_secret: Rand.get_random_string(32) + } + + app |> OIDCApp.changeset(attrs) |> Repo.insert() end diff --git a/lib/comfycamp/sso/oidc_app.ex b/lib/comfycamp/sso/oidc_app.ex index a27133e..d0dc376 100644 --- a/lib/comfycamp/sso/oidc_app.ex +++ b/lib/comfycamp/sso/oidc_app.ex @@ -15,10 +15,8 @@ defmodule Comfycamp.SSO.OIDCApp do @doc false def changeset(oidc_app, attrs) do oidc_app - |> cast(attrs, [:name, :client_id, :client_secret, :enabled]) - |> validate_required([:name, :client_id, :client_secret, :enabled]) - |> validate_length(:name, min: 2) - |> validate_length(:client_id, min: 8) - |> validate_length(:client_secret, min: 12) + |> cast(attrs, [:name, :enabled]) + |> validate_required([:name, :enabled]) + |> validate_length(:name, min: 2, max: 48) end end diff --git a/lib/comfycamp_web/controllers/oidc_app_controller.ex b/lib/comfycamp_web/controllers/oidc_app_controller.ex index 7317077..1660ce8 100644 --- a/lib/comfycamp_web/controllers/oidc_app_controller.ex +++ b/lib/comfycamp_web/controllers/oidc_app_controller.ex @@ -3,7 +3,6 @@ defmodule ComfycampWeb.OIDCAppController do alias Comfycamp.SSO alias Comfycamp.SSO.OIDCApp - alias Comfycamp.Rand def index(conn, _params) do oidc_apps = SSO.list_oidc_apps() @@ -14,10 +13,7 @@ defmodule ComfycampWeb.OIDCAppController do end def new(conn, _params) do - changeset = SSO.change_oidc_app(%OIDCApp{ - client_id: Rand.get_random_string(20), - client_secret: Rand.get_random_string(32), - }) + changeset = SSO.change_oidc_app(%OIDCApp{}) conn |> put_layout(html: :admin) diff --git a/lib/comfycamp_web/controllers/oidc_app_html/oidc_app_form.html.heex b/lib/comfycamp_web/controllers/oidc_app_html/oidc_app_form.html.heex index ad856de..da84b0b 100644 --- a/lib/comfycamp_web/controllers/oidc_app_html/oidc_app_form.html.heex +++ b/lib/comfycamp_web/controllers/oidc_app_html/oidc_app_form.html.heex @@ -3,8 +3,6 @@ <.error :if={@changeset.action}> Oops, something went wrong! Please check the errors below. - <.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[:enabled]} type="checkbox" label="Enabled" /> <:actions> diff --git a/lib/comfycamp_web/controllers/oidc_app_html/show.html.heex b/lib/comfycamp_web/controllers/oidc_app_html/show.html.heex index 001686d..38c08d6 100644 --- a/lib/comfycamp_web/controllers/oidc_app_html/show.html.heex +++ b/lib/comfycamp_web/controllers/oidc_app_html/show.html.heex @@ -11,7 +11,7 @@ <.list> <:item title="Name"><%= @oidc_app.name %> - <:item title="Client"><%= @oidc_app.client_id %> + <:item title="Client ID"><%= @oidc_app.client_id %> <:item title="Client secret"><%= @oidc_app.client_secret %> <:item title="Enabled"><%= @oidc_app.enabled %> diff --git a/test/comfycamp/sso_test.exs b/test/comfycamp/sso_test.exs index ec46802..e708f93 100644 --- a/test/comfycamp/sso_test.exs +++ b/test/comfycamp/sso_test.exs @@ -31,8 +31,6 @@ defmodule Comfycamp.SSOTest do assert {:ok, %OIDCApp{} = oidc_app} = SSO.create_oidc_app(valid_attrs) assert oidc_app.enabled == true assert oidc_app.name == "some name" - assert oidc_app.client_id == "some client_id" - assert oidc_app.client_secret == "some client_secret" end test "create_oidc_app/1 with invalid data returns error changeset" do @@ -52,8 +50,6 @@ defmodule Comfycamp.SSOTest do assert {:ok, %OIDCApp{} = oidc_app} = SSO.update_oidc_app(oidc_app, update_attrs) assert oidc_app.enabled == false assert oidc_app.name == "some updated name" - assert oidc_app.client_id == "some updated client_id" - assert oidc_app.client_secret == "some updated client_secret" end test "update_oidc_app/2 with invalid data returns error changeset" do