2019-11-13 04:12:46 +01:00
|
|
|
defmodule PhilomenaWeb.Session.TotpController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-20 22:29:29 +01:00
|
|
|
alias PhilomenaWeb.LayoutView
|
2020-07-28 22:56:26 +02:00
|
|
|
alias PhilomenaWeb.UserAuth
|
2019-11-13 04:12:46 +01:00
|
|
|
alias Philomena.Users.User
|
2020-07-28 22:56:26 +02:00
|
|
|
alias Philomena.Users
|
2019-11-13 04:12:46 +01:00
|
|
|
alias Philomena.Repo
|
|
|
|
|
|
|
|
def new(conn, _params) do
|
2020-07-28 22:56:26 +02:00
|
|
|
changeset = Users.change_user(conn.assigns.current_user)
|
2019-11-13 04:12:46 +01:00
|
|
|
|
2019-12-20 22:29:29 +01:00
|
|
|
render(conn, "new.html", layout: {LayoutView, "two_factor.html"}, changeset: changeset)
|
2019-11-13 04:12:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, params) do
|
2020-07-28 22:56:26 +02:00
|
|
|
%{"user" => user_params} = params
|
|
|
|
|
|
|
|
conn.assigns.current_user
|
2019-11-13 04:12:46 +01:00
|
|
|
|> User.consume_totp_token_changeset(params)
|
|
|
|
|> Repo.update()
|
|
|
|
|> case do
|
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
2020-07-28 22:56:26 +02:00
|
|
|
|> put_flash(:error, "Invalid TOTP token entered. Please sign in again.")
|
|
|
|
|> UserAuth.log_out_user()
|
2019-11-13 04:12:46 +01:00
|
|
|
|
|
|
|
{:ok, user} ->
|
2020-07-31 23:06:53 +02:00
|
|
|
UserAuth.totp_auth_user(conn, user, user_params)
|
2019-11-13 04:12:46 +01:00
|
|
|
end
|
|
|
|
end
|
2019-12-16 20:24:38 +01:00
|
|
|
end
|