-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: block access to admin pages when 2fa is disabled (#667)
* feat: block access to admin pages when 2fa is disabled * wip: fix tests
- Loading branch information
Showing
8 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule ApiWeb.Plugs.Require2Factor do | ||
@moduledoc """ | ||
Plug enforcing a user to have 2fa enabled | ||
""" | ||
|
||
import Phoenix.Controller | ||
|
||
def init(opts), do: opts | ||
|
||
def call(conn, _opts) do | ||
conn | ||
|> fetch_user() | ||
|> authenticate(conn) | ||
end | ||
|
||
defp fetch_user(conn) do | ||
conn.assigns[:user] | ||
end | ||
|
||
defp authenticate(%ApiAccounts.User{totp_enabled: true}, conn), do: conn | ||
|
||
defp authenticate(_, conn) do | ||
conn | ||
|> put_flash( | ||
:error, | ||
"Account does not have 2-Factor Authentication enabled. Please enable before performing administrative tasks." | ||
) | ||
|> redirect(to: ApiWeb.Router.Helpers.user_path(conn, :configure_2fa)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,11 @@ defmodule ApiWeb.Admin.Accounts.KeyControllerTest do | |
|
||
on_exit(fn -> ApiAccounts.Dynamo.delete_all_tables() end) | ||
|
||
{:ok, user} = ApiAccounts.create_user(%{email: "[email protected]", role: "administrator"}) | ||
{:ok, user} = | ||
ApiAccounts.create_user(%{email: "[email protected]", role: "administrator", totp_enabled: true}) | ||
|
||
{:ok, user} = ApiAccounts.generate_totp_secret(user) | ||
ApiAccounts.enable_totp(user, NimbleTOTP.verification_code(user.totp_secret_bin)) | ||
|
||
conn = | ||
conn | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,8 +49,13 @@ defmodule ApiWeb.Admin.Accounts.UserControllerTest do | |
|
||
on_exit(fn -> ApiAccounts.Dynamo.delete_all_tables() end) | ||
|
||
params = %{email: "[email protected]", role: "administrator"} | ||
{:ok, user} = ApiAccounts.create_user(params) | ||
params = %{email: "[email protected]", role: "administrator", totp_enabled: true} | ||
|
||
{:ok, user} = | ||
ApiAccounts.create_user(%{email: "[email protected]", role: "administrator", totp_enabled: true}) | ||
|
||
{:ok, user} = ApiAccounts.generate_totp_secret(user) | ||
ApiAccounts.enable_totp(user, NimbleTOTP.verification_code(user.totp_secret_bin)) | ||
|
||
conn = | ||
conn | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ defmodule ApiWeb.Admin.SessionControllerTest do | |
@authorized_user_attrs %{ | ||
email: "[email protected]", | ||
role: "administrator", | ||
totp_enabled: true, | ||
password: @test_password | ||
} | ||
@unauthorized_user_attrs %{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule ApiWeb.Plugs.Require2FactorTest do | ||
use ApiWeb.ConnCase, async: true | ||
|
||
setup %{conn: conn} do | ||
conn = | ||
conn | ||
|> conn_with_session() | ||
|> bypass_through(ApiWeb.Router, [:browser, :admin]) | ||
|
||
{:ok, conn: conn} | ||
end | ||
|
||
test "opts" do | ||
assert ApiWeb.Plugs.Require2Factor.init([]) == [] | ||
end | ||
|
||
describe ":require_2factor plug" do | ||
test "gives 404 with no authenicated user", %{conn: conn} do | ||
conn = get(conn, "/") | ||
assert conn.status == 404 | ||
assert html_response(conn, 404) =~ "not found" | ||
end | ||
|
||
test "gives 404 for user without administrator role", %{conn: conn} do | ||
conn = | ||
conn | ||
|> user_with_role(nil, true) | ||
|> get("/") | ||
|
||
assert html_response(conn, 404) =~ "not found" | ||
end | ||
|
||
test "redirects on missing 2fa, but valid admin account", %{conn: conn} do | ||
conn = | ||
conn | ||
|> user_with_role("administrator", false) | ||
|> get("/") | ||
|
||
assert html_response(conn, 302) | ||
end | ||
|
||
test "allows user with administrator role and 2fa to proceed", %{conn: conn} do | ||
conn = | ||
conn | ||
|> user_with_role("administrator", true) | ||
|> get("/") | ||
|
||
refute conn.status | ||
end | ||
end | ||
|
||
defp user_with_role(conn, role, totp_enabled) do | ||
Plug.Conn.assign(conn, :user, %ApiAccounts.User{role: role, totp_enabled: totp_enabled}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters