2019-02-26 06:26:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-28 04:12:41 -07:00
|
|
|
defmodule Pleroma.Web.Auth.PleromaAuthenticator do
|
2019-02-21 08:55:19 -07:00
|
|
|
alias Comeonin.Pbkdf2
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.User
|
2019-03-18 08:23:38 -06:00
|
|
|
alias Pleroma.Registration
|
|
|
|
alias Pleroma.Repo
|
2019-02-21 08:55:19 -07:00
|
|
|
|
2019-02-28 04:12:41 -07:00
|
|
|
@behaviour Pleroma.Web.Auth.Authenticator
|
2019-02-21 08:55:19 -07:00
|
|
|
|
2019-03-18 01:26:41 -06:00
|
|
|
def get_user(%Plug.Conn{} = _conn, params) do
|
2019-03-03 12:20:36 -07:00
|
|
|
{name, password} =
|
2019-03-18 01:26:41 -06:00
|
|
|
case params do
|
2019-03-03 12:20:36 -07:00
|
|
|
%{"authorization" => %{"name" => name, "password" => password}} ->
|
|
|
|
{name, password}
|
|
|
|
|
|
|
|
%{"grant_type" => "password", "username" => name, "password" => password} ->
|
|
|
|
{name, password}
|
|
|
|
end
|
2019-02-21 08:55:19 -07:00
|
|
|
|
|
|
|
with {_, %User{} = user} <- {:user, User.get_by_nickname_or_email(name)},
|
|
|
|
{_, true} <- {:checkpw, Pbkdf2.checkpw(password, user.password_hash)} do
|
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
error ->
|
|
|
|
{:error, error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
def get_by_external_registration(
|
2019-03-15 08:08:03 -06:00
|
|
|
%Plug.Conn{assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}},
|
|
|
|
_params
|
|
|
|
) do
|
2019-03-18 08:23:38 -06:00
|
|
|
registration = Registration.get_by_provider_uid(provider, uid)
|
2019-03-15 08:08:03 -06:00
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
if registration do
|
|
|
|
user = Repo.preload(registration, :user).user
|
2019-03-15 08:08:03 -06:00
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
info = auth.info
|
|
|
|
email = info.email
|
|
|
|
nickname = info.nickname
|
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
# Note: nullifying email in case this email is already taken
|
2019-03-15 08:08:03 -06:00
|
|
|
email =
|
|
|
|
if email && User.get_by_email(email) do
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
email
|
|
|
|
end
|
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
# Note: generating a random numeric suffix to nickname in case this nickname is already taken
|
2019-03-15 08:08:03 -06:00
|
|
|
nickname =
|
|
|
|
if nickname && User.get_by_nickname(nickname) do
|
2019-03-18 08:23:38 -06:00
|
|
|
"#{nickname}_#{:os.system_time()}"
|
2019-03-15 08:08:03 -06:00
|
|
|
else
|
|
|
|
nickname
|
|
|
|
end
|
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
with {:ok, new_user} <-
|
|
|
|
User.external_registration_changeset(
|
|
|
|
%User{},
|
|
|
|
%{
|
|
|
|
name: info.name,
|
|
|
|
bio: info.description,
|
|
|
|
email: email,
|
|
|
|
nickname: nickname
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|> Repo.insert(),
|
|
|
|
{:ok, _} <-
|
|
|
|
Registration.changeset(%Registration{}, %{
|
|
|
|
user_id: new_user.id,
|
|
|
|
provider: to_string(provider),
|
|
|
|
uid: to_string(uid),
|
|
|
|
info: %{nickname: info.nickname, email: info.email}
|
|
|
|
})
|
|
|
|
|> Repo.insert() do
|
|
|
|
{:ok, new_user}
|
|
|
|
end
|
2019-03-15 08:08:03 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
def get_by_external_registration(%Plug.Conn{} = _conn, _params),
|
2019-03-15 08:08:03 -06:00
|
|
|
do: {:error, :missing_credentials}
|
|
|
|
|
2019-02-21 08:55:19 -07:00
|
|
|
def handle_error(%Plug.Conn{} = _conn, error) do
|
|
|
|
error
|
|
|
|
end
|
2019-02-28 03:58:58 -07:00
|
|
|
|
|
|
|
def auth_template, do: nil
|
2019-02-21 08:55:19 -07:00
|
|
|
end
|