feat: add oidc login using keycloak

This commit is contained in:
Moritz Böhme 2025-04-05 14:03:27 +02:00
parent 630aec22cf
commit 00d15dfd47
2 changed files with 44 additions and 1 deletions

View file

@ -20,6 +20,21 @@ defmodule Putzplan.Accounts.User do
store_all_tokens? true
require_token_presence_for_authentication? true
end
strategies do
oidc :oidc do
client_id "putzplan"
base_url "http://localhost:8080/realms/master/"
client_secret "Kc3DkJiIrIr59HQhDmneqqB3iy6H8gxH"
nonce true
redirect_uri "http://localhost:4000/auth"
authorization_params [scope: "profile email"]
end
end
end
identities do
identity :id, [:id]
end
sqlite do
@ -36,6 +51,24 @@ defmodule Putzplan.Accounts.User do
get? true
prepare AshAuthentication.Preparations.FilterBySubject
end
create :register_with_oidc do
argument :user_info, :map, allow_nil?: false
argument :oauth_tokens, :map, allow_nil?: false
upsert? true
upsert_identity :id
change AshAuthentication.GenerateTokenChange
change fn changeset, _ctx ->
user_info = Ash.Changeset.get_argument(changeset, :user_info)
dbg(user_info)
changeset
|> Ash.Changeset.change_attribute(:name, user_info["name"])
|> Ash.Changeset.change_attribute(:id, user_info["sub"])
end
end
end
policies do
@ -49,6 +82,7 @@ defmodule Putzplan.Accounts.User do
end
attributes do
uuid_primary_key :id
attribute :id, :uuid, allow_nil?: false, primary_key?: true
attribute :name, :string, allow_nil?: false
end
end

View file

@ -0,0 +1,9 @@
defmodule Putzplan.Repo.Migrations.AddUserName do
use Ecto.Migration
def change do
alter table(:users, primary_key: false) do
add :name, :string, null: false
end
end
end