1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.App do
12 @type t :: %__MODULE__{}
15 field(:client_name, :string)
16 field(:redirect_uris, :string)
17 field(:scopes, {:array, :string}, default: [])
18 field(:website, :string)
19 field(:client_id, :string)
20 field(:client_secret, :string)
21 field(:trusted, :boolean, default: false)
23 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
25 has_many(:oauth_authorizations, Pleroma.Web.OAuth.Authorization, on_delete: :delete_all)
26 has_many(:oauth_tokens, Pleroma.Web.OAuth.Token, on_delete: :delete_all)
31 @spec changeset(t(), map()) :: Ecto.Changeset.t()
32 def changeset(struct, params) do
33 cast(struct, params, [:client_name, :redirect_uris, :scopes, :website, :trusted, :user_id])
36 @spec register_changeset(t(), map()) :: Ecto.Changeset.t()
37 def register_changeset(struct, params \\ %{}) do
41 |> validate_required([:client_name, :redirect_uris, :scopes])
43 if changeset.valid? do
47 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
51 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
58 @spec create(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
61 |> register_changeset(params)
65 @spec update(pos_integer(), map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
66 def update(id, params) do
67 with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
75 Gets app by attrs or create new with attrs.
76 And updates the scopes if need.
78 @spec get_or_make(map(), list(String.t())) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
79 def get_or_make(attrs, scopes) do
80 with %__MODULE__{} = app <- Repo.get_by(__MODULE__, attrs) do
81 update_scopes(app, scopes)
85 |> register_changeset(Map.put(attrs, :scopes, scopes))
90 defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app}
91 defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app}
93 defp update_scopes(%__MODULE__{} = app, scopes) do
95 |> change(%{scopes: scopes})
99 @spec search(map()) :: {:ok, [t()], non_neg_integer()}
100 def search(params) do
101 query = from(a in __MODULE__)
104 if params[:client_name] do
105 from(a in query, where: a.client_name == ^params[:client_name])
111 if params[:client_id] do
112 from(a in query, where: a.client_id == ^params[:client_id])
118 if Map.has_key?(params, :trusted) do
119 from(a in query, where: a.trusted == ^params[:trusted])
126 limit: ^params[:page_size],
127 offset: ^((params[:page] - 1) * params[:page_size])
130 count = Repo.aggregate(__MODULE__, :count, :id)
132 {:ok, Repo.all(query), count}
135 @spec get_user_apps(User.t()) :: {:ok, [t()], non_neg_integer()}
136 def get_user_apps(%User{id: user_id}) do
137 from(a in __MODULE__, where: a.user_id == ^user_id)
141 @spec destroy(pos_integer()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
143 with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
148 @spec errors(Ecto.Changeset.t()) :: map()
149 def errors(changeset) do
150 Enum.reduce(changeset.errors, %{}, fn
151 {:client_name, {error, _}}, acc ->
152 Map.put(acc, :name, error)
154 {key, {error, _}}, acc ->
155 Map.put(acc, key, error)