This commit is contained in:
parent
a8d9fe6121
commit
9b4f3b140c
19 changed files with 330 additions and 43 deletions
|
|
@ -54,7 +54,7 @@ defmodule MvWeb.Plugs.CheckPagePermission do
|
|||
conn
|
||||
|> fetch_session()
|
||||
|> fetch_flash()
|
||||
|> put_flash(:error, "You don't have permission to access this page.")
|
||||
|> maybe_put_access_denied_flash(user)
|
||||
|> redirect(to: redirect_to)
|
||||
|> halt()
|
||||
end
|
||||
|
|
@ -75,6 +75,13 @@ defmodule MvWeb.Plugs.CheckPagePermission do
|
|||
|
||||
defp redirect_target(user), do: redirect_target_for_user(user)
|
||||
|
||||
# Only set "no permission" flash when user is logged in; unauthenticated users get redirect only, no flash.
|
||||
defp maybe_put_access_denied_flash(conn, nil), do: conn
|
||||
|
||||
defp maybe_put_access_denied_flash(conn, _user) do
|
||||
put_flash(conn, :error, "You don't have permission to access this page.")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns true if the path is public (no auth/permission check).
|
||||
Used by LiveView hook to skip redirect on sign-in etc.
|
||||
|
|
|
|||
61
lib/mv_web/plugs/oidc_only_sign_in_redirect.ex
Normal file
61
lib/mv_web/plugs/oidc_only_sign_in_redirect.ex
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
defmodule MvWeb.Plugs.OidcOnlySignInRedirect do
|
||||
@moduledoc """
|
||||
When OIDC-only mode is active:
|
||||
- GET /sign-in redirects to the OIDC flow when OIDC is configured (sign-in page skipped).
|
||||
- GET /auth/user/password/sign_in_with_token is rejected (redirect to /sign-in with error)
|
||||
so password sign-in cannot complete.
|
||||
"""
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
|
||||
alias Mv.Config
|
||||
|
||||
def init(opts), do: opts
|
||||
|
||||
def call(conn, _opts) do
|
||||
conn
|
||||
|> maybe_redirect_sign_in_to_oidc()
|
||||
|> maybe_reject_password_token_sign_in()
|
||||
end
|
||||
|
||||
defp maybe_redirect_sign_in_to_oidc(conn) do
|
||||
if conn.request_path == "/sign-in" and conn.method == "GET" do
|
||||
if Config.oidc_only?() and Config.oidc_configured?() do
|
||||
conn
|
||||
|> redirect(to: "/auth/user/oidc")
|
||||
|> halt()
|
||||
else
|
||||
conn
|
||||
end
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_reject_password_token_sign_in(conn) do
|
||||
if conn.halted, do: conn, else: reject_password_token_sign_in_if_applicable(conn)
|
||||
end
|
||||
|
||||
defp reject_password_token_sign_in_if_applicable(conn) do
|
||||
path = conn.request_path
|
||||
|
||||
password_token_path? =
|
||||
path =~ ~r|/auth/user/password/sign_in_with_token| and conn.method == "GET"
|
||||
|
||||
if password_token_path? and Config.oidc_only?() do
|
||||
message =
|
||||
Gettext.dgettext(
|
||||
MvWeb.Gettext,
|
||||
"default",
|
||||
"Only sign-in via Single Sign-On (SSO) is allowed."
|
||||
)
|
||||
|
||||
conn
|
||||
|> put_flash(:error, message)
|
||||
|> redirect(to: "/sign-in")
|
||||
|> halt()
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue