2019-02-26 13:26:54 +00: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 11:12:41 +00:00
|
|
|
defmodule Pleroma.Web.Auth.PleromaAuthenticator do
|
2019-02-21 15:55:19 +00:00
|
|
|
alias Comeonin.Pbkdf2
|
2019-03-05 02:52:23 +00:00
|
|
|
alias Pleroma.User
|
2019-02-21 15:55:19 +00:00
|
|
|
|
2019-02-28 11:12:41 +00:00
|
|
|
@behaviour Pleroma.Web.Auth.Authenticator
|
2019-02-21 15:55:19 +00:00
|
|
|
|
2019-03-18 07:26:41 +00:00
|
|
|
def get_user(%Plug.Conn{} = _conn, params) do
|
2019-03-03 19:20:36 +00:00
|
|
|
{name, password} =
|
2019-03-18 07:26:41 +00:00
|
|
|
case params do
|
2019-03-03 19:20:36 +00:00
|
|
|
%{"authorization" => %{"name" => name, "password" => password}} ->
|
|
|
|
{name, password}
|
|
|
|
|
|
|
|
%{"grant_type" => "password", "username" => name, "password" => password} ->
|
|
|
|
{name, password}
|
|
|
|
end
|
2019-02-21 15:55:19 +00: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-15 14:08:03 +00:00
|
|
|
def get_or_create_user_by_oauth(
|
|
|
|
%Plug.Conn{assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}},
|
|
|
|
_params
|
|
|
|
) do
|
|
|
|
user = User.get_by_auth_provider_uid(provider, uid)
|
|
|
|
|
|
|
|
if user do
|
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
info = auth.info
|
|
|
|
email = info.email
|
|
|
|
nickname = info.nickname
|
|
|
|
|
|
|
|
# TODO: FIXME: connect to existing (non-oauth) account (need a UI flow for that) / generate a random nickname?
|
|
|
|
email =
|
|
|
|
if email && User.get_by_email(email) do
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
email
|
|
|
|
end
|
|
|
|
|
|
|
|
nickname =
|
|
|
|
if nickname && User.get_by_nickname(nickname) do
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
nickname
|
|
|
|
end
|
|
|
|
|
|
|
|
new_user =
|
|
|
|
User.oauth_register_changeset(
|
|
|
|
%User{},
|
|
|
|
%{
|
|
|
|
auth_provider: to_string(provider),
|
|
|
|
auth_provider_uid: to_string(uid),
|
|
|
|
name: info.name,
|
|
|
|
bio: info.description,
|
|
|
|
email: email,
|
|
|
|
nickname: nickname
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
Pleroma.Repo.insert(new_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_or_create_user_by_oauth(%Plug.Conn{} = _conn, _params),
|
|
|
|
do: {:error, :missing_credentials}
|
|
|
|
|
2019-02-21 15:55:19 +00:00
|
|
|
def handle_error(%Plug.Conn{} = _conn, error) do
|
|
|
|
error
|
|
|
|
end
|
2019-02-28 10:58:58 +00:00
|
|
|
|
|
|
|
def auth_template, do: nil
|
2019-02-21 15:55:19 +00:00
|
|
|
end
|