Skip to content
Merged
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
28 changes: 28 additions & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"default_stop_words": [
"defmodule",
"defrecord",
"defimpl",
"defexception",
"defprotocol",
"defstruct",
"def.+(.+\\\\.+).+do",
"^\\s+use\\s+"
],
"custom_stop_words": [],
"coverage_options": {
"treat_no_relevant_lines_as_covered": false,
"output_dir": "cover/",
"minimum_coverage": 0,
"floor_coverage": true
},
"terminal_options": {
"file_column_width": 40
},
"skip_files": [
"lib/ex_ftp.ex",
"lib/ex_ftp/util/doc.ex",
"lib/ex_ftp/authenticator.ex",
"lib/ex_ftp/storage_connector.ex"
]
}
25 changes: 8 additions & 17 deletions lib/ex_ftp/auth/basic_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,8 @@ defmodule ExFTP.Auth.BasicAuth do
@impl Authenticator
@spec authenticated?(authenticator_state :: Authenticator.authenticator_state()) :: boolean()
def authenticated?(authenticator_state) do
with_result =
with {:ok, config} <- validate_config(BasicAuthConfig) do
check_authentication(config, authenticator_state)
end

case with_result do
{:ok, _} -> true
_ -> false
with {:ok, config} <- validate_config(BasicAuthConfig) do
check_authentication(config, authenticator_state)
end
end

Expand All @@ -178,27 +172,24 @@ defmodule ExFTP.Auth.BasicAuth do
end
end

defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = authenticator_state) do
{:ok, authenticator_state}
end
defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = _authenticator_state),
do: true

defp check_authentication(
%{authenticated_url: url, authenticated_method: http_method} = _config,
%{username: username, password: password} = authenticator_state
%{username: username, password: password} = _authenticator_state
)
when not is_nil(url) do
[url: url, method: http_method, redirect: true, auth: {:basic, "#{username}:#{password}"}]
|> Req.request()
|> case do
{:ok, %{status: 200}} ->
{:ok, authenticator_state}
true

_ ->
{:error, "Did not get a 200 response"}
false
end
end

defp check_authentication(_config, _authenticator_state) do
{:error, "Not Authenticated"}
end
defp check_authentication(_config, _authenticator_state), do: false
end
25 changes: 8 additions & 17 deletions lib/ex_ftp/auth/bearer_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,8 @@ defmodule ExFTP.Auth.BearerAuth do
@impl Authenticator
@spec authenticated?(authenticator_state :: Authenticator.authenticator_state()) :: boolean()
def authenticated?(authenticator_state) do
with_result =
with {:ok, config} <- validate_config(BearerAuthConfig) do
check_authentication(config, authenticator_state)
end

case with_result do
{:ok, _} -> true
_ -> false
with {:ok, config} <- validate_config(BearerAuthConfig) do
check_authentication(config, authenticator_state)
end
end

Expand All @@ -163,13 +157,12 @@ defmodule ExFTP.Auth.BearerAuth do
end
end

defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = authenticator_state) do
{:ok, authenticator_state}
end
defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = _authenticator_state),
do: true

defp check_authentication(
%{authenticated_url: url, authenticated_method: http_method} = _config,
%{bearer_token: bearer_token} = authenticator_state
%{bearer_token: bearer_token} = _authenticator_state
)
when not is_nil(url) and not is_nil(bearer_token) do
headers = [{"authorization", "Bearer #{bearer_token}"}]
Expand All @@ -178,14 +171,12 @@ defmodule ExFTP.Auth.BearerAuth do
|> Req.request()
|> case do
{:ok, %{status: 200}} ->
{:ok, authenticator_state}
true

_ ->
{:error, "Did not get a 200 response"}
false
end
end

defp check_authentication(_config, _authenticator_state) do
{:error, "Not Authenticated"}
end
defp check_authentication(_config, _authenticator_state), do: false
end
25 changes: 8 additions & 17 deletions lib/ex_ftp/auth/digest_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,8 @@ defmodule ExFTP.Auth.DigestAuth do
@impl Authenticator
@spec authenticated?(authenticator_state :: Authenticator.authenticator_state()) :: boolean()
def authenticated?(authenticator_state) do
with_result =
with {:ok, config} <- validate_config(DigestAuthConfig) do
check_authentication(config, authenticator_state)
end

