2019-11-13 04:12:46 +01:00
|
|
|
defmodule PhilomenaWeb.Registration.TotpController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Users.User
|
|
|
|
alias Philomena.Repo
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case user.encrypted_otp_secret do
|
|
|
|
nil ->
|
|
|
|
user
|
|
|
|
|> User.create_totp_secret_changeset()
|
|
|
|
|> Repo.update()
|
|
|
|
|
|
|
|
# Redirect to have Pow pick up the changes
|
|
|
|
redirect(conn, to: Routes.registration_totp_path(conn, :edit))
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
changeset = Pow.Plug.change_user(conn)
|
2019-11-13 05:49:37 +01:00
|
|
|
secret = User.totp_secret(user)
|
|
|
|
qrcode = User.totp_qrcode(user)
|
|
|
|
render(conn, "edit.html", changeset: changeset, totp_secret: secret, totp_qrcode: qrcode)
|
2019-11-13 04:12:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, params) do
|
|
|
|
backup_codes = User.random_backup_codes()
|
2019-11-13 05:49:37 +01:00
|
|
|
user = Pow.Plug.current_user(conn)
|
2019-11-13 04:12:46 +01:00
|
|
|
|
2019-11-13 05:49:37 +01:00
|
|
|
user
|
2019-11-13 04:12:46 +01:00
|
|
|
|> User.totp_changeset(params, backup_codes)
|
|
|
|
|> Repo.update()
|
|
|
|
|> case do
|
|
|
|
{:error, changeset} ->
|
2019-11-13 05:49:37 +01:00
|
|
|
secret = User.totp_secret(user)
|
|
|
|
qrcode = User.totp_qrcode(user)
|
|
|
|
render(conn, "edit.html", changeset: changeset, totp_secret: secret, totp_qrcode: qrcode)
|
2019-11-13 04:12:46 +01:00
|
|
|
|
|
|
|
{:ok, user} ->
|
|
|
|
conn
|
2019-11-17 19:18:21 +01:00
|
|
|
|> PhilomenaWeb.TotpPlug.update_valid_totp_at_for_session(user)
|
2019-11-13 04:12:46 +01:00
|
|
|
|> put_flash(:totp_backup_codes, backup_codes)
|
|
|
|
|> redirect(to: Routes.registration_totp_path(conn, :edit))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|