From: Christopher Date: Thu, 20 Feb 2020 05:02:32 +0000 (-0600) Subject: initial support for authentication X-Git-Url: http://git.entropealabs.com/?a=commitdiff_plain;h=df4331c0dcc18814ee40450397d545841ad05522;p=wampex.git initial support for authentication --- diff --git a/lib/wampex/authentication.ex b/lib/wampex/authentication.ex new file mode 100644 index 0000000..1325179 --- /dev/null +++ b/lib/wampex/authentication.ex @@ -0,0 +1,9 @@ +defmodule Wampex.Authentication do + @moduledoc false + defstruct [:authid, :authmethods] + + @type t :: %__MODULE__{ + authid: binary(), + authmethods: [binary()] + } +end diff --git a/lib/wampex/roles/peer.ex b/lib/wampex/roles/peer.ex index c37dce7..43662cc 100644 --- a/lib/wampex/roles/peer.ex +++ b/lib/wampex/roles/peer.ex @@ -17,12 +17,13 @@ defmodule Wampex.Role.Peer do defmodule Hello do @moduledoc false @enforce_keys [:realm, :roles] - defstruct [:realm, :roles, agent: "WAMPex"] + defstruct [:realm, :roles, options: %{}, agent: "WAMPex"] @type t :: %__MODULE__{ realm: binary(), roles: nonempty_list(module()), - agent: binary() + agent: binary(), + options: map() } end @@ -41,8 +42,11 @@ defmodule Wampex.Role.Peer do def add(roles), do: roles @spec hello(Hello.t()) :: Wampex.message() - def hello(%Hello{realm: r, roles: roles, agent: agent}) do - [@hello, r, %{agent: agent, roles: Enum.reduce(roles, %{}, fn r, acc -> r.add(acc) end)}] + def hello(%Hello{realm: r, roles: roles, agent: agent, options: opts}) do + options = + Map.merge(opts, %{agent: agent, roles: Enum.reduce(roles, %{}, fn r, acc -> r.add(acc) end)}) + + [@hello, r, options] end @spec goodbye(Goodbye.t()) :: Wampex.message() diff --git a/lib/wampex/session.ex b/lib/wampex/session.ex index cb08a7d..82cbc03 100644 --- a/lib/wampex/session.ex +++ b/lib/wampex/session.ex @@ -8,6 +8,7 @@ defmodule Wampex.Session do alias StatesLanguage, as: SL alias Wampex.Realm + alias Wampex.Authentication alias Wampex.Role.Peer alias Wampex.Role.Peer.Hello alias Wampex.Serializer.MessagePack @@ -16,6 +17,8 @@ defmodule Wampex.Session do @yield 70 + @enforce_keys [:url, :roles] + defstruct [ :id, :url, @@ -28,6 +31,7 @@ defmodule Wampex.Session do :realm, :name, :roles, + :authentication, request_id: 0, protocol: "wamp.2.msgpack", transport: WebSocket, @@ -46,6 +50,7 @@ defmodule Wampex.Session do realm: Realm.t(), name: module() | nil, roles: [module()], + authentication: Authentication.t(), request_id: integer(), protocol: binary(), transport: module(), @@ -105,10 +110,17 @@ defmodule Wampex.Session do _, "Init", %SL{ - data: %Sess{transport: tt, transport_pid: t, roles: roles, realm: %Realm{name: realm}} + data: %Sess{ + transport: tt, + transport_pid: t, + roles: roles, + realm: %Realm{name: realm}, + authentication: auth + } } = data ) do - tt.send_request(t, Peer.hello(%Hello{realm: realm, roles: roles})) + message = maybe_authentication(%Hello{realm: realm, roles: roles}, auth) + tt.send_request(t, Peer.hello(message)) {:ok, data, [{:next_event, :internal, :hello_sent}]} end @@ -234,6 +246,12 @@ defmodule Wampex.Session do request_id end + defp maybe_authentication(message, nil), do: message + + defp maybe_authentication(%Hello{} = message, %Authentication{} = auth) do + %Hello{message | options: Map.from_struct(auth)} + end + defp remove_nil_values(message), do: Enum.reject(message, &is_nil/1) defp maybe_inject_request_id(r_id, [@yield | _] = message), do: {r_id, message} diff --git a/test/wampex_test.exs b/test/wampex_test.exs index 59354ca..d0ea1b5 100644 --- a/test/wampex_test.exs +++ b/test/wampex_test.exs @@ -2,7 +2,7 @@ defmodule WampexTest do use ExUnit.Case, async: true doctest Wampex - alias Wampex.{Realm, Session} + alias Wampex.{Authentication, Realm, Session} alias Wampex.Role.{Callee, Caller, Peer, Publisher, Subscriber} alias Callee.{Register, Unregister, Yield} alias Caller.Call @@ -15,8 +15,9 @@ defmodule WampexTest do @realm %Realm{name: "com.myrealm"} @roles [Callee, Caller, Publisher, Subscriber] @device "as987d9a8sd79a87ds" + @auth %Authentication{authid: "entone", authmethods: ["wampcra"]} - @session %Session{url: @url, realm: @realm, roles: @roles} + @session %Session{url: @url, realm: @realm, roles: @roles, authentication: @auth} test "session_name" do assert Test.Session = Wampex.session_name(Test)