2019-11-17 19:18:21 +01:00
|
|
|
defmodule PhilomenaWeb.TotpPlug do
|
2019-11-13 04:12:46 +01:00
|
|
|
@moduledoc """
|
|
|
|
This plug ensures that a user session has a valid TOTP.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
plug PhilomenaWeb.TotpPlug
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias PhilomenaWeb.Router.Helpers, as: Routes
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
@spec init(any()) :: any()
|
|
|
|
def init(opts), do: opts
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
@spec call(Plug.Conn.t(), any()) :: Plug.Conn.t()
|
|
|
|
def call(conn, _opts) do
|
|
|
|
conn
|
|
|
|
|> Pow.Plug.current_user()
|
|
|
|
|> case do
|
2020-01-11 05:20:19 +01:00
|
|
|
nil -> conn
|
2019-11-13 04:12:46 +01:00
|
|
|
user -> maybe_require_totp_phase(user, conn)
|
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-13 04:12:46 +01:00
|
|
|
defp maybe_require_totp_phase(%{otp_required_for_login: nil}, conn), do: conn
|
|
|
|
defp maybe_require_totp_phase(%{otp_required_for_login: false}, conn), do: conn
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-13 04:12:46 +01:00
|
|
|
defp maybe_require_totp_phase(_user, conn) do
|
|
|
|
conn.private
|
|
|
|
|> Map.get(:pow_session_metadata, [])
|
|
|
|
|> Keyword.get(:valid_totp_at)
|
|
|
|
|> case do
|
|
|
|
nil ->
|
|
|
|
conn
|
|
|
|
|> Phoenix.Controller.redirect(to: Routes.session_totp_path(conn, :new))
|
|
|
|
|> Plug.Conn.halt()
|
|
|
|
|
|
|
|
_valid_at ->
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
@spec update_valid_totp_at_for_session(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
|
|
|
def update_valid_totp_at_for_session(conn, user) do
|
|
|
|
metadata =
|
|
|
|
conn.private
|
|
|
|
|> Map.get(:pow_session_metadata, [])
|
|
|
|
|> Keyword.put(:valid_totp_at, DateTime.utc_now())
|
|
|
|
|
|
|
|
config = Pow.Plug.fetch_config(conn)
|
2020-01-11 05:20:19 +01:00
|
|
|
plug = Pow.Plug.get_plug(config)
|
|
|
|
conn = Plug.Conn.put_private(conn, :pow_session_metadata, metadata)
|
2019-11-13 04:12:46 +01:00
|
|
|
|
2019-11-20 06:34:45 +01:00
|
|
|
conn =
|
|
|
|
conn
|
2020-01-11 05:20:19 +01:00
|
|
|
|> Plug.Conn.put_private(:pow_persistent_session_metadata,
|
|
|
|
session_metadata: Keyword.take(metadata, [:valid_totp_at])
|
|
|
|
)
|
2019-11-20 06:34:45 +01:00
|
|
|
|> PowPersistentSession.Plug.Cookie.create(user, config)
|
|
|
|
|
2019-11-13 04:12:46 +01:00
|
|
|
plug.do_create(conn, user, config)
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|