1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.App do
10 @type t :: %__MODULE__{}
13 field(:client_name, :string)
14 field(:redirect_uris, :string)
15 field(:scopes, {:array, :string}, default: [])
16 field(:website, :string)
17 field(:client_id, :string)
18 field(:client_secret, :string)
23 def register_changeset(struct, params \\ %{}) do
26 |> cast(params, [:client_name, :redirect_uris, :scopes, :website])
27 |> validate_required([:client_name, :redirect_uris, :scopes])
29 if changeset.valid? do
33 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
37 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
45 Gets app by attrs or create new with attrs.
46 And updates the scopes if need.
48 @spec get_or_make(map(), list(String.t())) :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
49 def get_or_make(attrs, scopes) do
50 with %__MODULE__{} = app <- Repo.get_by(__MODULE__, attrs) do
51 update_scopes(app, scopes)
55 |> register_changeset(Map.put(attrs, :scopes, scopes))
60 defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app}
61 defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app}
63 defp update_scopes(%__MODULE__{} = app, scopes) do
65 |> change(%{scopes: scopes})