From 2de3f62dd33f85685ec7be72266f8ae2e46c5d3c Mon Sep 17 00:00:00 2001 From: Christopher Date: Sat, 21 Mar 2020 10:26:02 -0500 Subject: [PATCH] user Peer instead of User --- lib/router/admin.ex | 8 ++++---- lib/router/authentication.ex | 6 +++--- lib/router/authentication/ensure_default_admin.ex | 8 ++++---- lib/router/authentication/{user.ex => peer.ex} | 12 +++++++----- lib/router/realms.ex | 1 - .../repo/migrations/20200319210508_create_realms.exs | 7 ++++--- test/wampex_test.exs | 4 ++-- 7 files changed, 24 insertions(+), 22 deletions(-) rename lib/router/authentication/{user.ex => peer.ex} (87%) diff --git a/lib/router/admin.ex b/lib/router/admin.ex index f20906e..77e9c2c 100644 --- a/lib/router/admin.ex +++ b/lib/router/admin.ex @@ -4,11 +4,11 @@ defmodule Wampex.Router.Admin do require Logger alias Wampex.Roles.Dealer.{Invocation, Result} alias Wampex.Roles.Peer.Error - alias Wampex.Router.Authentication.{User, Realm} + alias Wampex.Router.Authentication.{Peer, Realm} alias Wampex.Router.{Realms, Session} @procedures [ - "admin.create_user", + "admin.create_peer", "admin.create_realm" ] @@ -39,12 +39,12 @@ defmodule Wampex.Router.Admin do {req_id, %Invocation{ arg_kw: %{"realm" => realm, "authid" => authid, "password" => password}, - options: %{"procedure" => "admin.create_user"} + options: %{"procedure" => "admin.create_peer"} } = event, {pid, node}}, %{proxy: proxy} = state ) do with realm <- Realm.get(uri: realm), - %User{id: id} <- User.create(authid: authid, password: password, realm: realm) do + %Peer{id: id} <- Peer.create(authid: authid, password: password, realm: realm) do Logger.info("Admin handled event: #{inspect(event)}") send({proxy, node}, {%Result{request_id: req_id, arg_list: [id]}, pid}) diff --git a/lib/router/authentication.ex b/lib/router/authentication.ex index 40a59e9..87bc349 100644 --- a/lib/router/authentication.ex +++ b/lib/router/authentication.ex @@ -5,7 +5,7 @@ defmodule Wampex.Router.Authentication do alias Wampex.Crypto alias Wampex.Serializers.JSON - alias Wampex.Router.Authentication.{Realm, User} + alias Wampex.Router.Authentication.{Realm, Peer} @wampcra "wampcra" @auth_provider "userdb" @@ -19,7 +19,7 @@ defmodule Wampex.Router.Authentication do def challenge(realm, authid, session_id) do %Realm{} = realm = Realm.get(uri: realm) - %User{} = user = User.get(authid: authid, realm: realm) + %Peer{} = user = Peer.get(authid: authid, realm: realm) now = DateTime.to_iso8601(DateTime.utc_now()) %{ @@ -56,7 +56,7 @@ defmodule Wampex.Router.Authentication do defp get_secret(authid, uri) do realm = Realm.get(uri: uri) - %User{password: password} = User.get(authid: authid, realm: realm) + %Peer{password: password} = Peer.get(authid: authid, realm: realm) password end diff --git a/lib/router/authentication/ensure_default_admin.ex b/lib/router/authentication/ensure_default_admin.ex index 9052191..6a72096 100644 --- a/lib/router/authentication/ensure_default_admin.ex +++ b/lib/router/authentication/ensure_default_admin.ex @@ -3,7 +3,7 @@ defmodule Wampex.Router.Authentication.EnsureDefaultAdmin do require Logger use GenServer - alias Wampex.Router.Authentication.{Realm, User} + alias Wampex.Router.Authentication.{Realm, Peer} def start_link(uri: uri, authid: authid, password: password) do GenServer.start_link(__MODULE__, {uri, authid, password}) @@ -11,9 +11,9 @@ defmodule Wampex.Router.Authentication.EnsureDefaultAdmin do def init({uri, authid, password}) do %Realm{} = realm = Realm.create(uri: uri) - %User{} = user = User.create(authid: authid, password: password, realm: realm) - Logger.info("Realm: #{inspect(realm)}") - Logger.info("User: #{inspect(user)}") + %Peer{} = user = Peer.create(authid: authid, password: password, realm: realm) + Logger.debug("Realm: #{inspect(realm)}") + Logger.debug("Peer: #{inspect(user)}") :ignore end end diff --git a/lib/router/authentication/user.ex b/lib/router/authentication/peer.ex similarity index 87% rename from lib/router/authentication/user.ex rename to lib/router/authentication/peer.ex index 8d3e8a2..ad6ed5f 100644 --- a/lib/router/authentication/user.ex +++ b/lib/router/authentication/peer.ex @@ -1,12 +1,13 @@ -defmodule Wampex.Router.Authentication.User do +defmodule Wampex.Router.Authentication.Peer do @moduledoc """ - CREATE TABLE authentication.users ( + CREATE TABLE authentication.peers ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), authid STRING(255) NOT NULL, password STRING NOT NULL, salt STRING NOT NULL, iterations INT NOT NULL, keylen INT NOT NULL, + cidr STRING, realm_id UUID NOT NULL REFERENCES authentication.realms (id) ON DELETE CASCADE, inserted_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, @@ -23,12 +24,13 @@ defmodule Wampex.Router.Authentication.User do @primary_key {:id, :binary_id, autogenerate: false, read_after_writes: true} @foreign_key_type :binary_id - schema "users" do + schema "peers" do field(:authid, :string) field(:password, :string) field(:salt, :string) field(:iterations, :integer) field(:keylen, :integer) + field(:cidr, :string) belongs_to(:realm, Realm) timestamps() end @@ -38,7 +40,7 @@ defmodule Wampex.Router.Authentication.User do end def get(authid: authid, realm: realm) do - Repo.get_by(User, authid: authid, realm_id: realm.id) + Repo.get_by(Peer, authid: authid, realm_id: realm.id) end def create(authid: authid, password: password, realm: realm) do @@ -50,7 +52,7 @@ defmodule Wampex.Router.Authentication.User do password = Crypto.pbkdf2(password, salt, iterations, keylen) {:ok, u} = - %User{ + %Peer{ authid: authid, password: password, realm: realm, diff --git a/lib/router/realms.ex b/lib/router/realms.ex index 06625c6..1348d76 100644 --- a/lib/router/realms.ex +++ b/lib/router/realms.ex @@ -14,7 +14,6 @@ defmodule Wampex.Router.Realms do {DynamicSupervisor, strategy: :one_for_one, name: realm_supervisor_name(name)} ] - Logger.info("Starting Realms Supervisor: #{inspect(children)}") Supervisor.init(children, strategy: :one_for_one) end diff --git a/priv/repo/migrations/20200319210508_create_realms.exs b/priv/repo/migrations/20200319210508_create_realms.exs index 86f165b..d694b1f 100644 --- a/priv/repo/migrations/20200319210508_create_realms.exs +++ b/priv/repo/migrations/20200319210508_create_realms.exs @@ -11,18 +11,19 @@ defmodule Wampex.Router.Authentication.Repo.Migrations.CreateRealms do create(unique_index(:realms, [:uri])) - create table("users", primary_key: false) do + create table("peers", primary_key: false) do add(:id, :binary_id, primary_key: true, default: fragment("gen_random_uuid()")) add(:authid, :string, null: false) add(:password, :string, null: false) add(:salt, :string, null: false) add(:iterations, :integer, null: false) add(:keylen, :integer, null: false) + add(:cidr, :string) add(:realm_id, references(:realms, type: :binary_id, on_delete: :delete_all), null: false) timestamps() end - create(index(:users, [:authid])) - create(unique_index(:users, [:authid, :realm_id])) + create(index(:peers, [:authid])) + create(unique_index(:peers, [:authid, :realm_id])) end end diff --git a/test/wampex_test.exs b/test/wampex_test.exs index c496773..c7782ab 100644 --- a/test/wampex_test.exs +++ b/test/wampex_test.exs @@ -335,7 +335,7 @@ defmodule WampexTest do Client.send_request( caller_name, Caller.call(%Call{ - procedure: "admin.create_user", + procedure: "admin.create_peer", arg_kw: %{authid: "chris", password: "woot!", realm: @realm_uri} }) ) @@ -352,7 +352,7 @@ defmodule WampexTest do Client.send_request( caller_name, Caller.call(%Call{ - procedure: "admin.create_user", + procedure: "admin.create_peer", arg_kw: %{authid: "chris", password: "woot!", realm: "not.real"} }) ) -- 2.45.3