diff --git a/lib/vaultex/client.ex b/lib/vaultex/client.ex index 14630ba..c1ecce0 100644 --- a/lib/vaultex/client.ex +++ b/lib/vaultex/client.ex @@ -38,7 +38,14 @@ defmodule Vaultex.Client do def init(state) do url = "#{get_env(:scheme)}://#{get_env(:host)}:#{get_env(:port)}/#{@version}/" - {:ok, Map.merge(state, %{url: url})} + {:ok, Map.merge(state, %{url: url})} + end + + def handle_call({:auth, {:github, github_token}}, _from, state) do + {:ok, req} = request(:post, "#{state.url}auth/github/login", %{token: github_token}) + Logger.debug("Got auth response: #{inspect req}") + + {:reply, {:ok, :authenticated}, Map.merge(state, %{token: req["auth"]["client_token"]})} end # authenticate and save the access token in `token` @@ -48,8 +55,9 @@ defmodule Vaultex.Client do {:ok, req} = request(:post, "#{state.url}auth/app-id/login", %{app_id: app_id, user_id: user_id}) Logger.debug("Got auth reponse: #{inspect req}") - {:reply, {:ok, :authenticated}, Map.merge(state, %{token: req["auth"]["client_token"]})} + {:reply, {:ok, :authenticated}, Map.merge(state, %{token: req["auth"]["client_token"]})} end + def handle_call({:auth, {:token, token}}, _from, state) do Logger.debug("Merged in token auth") {:reply, {:ok, :authenticated}, Map.merge(state, %{token: token})} @@ -117,27 +125,24 @@ defmodule Vaultex.Client do end defp request(method, url, params, auth) do case get_content(method, url, params, auth) do - {:ok, code, _headers, body_ref} -> - {:ok, res} = :hackney.body body_ref + {:ok, %HTTPoison.Response{status_code: 200, body: res}} -> Logger.debug("[body] #{inspect res}") - case Poison.decode(res) do - {:ok, json} -> - cond do - 200 -> - {:ok, json} - 204 -> - {:ok, :no_data} - code in 400..599 -> - {:error, {{:http_status, code}, json}} - true -> - {:error, res} - end - {:error, json_err} -> + case Poison.decode(res) do + {:ok, json} -> + {:ok, json} + {:error, json_err} -> case res do "" -> {:ok, :no_data} - _ -> {:error, json_err} + _ -> {:error, json_err} end - end + end + {:ok, %HTTPoison.AsyncResponse{id: {:maybe_redirect, _status, headers, _client}}} -> + case Enum.find(headers, fn ({key, val}) -> key == "Location" end) do + nil -> + {:error, "Error redirecting"} + {_key, new_url} -> + request(method, new_url, params, auth) + end error -> error end end @@ -154,10 +159,12 @@ defmodule Vaultex.Client do case Poison.encode(params) do # empty params {:ok, "null"} -> - :hackney.request(method, url, headers) + HTTPoison.request(method, url, "", headers, [follow_redirect: true, hackney: [ssl_options: [versions: [:"tlsv1.2"]]]]) + {:ok, json} -> Logger.debug("[JSON] #{inspect json}") - :hackney.request(method, url, headers, json) + HTTPoison.request(method, url, json, headers, [follow_redirect: true, hackney: [ssl_options: [versions: [:"tlsv1.2"]]]]) + error -> error end end diff --git a/mix.exs b/mix.exs index 11f47c5..ba66085 100644 --- a/mix.exs +++ b/mix.exs @@ -3,7 +3,7 @@ defmodule Vaultex.Mixfile do def project do [app: :vaultex, - version: "0.0.1", + version: "0.0.3", elixir: "~> 1.1", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, @@ -14,7 +14,7 @@ defmodule Vaultex.Mixfile do # # Type "mix help compile.app" for more information def application do - [applications: [:logger, :hackney, :poison], + [applications: [:logger, :httpoison, :poison], mod: {Vaultex, []}] end @@ -28,8 +28,8 @@ defmodule Vaultex.Mixfile do # # Type "mix help deps" for more examples and options defp deps do - [{:hackney, "~> 1.3"}, - {:poison, "~> 1.5"} + [{:httpoison, "~> 0.8.1"}, + {:poison, "~> 1.5"} ] end end diff --git a/mix.lock b/mix.lock index 493134e..22bc9ef 100644 --- a/mix.lock +++ b/mix.lock @@ -1,4 +1,7 @@ -%{"hackney": {:hex, :hackney, "1.3.2"}, - "idna": {:hex, :idna, "1.0.2"}, - "poison": {:hex, :poison, "1.5.0"}, +%{"certifi": {:hex, :certifi, "0.3.0"}, + "hackney": {:hex, :hackney, "1.4.10"}, + "httpoison": {:hex, :httpoison, "0.8.1"}, + "idna": {:hex, :idna, "1.1.0"}, + "mimerl": {:hex, :mimerl, "1.0.2"}, + "poison": {:hex, :poison, "1.5.2"}, "ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5"}}