philomena/lib/philomena_web/controllers/session/totp_controller.ex

33 lines
851 B
Elixir
Raw Permalink Normal View History

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
alias PhilomenaWeb.UserAuth
2019-11-13 04:12:46 +01:00
alias Philomena.Users.User
alias Philomena.Users
2019-11-13 04:12:46 +01:00
alias Philomena.Repo
def new(conn, _params) do
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
%{"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
|> 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