diff --git a/lib/gnat/handshake.ex b/lib/gnat/handshake.ex index a6e7b1f..9c49e5e 100644 --- a/lib/gnat/handshake.ex +++ b/lib/gnat/handshake.ex @@ -15,8 +15,10 @@ defmodule Gnat.Handshake do end def negotiate_settings(server_settings, user_settings) do + auth_required = server_settings[:auth_required] || user_settings[:auth_required] || false + %{verbose: false} - |> negotiate_auth(server_settings, user_settings) + |> negotiate_auth(server_settings, user_settings, auth_required) |> negotiate_headers(server_settings, user_settings) end @@ -40,26 +42,26 @@ defmodule Gnat.Handshake do :gen_tcp.send(socket, "CONNECT " <> Jason.encode!(settings, maps: :strict) <> "\r\n") end - defp negotiate_auth(settings, %{auth_required: true}=_server, %{username: username, password: password}=_user) do + defp negotiate_auth(settings, _server, %{username: username, password: password}=_user, true = _auth_required) do Map.merge(settings, %{user: username, pass: password}) end - defp negotiate_auth(settings, %{auth_required: true}=_server, %{token: token}=_user) do + defp negotiate_auth(settings, _server, %{token: token}=_user, true = _auth_required) do Map.merge(settings, %{auth_token: token}) end - defp negotiate_auth(settings, %{auth_required: true, nonce: nonce}=_server, %{nkey_seed: seed, jwt: jwt}=_user) do + defp negotiate_auth(settings, %{nonce: nonce}=_server, %{nkey_seed: seed, jwt: jwt}=_user, true = _auth_required) do {:ok, nkey} = NKEYS.from_seed(seed) signature = NKEYS.sign(nkey, nonce) |> Base.url_encode64() |> String.replace("=", "") Map.merge(settings, %{sig: signature, protocol: 1, jwt: jwt}) end - defp negotiate_auth(settings, %{auth_required: true, nonce: nonce}=_server, %{nkey_seed: seed}=_user) do + defp negotiate_auth(settings, %{nonce: nonce}=_server, %{nkey_seed: seed}=_user, true = _auth_required) do {:ok, nkey} = NKEYS.from_seed(seed) signature = NKEYS.sign(nkey, nonce) |> Base.url_encode64() |> String.replace("=", "") public = NKEYS.public_nkey(nkey) Map.merge(settings, %{sig: signature, protocol: 1, nkey: public}) end - defp negotiate_auth(settings, _server, _user) do + defp negotiate_auth(settings, _server, _user, _auth_required) do settings end