Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.6KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.MFA.TOTP do
  5. @moduledoc """
  6. This module represents functions to create secrets for
  7. TOTP Application as well as validate them with a time based token.
  8. """
  9. alias Pleroma.Config
  10. @config_ns [:instance, :multi_factor_authentication, :totp]
  11. @doc """
  12. https://github.com/google/google-authenticator/wiki/Key-Uri-Format
  13. """
  14. def provisioning_uri(secret, label, opts \\ []) do
  15. query =
  16. %{
  17. secret: secret,
  18. issuer: Keyword.get(opts, :issuer, default_issuer()),
  19. digits: Keyword.get(opts, :digits, default_digits()),
  20. period: Keyword.get(opts, :period, default_period())
  21. }
  22. |> Enum.filter(fn {_, v} -> not is_nil(v) end)
  23. |> Enum.into(%{})
  24. |> URI.encode_query()
  25. %URI{scheme: "otpauth", host: "totp", path: "/" <> label, query: query}
  26. |> URI.to_string()
  27. end
  28. defp default_period, do: Config.get(@config_ns ++ [:period])
  29. defp default_digits, do: Config.get(@config_ns ++ [:digits])
  30. defp default_issuer,
  31. do: Config.get(@config_ns ++ [:issuer], Config.get([:instance, :host]))
  32. @doc "Creates a random Base 32 encoded string"
  33. def generate_secret do
  34. Base.encode32(:crypto.strong_rand_bytes(10))
  35. end
  36. @doc "Generates a valid token based on a secret"
  37. def generate_token(secret) do
  38. :pot.totp(secret)
  39. end
  40. @doc """
  41. Validates a given token based on a secret.
  42. optional parameters:
  43. `token_length` default `6`
  44. `interval_length` default `30`
  45. `window` default 0
  46. Returns {:ok, :pass} if the token is valid and
  47. {:error, :invalid_token} if it is not.
  48. """
  49. @spec validate_token(String.t(), String.t()) ::
  50. {:ok, :pass} | {:error, :invalid_token | :invalid_secret_and_token}
  51. def validate_token(secret, token)
  52. when is_binary(secret) and is_binary(token) do
  53. opts = [
  54. token_length: default_digits(),
  55. interval_length: default_period()
  56. ]
  57. validate_token(secret, token, opts)
  58. end
  59. def validate_token(_, _), do: {:error, :invalid_secret_and_token}
  60. @doc "See `validate_token/2`"
  61. @spec validate_token(String.t(), String.t(), Keyword.t()) ::
  62. {:ok, :pass} | {:error, :invalid_token | :invalid_secret_and_token}
  63. def validate_token(secret, token, options)
  64. when is_binary(secret) and is_binary(token) do
  65. case :pot.valid_totp(token, secret, options) do
  66. true -> {:ok, :pass}
  67. false -> {:error, :invalid_token}
  68. end
  69. end
  70. def validate_token(_, _, _), do: {:error, :invalid_secret_and_token}
  71. end