Attempt at adding a last active field to tokens
This commit is contained in:
parent
ab8fae0b94
commit
a06e471db7
6 changed files with 26 additions and 1 deletions
|
|
@ -21,6 +21,7 @@ defmodule Pleroma.Web.OAuth.Token do
|
|||
field(:refresh_token, :string)
|
||||
field(:scopes, {:array, :string}, default: [])
|
||||
field(:valid_until, :naive_datetime_usec)
|
||||
field(:last_active, :naive_datetime_usec)
|
||||
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
||||
belongs_to(:app, App)
|
||||
|
||||
|
|
@ -96,6 +97,14 @@ defmodule Pleroma.Web.OAuth.Token do
|
|||
|> validate_required([:valid_until])
|
||||
end
|
||||
|
||||
defp update_activity() do
|
||||
last_active = NaiveDateTime.utc_now()
|
||||
|
||||
changeset
|
||||
|> change(%{last_active: last_active})
|
||||
|> validate_required([:last_active])
|
||||
end
|
||||
|
||||
@spec create(App.t(), User.t(), map()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
|
||||
def create(%App{} = app, %User{} = user, attrs \\ %{}) do
|
||||
with {:ok, token} <- do_create(app, user, attrs) do
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ defmodule Pleroma.Web.OAuth.TokenView do
|
|||
%{
|
||||
id: token_entry.id,
|
||||
valid_until: token_entry.valid_until,
|
||||
last_active: token_entry.last_active,
|
||||
app_name: token_entry.app.client_name,
|
||||
scopes: token_entry.scopes,
|
||||
is_current_token: token_entry.id == current_token.id
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ defmodule Pleroma.Web.Plugs.OAuthPlug do
|
|||
with {:ok, token_str} <- fetch_token_str(conn) do
|
||||
with {:ok, user, user_token} <- fetch_user_and_token(token_str),
|
||||
false <- Token.expired?(user_token) do
|
||||
Token.update_activity()
|
||||
conn
|
||||
|> assign(:token, user_token)
|
||||
|> assign(:user, user)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue