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.OAuth.App do
11 @type t :: %__MODULE__{}
14 field(:client_name, :string)
15 field(:redirect_uris, :string)
16 field(:scopes, {:array, :string}, default: [])
17 field(:website, :string)
18 field(:client_id, :string)
19 field(:client_secret, :string)
20 field(:trusted, :boolean, default: false)
22 has_many(:oauth_authorizations, Pleroma.Web.OAuth.Authorization, on_delete: :delete_all)
23 has_many(:oauth_tokens, Pleroma.Web.OAuth.Token, on_delete: :delete_all)
28 @spec changeset(t(), map()) :: Ecto.Changeset.t()
29 def changeset(struct, params) do
30 cast(struct, params, [:client_name, :redirect_uris, :scopes, :website, :trusted])
33 @spec register_changeset(t(), map()) :: Ecto.Changeset.t()
34 def register_changeset(struct, params \\ %{}) do
38 |> validate_required([:client_name, :redirect_uris, :scopes])
40 if changeset.valid? do
44 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
48 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
55 @spec create(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
58 |> register_changeset(params)
62 @spec update(pos_integer(), map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
63 def update(id, params) do
64 with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
72 Gets app by attrs or create new with attrs.
73 And updates the scopes if need.
75 @spec get_or_make(map(), list(String.t())) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
76 def get_or_make(attrs, scopes) do
77 with %__MODULE__{} = app <- Repo.get_by(__MODULE__, attrs) do
78 update_scopes(app, scopes)
82 |> register_changeset(Map.put(attrs, :scopes, scopes))
87 defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app}
88 defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app}
90 defp update_scopes(%__MODULE__{} = app, scopes) do
92 |> change(%{scopes: scopes})
96 @spec search(map()) :: {:ok, [t()], non_neg_integer()}
98 query = from(a in __MODULE__)
101 if params[:client_name] do
102 from(a in query, where: a.client_name == ^params[:client_name])
108 if params[:client_id] do
109 from(a in query, where: a.client_id == ^params[:client_id])
115 if Map.has_key?(params, :trusted) do
116 from(a in query, where: a.trusted == ^params[:trusted])
123 limit: ^params[:page_size],
124 offset: ^((params[:page] - 1) * params[:page_size])
127 count = Repo.aggregate(__MODULE__, :count, :id)
129 {:ok, Repo.all(query), count}
132 @spec destroy(pos_integer()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
134 with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
139 @spec errors(Ecto.Changeset.t()) :: map()
140 def errors(changeset) do
141 Enum.reduce(changeset.errors, %{}, fn
142 {:client_name, {error, _}}, acc ->
143 Map.put(acc, :name, error)
145 {key, {error, _}}, acc ->
146 Map.put(acc, key, error)
150 @spec get_app_by_id(pos_integer()) :: {:ok, map()}
151 def get_app_by_id(app_id) do
154 |> where([a], a.id == ^app_id)
155 |> select([a], %{name: a.client_name, website: a.website})