083b5ce092075cb4602b49c3ad760ac190da06ad
[akkoma] / lib / pleroma / web / o_auth / app.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OAuth.App do
6 use Ecto.Schema
7 import Ecto.Changeset
8 import Ecto.Query
9 alias Pleroma.Repo
10
11 @type t :: %__MODULE__{}
12
13 schema "apps" do
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)
21
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)
24
25 timestamps()
26 end
27
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])
31 end
32
33 @spec register_changeset(t(), map()) :: Ecto.Changeset.t()
34 def register_changeset(struct, params \\ %{}) do
35 changeset =
36 struct
37 |> changeset(params)
38 |> validate_required([:client_name, :redirect_uris, :scopes])
39
40 if changeset.valid? do
41 changeset
42 |> put_change(
43 :client_id,
44 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
45 )
46 |> put_change(
47 :client_secret,
48 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
49 )
50 else
51 changeset
52 end
53 end
54
55 @spec create(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
56 def create(params) do
57 %__MODULE__{}
58 |> register_changeset(params)
59 |> Repo.insert()
60 end
61
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
65 app
66 |> changeset(params)
67 |> Repo.update()
68 end
69 end
70
71 @doc """
72 Gets app by attrs or create new with attrs.
73 And updates the scopes if need.
74 """
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)
79 else
80 _e ->
81 %__MODULE__{}
82 |> register_changeset(Map.put(attrs, :scopes, scopes))
83 |> Repo.insert()
84 end
85 end
86
87 defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app}
88 defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app}
89
90 defp update_scopes(%__MODULE__{} = app, scopes) do
91 app
92 |> change(%{scopes: scopes})
93 |> Repo.update()
94 end
95
96 @spec search(map()) :: {:ok, [t()], non_neg_integer()}
97 def search(params) do
98 query = from(a in __MODULE__)
99
100 query =
101 if params[:client_name] do
102 from(a in query, where: a.client_name == ^params[:client_name])
103 else
104 query
105 end
106
107 query =
108 if params[:client_id] do
109 from(a in query, where: a.client_id == ^params[:client_id])
110 else
111 query
112 end
113
114 query =
115 if Map.has_key?(params, :trusted) do
116 from(a in query, where: a.trusted == ^params[:trusted])
117 else
118 query
119 end
120
121 query =
122 from(u in query,
123 limit: ^params[:page_size],
124 offset: ^((params[:page] - 1) * params[:page_size])
125 )
126
127 count = Repo.aggregate(__MODULE__, :count, :id)
128
129 {:ok, Repo.all(query), count}
130 end
131
132 @spec destroy(pos_integer()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
133 def destroy(id) do
134 with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
135 Repo.delete(app)
136 end
137 end
138
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)
144
145 {key, {error, _}}, acc ->
146 Map.put(acc, key, error)
147 end)
148 end
149
150 @spec get_app_by_id(pos_integer()) :: {:ok, map()}
151 def get_app_by_id(app_id) do
152 query =
153 __MODULE__
154 |> where([a], a.id == ^app_id)
155 |> select([a], %{name: a.client_name, website: a.website})
156
157 Repo.one!(query)
158 end
159 end