1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.MFA.Changeset do
7 alias Pleroma.MFA.Settings
10 def disable(%Ecto.Changeset{} = changeset, force \\ false) do
13 |> Ecto.Changeset.apply_changes()
14 |> MFA.fetch_settings()
16 if force || not MFA.enabled?(settings) do
17 put_change(changeset, %Settings{settings | enabled: false})
23 def disable_totp(%User{multi_factor_authentication_settings: settings} = user) do
25 |> put_change(%Settings{settings | totp: %Settings.TOTP{}})
28 def confirm_totp(%User{multi_factor_authentication_settings: settings} = user) do
29 totp_settings = %Settings.TOTP{settings.totp | confirmed: true}
32 |> put_change(%Settings{settings | totp: totp_settings, enabled: true})
35 def setup_totp(%User{} = user, attrs) do
36 mfa_settings = MFA.fetch_settings(user)
40 |> Ecto.Changeset.cast(attrs, [:secret, :delivery_type])
43 |> put_change(%Settings{mfa_settings | totp: Ecto.Changeset.apply_changes(totp_settings)})
46 def cast_backup_codes(%User{} = user, codes) do
48 |> put_change(%Settings{
49 user.multi_factor_authentication_settings
54 defp put_change(%User{} = user, settings) do
56 |> Ecto.Changeset.change()
57 |> put_change(settings)
60 defp put_change(%Ecto.Changeset{} = changeset, settings) do
62 |> Ecto.Changeset.put_change(:multi_factor_authentication_settings, settings)