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