--- /dev/null
+defmodule Wampex.Authentication do
+ @moduledoc false
+ defstruct [:authid, :authmethods]
+
+ @type t :: %__MODULE__{
+ authid: binary(),
+ authmethods: [binary()]
+ }
+end
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
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()
alias StatesLanguage, as: SL
alias Wampex.Realm
+ alias Wampex.Authentication
alias Wampex.Role.Peer
alias Wampex.Role.Peer.Hello
alias Wampex.Serializer.MessagePack
@yield 70
+ @enforce_keys [:url, :roles]
+
defstruct [
:id,
:url,
:realm,
:name,
:roles,
+ :authentication,
request_id: 0,
protocol: "wamp.2.msgpack",
transport: WebSocket,
realm: Realm.t(),
name: module() | nil,
roles: [module()],
+ authentication: Authentication.t(),
request_id: integer(),
protocol: binary(),
transport: module(),
_,
"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
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}
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
@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)