logo

pleroma

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

pleroma_authenticator.ex (3412B)


  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.Web.Auth.PleromaAuthenticator do
  5. alias Pleroma.Registration
  6. alias Pleroma.Repo
  7. alias Pleroma.User
  8. alias Pleroma.Web.Plugs.AuthenticationPlug
  9. import Pleroma.Web.Auth.Helpers, only: [fetch_credentials: 1, fetch_user: 1]
  10. @behaviour Pleroma.Web.Auth.Authenticator
  11. def get_user(%Plug.Conn{} = conn) do
  12. with {:ok, {name, password}} <- fetch_credentials(conn),
  13. {_, %User{} = user} <- {:user, fetch_user(name)},
  14. {_, true} <- {:checkpw, AuthenticationPlug.checkpw(password, user.password_hash)},
  15. {:ok, user} <- AuthenticationPlug.maybe_update_password(user, password) do
  16. {:ok, user}
  17. else
  18. {:error, _reason} = error -> error
  19. error -> {:error, error}
  20. end
  21. end
  22. @doc """
  23. Gets or creates Pleroma.Registration record from Ueberauth assigns.
  24. Note: some strategies (like `keycloak`) might need extra configuration to fill `uid` from callback response —
  25. see [`docs/config.md`](docs/config.md).
  26. """
  27. def get_registration(%Plug.Conn{assigns: %{ueberauth_auth: %{uid: nil}}}),
  28. do: {:error, :missing_uid}
  29. def get_registration(%Plug.Conn{
  30. assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}
  31. }) do
  32. registration = Registration.get_by_provider_uid(provider, uid)
  33. if registration do
  34. {:ok, registration}
  35. else
  36. info = auth.info
  37. %Registration{}
  38. |> Registration.changeset(%{
  39. provider: to_string(provider),
  40. uid: to_string(uid),
  41. info: %{
  42. "nickname" => info.nickname,
  43. "email" => info.email,
  44. "name" => info.name,
  45. "description" => info.description
  46. }
  47. })
  48. |> Repo.insert()
  49. end
  50. end
  51. def get_registration(%Plug.Conn{} = _conn), do: {:error, :missing_credentials}
  52. @doc "Creates Pleroma.User record basing on params and Pleroma.Registration record."
  53. def create_from_registration(
  54. %Plug.Conn{params: %{"authorization" => registration_attrs}},
  55. %Registration{} = registration
  56. ) do
  57. nickname = value([registration_attrs["nickname"], Registration.nickname(registration)])
  58. email = value([registration_attrs["email"], Registration.email(registration)])
  59. name = value([registration_attrs["name"], Registration.name(registration)]) || nickname
  60. bio = value([registration_attrs["bio"], Registration.description(registration)]) || ""
  61. random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
  62. with {:ok, new_user} <-
  63. User.register_changeset(
  64. %User{},
  65. %{
  66. email: email,
  67. nickname: nickname,
  68. name: name,
  69. bio: bio,
  70. password: random_password,
  71. password_confirmation: random_password
  72. },
  73. external: true,
  74. confirmed: true
  75. )
  76. |> Repo.insert(),
  77. {:ok, _} <-
  78. Registration.changeset(registration, %{user_id: new_user.id}) |> Repo.update() do
  79. {:ok, new_user}
  80. end
  81. end
  82. defp value(list), do: Enum.find(list, &(to_string(&1) != ""))
  83. def handle_error(%Plug.Conn{} = _conn, error) do
  84. error
  85. end
  86. def auth_template, do: nil
  87. def oauth_consumer_template, do: nil
  88. end