Merge branch 'default-mrf' into 'develop'
[akkoma] / lib / pleroma / web / auth / pleroma_authenticator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Auth.PleromaAuthenticator do
6 alias Pleroma.Registration
7 alias Pleroma.Repo
8 alias Pleroma.User
9 alias Pleroma.Web.Plugs.AuthenticationPlug
10
11 import Pleroma.Web.Auth.Authenticator,
12 only: [fetch_credentials: 1, fetch_user: 1]
13
14 @behaviour Pleroma.Web.Auth.Authenticator
15
16 def get_user(%Plug.Conn{} = conn) do
17 with {:ok, {name, password}} <- fetch_credentials(conn),
18 {_, %User{} = user} <- {:user, fetch_user(name)},
19 {_, true} <- {:checkpw, AuthenticationPlug.checkpw(password, user.password_hash)},
20 {:ok, user} <- AuthenticationPlug.maybe_update_password(user, password) do
21 {:ok, user}
22 else
23 {:error, _reason} = error -> error
24 error -> {:error, error}
25 end
26 end
27
28 @doc """
29 Gets or creates Pleroma.Registration record from Ueberauth assigns.
30 Note: some strategies (like `keycloak`) might need extra configuration to fill `uid` from callback response —
31 see [`docs/config.md`](docs/config.md).
32 """
33 def get_registration(%Plug.Conn{assigns: %{ueberauth_auth: %{uid: nil}}}),
34 do: {:error, :missing_uid}
35
36 def get_registration(%Plug.Conn{
37 assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}
38 }) do
39 registration = Registration.get_by_provider_uid(provider, uid)
40
41 if registration do
42 {:ok, registration}
43 else
44 info = auth.info
45
46 %Registration{}
47 |> Registration.changeset(%{
48 provider: to_string(provider),
49 uid: to_string(uid),
50 info: %{
51 "nickname" => info.nickname,
52 "email" => info.email,
53 "name" => info.name,
54 "description" => info.description
55 }
56 })
57 |> Repo.insert()
58 end
59 end
60
61 def get_registration(%Plug.Conn{} = _conn), do: {:error, :missing_credentials}
62
63 @doc "Creates Pleroma.User record basing on params and Pleroma.Registration record."
64 def create_from_registration(
65 %Plug.Conn{params: %{"authorization" => registration_attrs}},
66 %Registration{} = registration
67 ) do
68 nickname = value([registration_attrs["nickname"], Registration.nickname(registration)])
69 email = value([registration_attrs["email"], Registration.email(registration)])
70 name = value([registration_attrs["name"], Registration.name(registration)]) || nickname
71 bio = value([registration_attrs["bio"], Registration.description(registration)]) || ""
72
73 random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
74
75 with {:ok, new_user} <-
76 User.register_changeset(
77 %User{},
78 %{
79 email: email,
80 nickname: nickname,
81 name: name,
82 bio: bio,
83 password: random_password,
84 password_confirmation: random_password
85 },
86 external: true,
87 need_confirmation: false
88 )
89 |> Repo.insert(),
90 {:ok, _} <-
91 Registration.changeset(registration, %{user_id: new_user.id}) |> Repo.update() do
92 {:ok, new_user}
93 end
94 end
95
96 defp value(list), do: Enum.find(list, &(to_string(&1) != ""))
97
98 def handle_error(%Plug.Conn{} = _conn, error) do
99 error
100 end
101
102 def auth_template, do: nil
103
104 def oauth_consumer_template, do: nil
105 end