X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fweb%2Foauth%2Fauthorization.ex;h=5a68bd5932a1419d6ae6c1189a79f0c0cf1dec0e;hb=2702df489fb7bbc2dbb0012f5b7eb8c3278faebe;hp=9423c9632b6570b7c6ff67fcf509a5c432fcd36e;hpb=2a298d70f9938d1b6d5af04d8b8863fdd3299f46;p=akkoma diff --git a/lib/pleroma/web/oauth/authorization.ex b/lib/pleroma/web/oauth/authorization.ex index 9423c9632..5a68bd593 100644 --- a/lib/pleroma/web/oauth/authorization.ex +++ b/lib/pleroma/web/oauth/authorization.ex @@ -1,8 +1,10 @@ defmodule Pleroma.Web.OAuth.Authorization do use Ecto.Schema - alias Pleroma.{App, User, Repo} - alias Pleroma.Web.OAuth.Authorization + alias Pleroma.{User, Repo} + alias Pleroma.Web.OAuth.{Authorization, App} + + import Ecto.{Changeset} schema "oauth_authorizations" do field :token, :string @@ -27,4 +29,19 @@ defmodule Pleroma.Web.OAuth.Authorization do Repo.insert(authorization) end + + def use_changeset(%Authorization{} = auth, params) do + auth + |> cast(params, [:used]) + |> validate_required([:used]) + end + + def use_token(%Authorization{used: false, valid_until: valid_until} = auth) do + if NaiveDateTime.diff(NaiveDateTime.utc_now, valid_until) < 0 do + Repo.update(use_changeset(auth, %{used: true})) + else + {:error, "Token expired"} + end + end + def use_token(%Authorization{used: true}), do: {:error, "Already used"} end