Slight cleanup.
[akkoma] / lib / pleroma / web / oauth / authorization.ex
1 defmodule Pleroma.Web.OAuth.Authorization do
2 use Ecto.Schema
3
4 alias Pleroma.{User, Repo}
5 alias Pleroma.Web.OAuth.{Authorization, App}
6
7 schema "oauth_authorizations" do
8 field :token, :string
9 field :valid_until, :naive_datetime
10 field :used, :boolean, default: false
11 belongs_to :user, Pleroma.User
12 belongs_to :app, Pleroma.App
13
14 timestamps()
15 end
16
17 def create_authorization(%App{} = app, %User{} = user) do
18 token = :crypto.strong_rand_bytes(32) |> Base.url_encode64
19
20 authorization = %Authorization{
21 token: token,
22 used: false,
23 user_id: user.id,
24 app_id: app.id,
25 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now, 60 * 10)
26 }
27
28 Repo.insert(authorization)
29 end
30 end