1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
6 import Pleroma.Web.Gettext
8 alias Pleroma.Emails.Mailer
9 alias Pleroma.Emails.UserEmail
12 alias Pleroma.UserInviteToken
14 def register_user(params, opts \\ []) do
17 |> Map.take([:email, :token, :password])
18 |> Map.put(:bio, params |> Map.get(:bio, "") |> User.parse_bio())
19 |> Map.put(:nickname, params[:username])
20 |> Map.put(:name, Map.get(params, :fullname, params[:username]))
21 |> Map.put(:password_confirmation, params[:password])
22 |> Map.put(:registration_reason, params[:reason])
24 if Pleroma.Config.get([:instance, :registrations_open]) do
25 create_user(params, opts)
27 create_user_with_invite(params, opts)
31 defp create_user_with_invite(params, opts) do
32 with %{token: token} when is_binary(token) <- params,
33 %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, %{token: token}),
34 true <- UserInviteToken.valid_invite?(invite) do
35 UserInviteToken.update_usage!(invite)
36 create_user(params, opts)
38 nil -> {:error, "Invalid token"}
39 _ -> {:error, "Expired token"}
43 defp create_user(params, opts) do
44 changeset = User.register_changeset(%User{}, params, opts)
46 case User.register(changeset) do
50 {:error, changeset} ->
53 |> Ecto.Changeset.traverse_errors(fn {msg, _opts} -> msg end)
60 def password_reset(nickname_or_email) do
61 with true <- is_binary(nickname_or_email),
62 %User{local: true, email: email, deactivated: false} = user when is_binary(email) <-
63 User.get_by_nickname_or_email(nickname_or_email),
64 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
66 |> UserEmail.password_reset_email(token_record.token)
67 |> Mailer.deliver_async()
76 def validate_captcha(app, params) do
77 if app.trusted || not Pleroma.Captcha.enabled?() do
80 do_validate_captcha(params)
84 defp do_validate_captcha(params) do
85 with :ok <- validate_captcha_presence(params),
87 Pleroma.Captcha.validate(
88 params[:captcha_token],
89 params[:captcha_solution],
90 params[:captcha_answer_data]
94 {:error, :captcha_error} ->
95 captcha_error(dgettext("errors", "CAPTCHA Error"))
98 captcha_error(dgettext("errors", "Invalid CAPTCHA"))
100 {:error, :kocaptcha_service_unavailable} ->
101 captcha_error(dgettext("errors", "Kocaptcha service unavailable"))
103 {:error, :expired} ->
104 captcha_error(dgettext("errors", "CAPTCHA expired"))
106 {:error, :already_used} ->
107 captcha_error(dgettext("errors", "CAPTCHA already used"))
109 {:error, :invalid_answer_data} ->
110 captcha_error(dgettext("errors", "Invalid answer data"))
117 defp validate_captcha_presence(params) do
118 [:captcha_solution, :captcha_token, :captcha_answer_data]
119 |> Enum.find_value(:ok, fn key ->
120 unless is_binary(params[key]) do
121 error = dgettext("errors", "Invalid CAPTCHA (Missing parameter: %{name})", name: key)
127 # For some reason FE expects error message to be a serialized JSON
128 defp captcha_error(error), do: {:error, Jason.encode!(%{captcha: [error]})}