akkoma/lib/pleroma/web/oauth/app.ex

42 lines
1.1 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 08:41:47 -07:00
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2017-09-07 00:58:10 -06:00
defmodule Pleroma.Web.OAuth.App do
use Ecto.Schema
2019-02-09 08:20:18 -07:00
import Ecto.Changeset
@type t :: %__MODULE__{}
schema "apps" do
2018-03-30 07:01:53 -06:00
field(:client_name, :string)
field(:redirect_uris, :string)
field(:scopes, {:array, :string}, default: [])
2018-03-30 07:01:53 -06:00
field(:website, :string)
field(:client_id, :string)
field(:client_secret, :string)
timestamps()
end
def register_changeset(struct, params \\ %{}) do
2018-03-30 07:01:53 -06:00
changeset =
struct
|> cast(params, [:client_name, :redirect_uris, :scopes, :website])
|> validate_required([:client_name, :redirect_uris, :scopes])
if changeset.valid? do
changeset
|> 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)
)
else
changeset
end
end
end