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