Skip to content
Permalink
Browse files

Add basic_auth

  • Loading branch information...
picandocodigo committed Dec 11, 2017
1 parent c8de5d3 commit af94967767ce0c97610eaa0c36ac47e882e9183f
@@ -47,3 +47,9 @@ config :logger, :console, format: "[$level] $message\n"
# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

config :phoenix_app, authentication: [
username: "user",
password: "password",
realm: "Development Realm"
]
@@ -18,7 +18,12 @@ config :phoenix_app, PhoenixAppWeb.Endpoint,
url: [host: "example.com", port: 80],
cache_static_manifest: "priv/static/cache_manifest.json"

# Do not print debug messages in production
config :phoenix_app, authentication: [
username: {system: ,"BASIC_AUTH_USERNAME"},
password: {system: ,"BASIC_AUTH_PASSWORD"},
realm: {system: ,"BASIC_AUTH_REALM"}
]

config :logger, level: :info

# ## SSL Support
@@ -61,4 +66,5 @@ config :logger, level: :info

# Finally import the config/prod.secret.exs
# which should be versioned separately.

import_config "prod.secret.exs"
@@ -8,3 +8,9 @@ config :phoenix_app, PhoenixAppWeb.Endpoint,

# Print only warnings and errors during test
config :logger, level: :warn

config :phoenix_app, authentication: [
username: "user",
password: "password",
realm: "Development Realm"
]
@@ -9,12 +9,16 @@ defmodule PhoenixAppWeb.Router do
plug :put_secure_browser_headers
end

pipeline :authentication do
plug BasicAuth, use_config: {:phoenix_app, :authentication}
end

pipeline :api do
plug :accepts, ["json"]
end

scope "/", PhoenixAppWeb do
pipe_through :browser # Use the default browser stack
pipe_through [:browser, :authentication]

get "/", PageController, :index
end
@@ -41,7 +41,8 @@ defmodule PhoenixApp.Mixfile do
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"}
{:cowboy, "~> 1.0"},
{:basic_auth, "~> 2.2"}
]
end
end
@@ -1,8 +1,25 @@
defmodule PhoenixAppWeb.PageControllerTest do
use PhoenixAppWeb.ConnCase

test "GET /", %{conn: conn} do
conn = get conn, "/"
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
describe "GET /" do
test "without valid credentials", %{conn: conn} do
conn = get conn, "/"
assert conn.status == 401
end

test "with valid credentials" do
conn = get conn_with_basic_auth(), "/"
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
end
end


def conn_with_basic_auth do
username = Application.get_env(:phoenix_app, :authentication)[:username]
password = Application.get_env(:phoenix_app, :authentication)[:password]
basic_auth_header = "Basic " <> Base.encode64("#{username}:#{password}")

build_conn()
|> put_req_header("authorization", basic_auth_header)
end
end
@@ -1,4 +1,5 @@
%{"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
%{"basic_auth": {:hex, :basic_auth, "2.2.1", "32e9383f4d3fbbecbe11690d73fabbd2e81fe0cf7d209f2bf8983c8e2b9f11bd", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 0.14 or ~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [], [], "hexpm"},
"file_system": {:hex, :file_system, "0.2.2", "7f1e9de4746f4eb8a4ca8f2fbab582d84a4e40fa394cce7bfcb068b988625b06", [], [], "hexpm"},
"gettext": {:hex, :gettext, "0.14.0", "1a019a2e51d5ad3d126efe166dcdf6563768e5d06c32a99ad2281a1fa94b4c72", [], [], "hexpm"},

0 comments on commit af94967

Please sign in to comment.
You can’t perform that action at this time.