From 5b2a832a42eece9cd13c43899298551ee4906878 Mon Sep 17 00:00:00 2001 From: Michael Schaefermeyer Date: Mon, 1 Aug 2016 13:21:31 +0200 Subject: Add auth --- lib/boltex/bolt.ex | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lib/boltex/bolt.ex') diff --git a/lib/boltex/bolt.ex b/lib/boltex/bolt.ex index 2e7f6b5..8bbf3e7 100644 --- a/lib/boltex/bolt.ex +++ b/lib/boltex/bolt.ex @@ -48,8 +48,20 @@ defmodule Boltex.Bolt do @doc """ Initialises the connection. + + Expects a transport module (i.e. `gen_tcp`) and a `Port`. Accepts + authorisation params in the form of {username, password}. + + ## Examples + + iex> Boltex.Bolt.init :gen_tcp, port + :ok + + iex> Boltex.Bolt.init :gen_tcp, port, {"username", "password"} + :ok """ - def init(transport, port, params \\ %{}) do + def init(transport, port, auth \\ nil) do + params = auth_params auth send_messages transport, port, [{[@user_agent, params], @sig_init}] case receive_data(transport, port) do @@ -62,6 +74,15 @@ defmodule Boltex.Bolt do end end + defp auth_params(nil), do: %{} + defp auth_params({username, password}) do + %{ + scheme: "basic", + principal: username, + credentials: password + } + end + @doc """ Sends a list of messages using the Bolt protocol and PackStream encoding. -- cgit v1.2.1