2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-09-07 00:58:10 -06:00
|
|
|
defmodule Pleroma.Web.OAuth.App do
|
2017-09-06 11:06:25 -06:00
|
|
|
use Ecto.Schema
|
2019-02-09 08:20:18 -07:00
|
|
|
import Ecto.Changeset
|
2020-02-28 01:16:40 -07:00
|
|
|
import Ecto.Query
|
2019-09-06 12:50:00 -06:00
|
|
|
alias Pleroma.Repo
|
2021-08-25 21:01:04 -06:00
|
|
|
alias Pleroma.User
|
2017-09-06 11:06:25 -06:00
|
|
|
|
2019-05-06 11:51:03 -06:00
|
|
|
@type t :: %__MODULE__{}
|
2019-05-13 12:35:45 -06:00
|
|
|
|
2017-09-06 11:06:25 -06:00
|
|
|
schema "apps" do
|
2018-03-30 07:01:53 -06:00
|
|
|
field(:client_name, :string)
|
|
|
|
field(:redirect_uris, :string)
|
2019-02-13 14:29:29 -07:00
|
|
|
field(:scopes, {:array, :string}, default: [])
|
2018-03-30 07:01:53 -06:00
|
|
|
field(:website, :string)
|
|
|
|
field(:client_id, :string)
|
|
|
|
field(:client_secret, :string)
|
2020-02-28 01:16:40 -07:00
|
|
|
field(:trusted, :boolean, default: false)
|
|
|
|
|
2021-08-25 21:01:04 -06:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
|
|
|
|
2020-02-28 01:16:40 -07:00
|
|
|
has_many(:oauth_authorizations, Pleroma.Web.OAuth.Authorization, on_delete: :delete_all)
|
|
|
|
has_many(:oauth_tokens, Pleroma.Web.OAuth.Token, on_delete: :delete_all)
|
2017-09-06 11:06:25 -06:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2020-05-25 12:02:22 -06:00
|
|
|
@spec changeset(t(), map()) :: Ecto.Changeset.t()
|
2020-02-28 01:16:40 -07:00
|
|
|
def changeset(struct, params) do
|
|
|
|
cast(struct, params, [:client_name, :redirect_uris, :scopes, :website, :trusted])
|
|
|
|
end
|
|
|
|
|
2020-05-25 12:02:22 -06:00
|
|
|
@spec register_changeset(t(), map()) :: Ecto.Changeset.t()
|
2017-09-06 11:06:25 -06:00
|
|
|
def register_changeset(struct, params \\ %{}) do
|
2018-03-30 07:01:53 -06:00
|
|
|
changeset =
|
|
|
|
struct
|
2020-02-28 01:16:40 -07:00
|
|
|
|> changeset(params)
|
2018-03-30 07:01:53 -06:00
|
|
|
|> validate_required([:client_name, :redirect_uris, :scopes])
|
2017-09-06 11:06:25 -06:00
|
|
|
|
|
|
|
if changeset.valid? do
|
|
|
|
changeset
|
2019-02-13 18:05:25 -07:00
|
|
|
|> put_change(
|
|
|
|
:client_id,
|
|
|
|
:crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
|
|
|
|
)
|
|
|
|
|> put_change(
|
|
|
|
:client_secret,
|
|
|
|
:crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
|
|
|
|
)
|
2017-09-06 11:06:25 -06:00
|
|
|
else
|
|
|
|
changeset
|
|
|
|
end
|
|
|
|
end
|
2019-09-06 12:50:00 -06:00
|
|
|
|
2020-05-25 12:02:22 -06:00
|
|
|
@spec create(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
|
2020-02-28 01:16:40 -07:00
|
|
|
def create(params) do
|
2020-05-25 12:02:22 -06:00
|
|
|
%__MODULE__{}
|
|
|
|
|> register_changeset(params)
|
|
|
|
|> Repo.insert()
|
2020-02-28 01:16:40 -07:00
|
|
|
end
|
|
|
|
|
2020-05-25 12:02:22 -06:00
|
|
|
@spec update(pos_integer(), map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def update(id, params) do
|
|
|
|
with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
|
|
|
|
app
|
|
|
|
|> changeset(params)
|
|
|
|
|> Repo.update()
|
2020-02-28 01:16:40 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-06 12:50:00 -06:00
|
|
|
@doc """
|
|
|
|
Gets app by attrs or create new with attrs.
|
|
|
|
And updates the scopes if need.
|
|
|
|
"""
|
2020-05-25 12:02:22 -06:00
|
|
|
@spec get_or_make(map(), list(String.t())) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
|
2019-09-06 12:50:00 -06:00
|
|
|
def get_or_make(attrs, scopes) do
|
|
|
|
with %__MODULE__{} = app <- Repo.get_by(__MODULE__, attrs) do
|
|
|
|
update_scopes(app, scopes)
|
|
|
|
else
|
|
|
|
_e ->
|
|
|
|
%__MODULE__{}
|
|
|
|
|> register_changeset(Map.put(attrs, :scopes, scopes))
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app}
|
|
|
|
defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app}
|
|
|
|
|
|
|
|
defp update_scopes(%__MODULE__{} = app, scopes) do
|
|
|
|
app
|
|
|
|
|> change(%{scopes: scopes})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
2020-02-28 01:16:40 -07:00
|
|
|
|
2020-05-25 12:02:22 -06:00
|
|
|
@spec search(map()) :: {:ok, [t()], non_neg_integer()}
|
2020-02-28 01:16:40 -07:00
|
|
|
def search(params) do
|
|
|
|
query = from(a in __MODULE__)
|
|
|
|
|
|
|
|
query =
|
|
|
|
if params[:client_name] do
|
|
|
|
from(a in query, where: a.client_name == ^params[:client_name])
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
query =
|
|
|
|
if params[:client_id] do
|
|
|
|
from(a in query, where: a.client_id == ^params[:client_id])
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
query =
|
|
|
|
if Map.has_key?(params, :trusted) do
|
|
|
|
from(a in query, where: a.trusted == ^params[:trusted])
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
query =
|
|
|
|
from(u in query,
|
|
|
|
limit: ^params[:page_size],
|
|
|
|
offset: ^((params[:page] - 1) * params[:page_size])
|
|
|
|
)
|
|
|
|
|
|
|
|
count = Repo.aggregate(__MODULE__, :count, :id)
|
|
|
|
|
|
|
|
{:ok, Repo.all(query), count}
|
|
|
|
end
|
|
|
|
|
2021-08-25 21:01:04 -06:00
|
|
|
@spec get_user_apps(User.t()) :: {:ok, [t()], non_neg_integer()}
|
|
|
|
def get_user_apps(%User{id: user_id}) do
|
|
|
|
from(a in __MODULE__, where: a.user_id == ^user_id)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2020-05-25 12:02:22 -06:00
|
|
|
@spec destroy(pos_integer()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
|
2020-02-28 01:16:40 -07:00
|
|
|
def destroy(id) do
|
|
|
|
with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
|
|
|
|
Repo.delete(app)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec errors(Ecto.Changeset.t()) :: map()
|
|
|
|
def errors(changeset) do
|
|
|
|
Enum.reduce(changeset.errors, %{}, fn
|
|
|
|
{:client_name, {error, _}}, acc ->
|
|
|
|
Map.put(acc, :name, error)
|
|
|
|
|
|
|
|
{key, {error, _}}, acc ->
|
|
|
|
Map.put(acc, key, error)
|
|
|
|
end)
|
|
|
|
end
|
2017-09-06 11:06:25 -06:00
|
|
|
end
|