Skip to content
This repository was archived by the owner on Oct 2, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions lib/vaultex/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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})}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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
9 changes: 6 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -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"}}