refactor: simplify client id and client secret generation
This commit is contained in:
parent
ba4e90ef51
commit
c1a4b839bd
7 changed files with 17 additions and 20 deletions
|
@ -1,6 +1,9 @@
|
||||||
defmodule Comfycamp.Rand do
|
defmodule Comfycamp.Rand do
|
||||||
|
@chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |> String.codepoints()
|
||||||
|
|
||||||
def get_random_string(length) do
|
def get_random_string(length) do
|
||||||
:crypto.strong_rand_bytes(length)
|
1..length
|
||||||
|> Base.url_encode64()
|
|> Enum.map(fn _i -> Enum.random(@chars) end)
|
||||||
|
|> Enum.join("")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,7 @@ defmodule Comfycamp.SSO do
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Comfycamp.Repo
|
alias Comfycamp.Repo
|
||||||
|
alias Comfycamp.Rand
|
||||||
|
|
||||||
alias Comfycamp.SSO.OIDCApp
|
alias Comfycamp.SSO.OIDCApp
|
||||||
|
|
||||||
|
@ -50,7 +51,12 @@ defmodule Comfycamp.SSO do
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_oidc_app(attrs \\ %{}) 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)
|
|> OIDCApp.changeset(attrs)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,10 +15,8 @@ defmodule Comfycamp.SSO.OIDCApp do
|
||||||
@doc false
|
@doc false
|
||||||
def changeset(oidc_app, attrs) do
|
def changeset(oidc_app, attrs) do
|
||||||
oidc_app
|
oidc_app
|
||||||
|> cast(attrs, [:name, :client_id, :client_secret, :enabled])
|
|> cast(attrs, [:name, :enabled])
|
||||||
|> validate_required([:name, :client_id, :client_secret, :enabled])
|
|> validate_required([:name, :enabled])
|
||||||
|> validate_length(:name, min: 2)
|
|> validate_length(:name, min: 2, max: 48)
|
||||||
|> validate_length(:client_id, min: 8)
|
|
||||||
|> validate_length(:client_secret, min: 12)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,6 @@ 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()
|
||||||
|
@ -14,10 +13,7 @@ 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)
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
<.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[:enabled]} type="checkbox" label="Enabled" />
|
<.input field={f[:enabled]} type="checkbox" label="Enabled" />
|
||||||
<:actions>
|
<:actions>
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
<.list>
|
<.list>
|
||||||
<:item title="Name"><%= @oidc_app.name %></:item>
|
<:item title="Name"><%= @oidc_app.name %></:item>
|
||||||
<:item title="Client"><%= @oidc_app.client_id %></:item>
|
<:item title="Client ID"><%= @oidc_app.client_id %></:item>
|
||||||
<:item title="Client secret"><%= @oidc_app.client_secret %></:item>
|
<:item title="Client secret"><%= @oidc_app.client_secret %></:item>
|
||||||
<:item title="Enabled"><%= @oidc_app.enabled %></:item>
|
<:item title="Enabled"><%= @oidc_app.enabled %></:item>
|
||||||
</.list>
|
</.list>
|
||||||
|
|
|
@ -31,8 +31,6 @@ defmodule Comfycamp.SSOTest do
|
||||||
assert {:ok, %OIDCApp{} = oidc_app} = SSO.create_oidc_app(valid_attrs)
|
assert {:ok, %OIDCApp{} = oidc_app} = SSO.create_oidc_app(valid_attrs)
|
||||||
assert oidc_app.enabled == true
|
assert oidc_app.enabled == true
|
||||||
assert oidc_app.name == "some name"
|
assert oidc_app.name == "some name"
|
||||||
assert oidc_app.client_id == "some client_id"
|
|
||||||
assert oidc_app.client_secret == "some client_secret"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create_oidc_app/1 with invalid data returns error changeset" do
|
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 {:ok, %OIDCApp{} = oidc_app} = SSO.update_oidc_app(oidc_app, update_attrs)
|
||||||
assert oidc_app.enabled == false
|
assert oidc_app.enabled == false
|
||||||
assert oidc_app.name == "some updated name"
|
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
|
end
|
||||||
|
|
||||||
test "update_oidc_app/2 with invalid data returns error changeset" do
|
test "update_oidc_app/2 with invalid data returns error changeset" do
|
||||||
|
|
Loading…
Reference in a new issue