logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

backup_codes.ex (990B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.MFA.BackupCodes do
  5. @moduledoc """
  6. This module contains functions for generating backup codes.
  7. """
  8. alias Pleroma.Config
  9. @config_ns [:instance, :multi_factor_authentication, :backup_codes]
  10. @doc """
  11. Generates backup codes.
  12. """
  13. @spec generate(Keyword.t()) :: list(String.t())
  14. def generate(opts \\ []) do
  15. number_of_codes = Keyword.get(opts, :number_of_codes, default_backup_codes_number())
  16. code_length = Keyword.get(opts, :length, default_backup_codes_code_length())
  17. Enum.map(1..number_of_codes, fn _ ->
  18. :crypto.strong_rand_bytes(div(code_length, 2))
  19. |> Base.encode16(case: :lower)
  20. end)
  21. end
  22. defp default_backup_codes_number, do: Config.get(@config_ns ++ [:number], 5)
  23. defp default_backup_codes_code_length,
  24. do: Config.get(@config_ns ++ [:length], 16)
  25. end