aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/auth/pleroma_authenticator.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/web/auth/pleroma_authenticator.ex')
-rw-r--r--lib/pleroma/web/auth/pleroma_authenticator.ex66
1 files changed, 64 insertions, 2 deletions
diff --git a/lib/pleroma/web/auth/pleroma_authenticator.ex b/lib/pleroma/web/auth/pleroma_authenticator.ex
index 94a19ad49..c826adb4c 100644
--- a/lib/pleroma/web/auth/pleroma_authenticator.ex
+++ b/lib/pleroma/web/auth/pleroma_authenticator.ex
@@ -4,13 +4,15 @@
defmodule Pleroma.Web.Auth.PleromaAuthenticator do
alias Comeonin.Pbkdf2
+ alias Pleroma.Registration
+ alias Pleroma.Repo
alias Pleroma.User
@behaviour Pleroma.Web.Auth.Authenticator
- def get_user(%Plug.Conn{} = conn) do
+ def get_user(%Plug.Conn{} = _conn, params) do
{name, password} =
- case conn.params do
+ case params do
%{"authorization" => %{"name" => name, "password" => password}} ->
{name, password}
@@ -27,9 +29,69 @@ defmodule Pleroma.Web.Auth.PleromaAuthenticator do
end
end
+ def get_registration(
+ %Plug.Conn{assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}},
+ _params
+ ) do
+ registration = Registration.get_by_provider_uid(provider, uid)
+
+ if registration do
+ {:ok, registration}
+ else
+ info = auth.info
+
+ Registration.changeset(%Registration{}, %{
+ provider: to_string(provider),
+ uid: to_string(uid),
+ info: %{
+ "nickname" => info.nickname,
+ "email" => info.email,
+ "name" => info.name,
+ "description" => info.description
+ }
+ })
+ |> Repo.insert()
+ end
+ end
+
+ def get_registration(%Plug.Conn{} = _conn, _params), do: {:error, :missing_credentials}
+
+ def create_from_registration(_conn, params, registration) do
+ nickname = value([params["nickname"], Registration.nickname(registration)])
+ email = value([params["email"], Registration.email(registration)])
+ name = value([params["name"], Registration.name(registration)]) || nickname
+ bio = value([params["bio"], Registration.description(registration)])
+
+ random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
+
+ with {:ok, new_user} <-
+ User.register_changeset(
+ %User{},
+ %{
+ email: email,
+ nickname: nickname,
+ name: name,
+ bio: bio,
+ password: random_password,
+ password_confirmation: random_password
+ },
+ external: true,
+ confirmed: true
+ )
+ |> Repo.insert(),
+ {:ok, _} <-
+ Registration.changeset(registration, %{user_id: new_user.id}) |> Repo.update() do
+ {:ok, new_user}
+ end
+ end
+
+ defp value(list), do: Enum.find(list, &(to_string(&1) != ""))
+
def handle_error(%Plug.Conn{} = _conn, error) do
error
end
def auth_template, do: nil
+
+ def oauth_consumer_template, do: nil
end