case with_result do
{:ok, _} -> true
_ -> false
with {:ok, config} <- validate_config(DigestAuthConfig) do
check_authentication(config, authenticator_state)
end
end

Expand All @@ -176,27 +170,24 @@ defmodule ExFTP.Auth.DigestAuth do
end
end

defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = authenticator_state) do
{:ok, authenticator_state}
end
defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = _authenticator_state),
do: true

defp check_authentication(
%{authenticated_url: url, authenticated_method: http_method} = _config,
%{username: username, password: password} = authenticator_state
%{username: username, password: password} = _authenticator_state
)
when not is_nil(url) do
url
|> ExFTP.DigestAuthUtil.request(http_method, username, password)
|> case do
{:ok, %{status: 200}} ->
{:ok, authenticator_state}
true

_ ->
{:error, "Did not get a 200 response"}
false
end
end

defp check_authentication(_config, _authenticator_state) do
{:error, "Not Authenticated"}
end
defp check_authentication(_config, _authenticator_state), do: false
end
23 changes: 7 additions & 16 deletions lib/ex_ftp/auth/webhook_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,8 @@ defmodule ExFTP.Auth.WebhookAuth do
"""
@impl Authenticator
def authenticated?(authenticator_state) do
with_result =
with {:ok, config} <- validate_config(WebhookAuthConfig) do
check_authentication(config, authenticator_state)
end

case with_result do
{:ok, _} -> true
_ -> false
with {:ok, config} <- validate_config(WebhookAuthConfig) do
check_authentication(config, authenticator_state)
end
end

Expand All @@ -166,13 +160,10 @@ defmodule ExFTP.Auth.WebhookAuth do
end
end

defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = authenticator_state) do
{:ok, authenticator_state}
end
defp check_authentication(%{authenticated_url: nil} = _config, %{authenticated: true} = _authenticator_state),
do: true

defp check_authentication(%{authenticated_url: nil} = _config, _authenticator_state) do
{:error, "Not Authenticated"}
end
defp check_authentication(%{authenticated_url: nil} = _config, _authenticator_state), do: false

defp check_authentication(%{authenticated_url: url, authenticated_method: http_method} = _config, authenticator_state) do
params =
Expand All @@ -182,10 +173,10 @@ defmodule ExFTP.Auth.WebhookAuth do
|> Req.request()
|> case do
{:ok, %{status: 200}} ->
{:ok, authenticator_state}
true

_ ->
{:error, "Did not get a 200 response"}
false
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ex_ftp/storage/common.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ defmodule ExFTP.Storage.Common do
new_d = change_prefix(wd, path)

if connector.directory_exists?(new_d, connector_state) do
:ok =
send_resp(@directory_action_not_taken, "\"#{new_d}\" directory already exists", socket)
send_resp(@directory_action_not_taken, "\"#{new_d}\" directory already exists", socket)
connector_state
else
new_d
|> connector.make_directory(connector_state)
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_ftp/storage/file_connector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,13 @@ defmodule ExFTP.Storage.FileConnector do
end
end

defp rmrf_dir("/"), do: {:ok, nil}
defp rmrf_dir("/"), do: {:error, "Not something to delete"}

defp rmrf_dir(dir) do
if File.exists?(dir) && File.dir?(dir) do
File.rm_rf(dir)
else
{:ok, nil}
{:error, "Not something to delete"}
end
end
end
7 changes: 7 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ defmodule ExFTP.MixProject do
start_permanent: Mix.env() == :prod,
deps: deps(),
test_coverage: [tool: ExCoveralls],
preferred_cli_env: [
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.html": :test,
"coveralls.cobertura": :test
],
# Hex
package: package(),
description: """
Expand Down
17 changes: 17 additions & 0 deletions test/ex_ftp/auth/basic_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ defmodule ExFTP.Auth.BasicAuthTest do
end

describe "authenticated/1" do
test "no inputs" do
Application.put_env(:ex_ftp, :authenticator, BasicAuth)

Application.put_env(:ex_ftp, :authenticator_config, %{
login_url: "",
login_method: :get,
authenticated_url: "",
authenticated_method: :get
})

refute BasicAuth.authenticated?(%{})

Application.put_env(:ex_ftp, :authenticator_config, %{})

refute BasicAuth.authenticated?(%{})
end

test "with custom authenticated route" do
username = Faker.Internet.slug()
password = Faker.Internet.slug()
Expand Down
17 changes: 17 additions & 0 deletions test/ex_ftp/auth/bearer_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ defmodule ExFTP.Auth.BearerAuthTest do
end

describe "authenticated/1" do
test "no inputs" do
Application.put_env(:ex_ftp, :authenticator, BearerAuth)

Application.put_env(:ex_ftp, :authenticator_config, %{
login_url: "",
login_method: :get,
authenticated_url: "",
authenticated_method: :get
})

refute BearerAuth.authenticated?(%{})

Application.put_env(:ex_ftp, :authenticator_config, %{})

refute BearerAuth.authenticated?(%{})
end

test "with custom authenticated route" do
Application.put_env(:ex_ftp, :authenticator, BearerAuth)

Expand Down
17 changes: 17 additions & 0 deletions test/ex_ftp/auth/digest_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ defmodule ExFTP.Auth.DigestAuthTest do
end

describe "authenticated/1" do
test "no inputs" do
Application.put_env(:ex_ftp, :authenticator, DigestAuth)

Application.put_env(:ex_ftp, :authenticator_config, %{
login_url: "",
login_method: :get,
authenticated_url: "",
authenticated_method: :get
})

refute DigestAuth.authenticated?(%{})

Application.put_env(:ex_ftp, :authenticator_config, %{})

refute DigestAuth.authenticated?(%{})
end

test "with custom authenticated route" do
username = Faker.Internet.slug()
password = Faker.Internet.slug()
Expand Down
15 changes: 15 additions & 0 deletions test/ex_ftp/auth/webhook_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ defmodule ExFTP.Auth.WebhookAuthTest do
end

describe "authenticated/1" do
test "no inputs" do
Application.put_env(:ex_ftp, :authenticator, WebhookAuth)

Application.put_env(:ex_ftp, :authenticator_config, %{
login_url: "",
login_method: :get
})

refute WebhookAuth.authenticated?(%{})

Application.put_env(:ex_ftp, :authenticator_config, %{})

refute WebhookAuth.authenticated?(%{})
end

test "with custom authenticated route" do
Application.put_env(:ex_ftp, :authenticator, WebhookAuth)

Expand Down
12 changes: 10 additions & 2 deletions test/ex_ftp/storage/file_connector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ defmodule ExFTP.Storage.FileConnectorTest do
test_cwd_cdup(state, tmp_dir)
end

test "MKD / RMD", state do
test "MKD / RMD", %{socket: socket} = state do
tmp_dir = Path.join(System.tmp_dir!(), Faker.Internet.slug())
on_exit(fn -> File.rm_rf(tmp_dir) end)
test_mkd_rmd(state, tmp_dir)

# try to make a directory that you shouldnt be able to
dir_to_make = "/cant_make"
send_and_expect(socket, "MKD", [dir_to_make], 521, "Failed to make directory.")

# try to rm directory that you shouldnt be able to
dir_to_remove = "/dev/tty"
send_and_expect(socket, "RMD", [dir_to_remove], 550, "Failed to remove directory.")
end

test "LIST -a", state do
Expand Down Expand Up @@ -114,7 +122,7 @@ defmodule ExFTP.Storage.FileConnectorTest do
refute Enum.empty?(files_to_find)

Enum.each(files_to_find, fn file_to_find ->
assert [_found] = Enum.filter(parts, fn part -> String.starts_with?(part, file_to_find) end)
assert [_found | _] = Enum.filter(parts, fn part -> String.starts_with?(part, file_to_find) end)
end)
end

Expand Down
Loading
Loading