[#923] OAuth: prototype of sign in / sign up with Twitter.
This commit is contained in:
parent
63ab61ed3f
commit
aacbf0f570
10 changed files with 209 additions and 37 deletions
|
|
@ -8,9 +8,9 @@ defmodule Pleroma.Web.Auth.PleromaAuthenticator do
|
|||
|
||||
@behaviour Pleroma.Web.Auth.Authenticator
|
||||
|
||||
def get_user(%Plug.Conn{} = conn) do
|
||||
%{"authorization" => %{"name" => name, "password" => password}} = conn.params
|
||||
|
||||
def get_user(%Plug.Conn{} = _conn, %{
|
||||
"authorization" => %{"name" => name, "password" => password}
|
||||
}) do
|
||||
with {_, %User{} = user} <- {:user, User.get_by_nickname_or_email(name)},
|
||||
{_, true} <- {:checkpw, Pbkdf2.checkpw(password, user.password_hash)} do
|
||||
{:ok, user}
|
||||
|
|
@ -20,6 +20,56 @@ defmodule Pleroma.Web.Auth.PleromaAuthenticator do
|
|||
end
|
||||
end
|
||||
|
||||
def get_user(%Plug.Conn{} = _conn, _params), do: {:error, :missing_credentials}
|
||||
|
||||
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}
|
||||
|
||||
def handle_error(%Plug.Conn{} = _conn, error) do
|
||||
error
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue