]> Entropealabs - wampex_client.git/commitdiff
initial support for authentication
authorChristopher <chris@entropealabs.com>
Thu, 20 Feb 2020 05:02:32 +0000 (23:02 -0600)
committerChristopher <chris@entropealabs.com>
Thu, 20 Feb 2020 05:02:32 +0000 (23:02 -0600)
lib/wampex/authentication.ex [new file with mode: 0644]
lib/wampex/roles/peer.ex
lib/wampex/session.ex
test/wampex_test.exs

diff --git a/lib/wampex/authentication.ex b/lib/wampex/authentication.ex
new file mode 100644 (file)
index 0000000..1325179
--- /dev/null
@@ -0,0 +1,9 @@
+defmodule Wampex.Authentication do
+  @moduledoc false
+  defstruct [:authid, :authmethods]
+
+  @type t :: %__MODULE__{
+          authid: binary(),
+          authmethods: [binary()]
+        }
+end
index c37dce7edf3587d16acbf8ab7e41099923646c53..43662cc8d099acac572d84fdde3f2392bd4d3607 100644 (file)
@@ -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()
index cb08a7df4c2aca75dd8ee6641af6872fd81eda7a..82cbc0308179eb2bd59abe1546ec668f7339f9c5 100644 (file)
@@ -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}
index 59354ca151cbb62706d25c6abbd6718b53642541..d0ea1b5236252fa8505a0a82e09843cfb8b13698 100644 (file)
@@ -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)