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(:refresh_token, :string)
|
||||||
field(:scopes, {:array, :string}, default: [])
|
field(:scopes, {:array, :string}, default: [])
|
||||||
field(:valid_until, :naive_datetime_usec)
|
field(:valid_until, :naive_datetime_usec)
|
||||||
|
field(:last_active, :naive_datetime_usec)
|
||||||
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
||||||
belongs_to(:app, App)
|
belongs_to(:app, App)
|
||||||
|
|
||||||
|
|
@ -96,6 +97,14 @@ defmodule Pleroma.Web.OAuth.Token do
|
||||||
|> validate_required([:valid_until])
|
|> validate_required([:valid_until])
|
||||||
end
|
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()}
|
@spec create(App.t(), User.t(), map()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
|
||||||
def create(%App{} = app, %User{} = user, attrs \\ %{}) do
|
def create(%App{} = app, %User{} = user, attrs \\ %{}) do
|
||||||
with {:ok, token} <- do_create(app, 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,
|
id: token_entry.id,
|
||||||
valid_until: token_entry.valid_until,
|
valid_until: token_entry.valid_until,
|
||||||
|
last_active: token_entry.last_active,
|
||||||
app_name: token_entry.app.client_name,
|
app_name: token_entry.app.client_name,
|
||||||
scopes: token_entry.scopes,
|
scopes: token_entry.scopes,
|
||||||
is_current_token: token_entry.id == current_token.id
|
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, token_str} <- fetch_token_str(conn) do
|
||||||
with {:ok, user, user_token} <- fetch_user_and_token(token_str),
|
with {:ok, user, user_token} <- fetch_user_and_token(token_str),
|
||||||
false <- Token.expired?(user_token) do
|
false <- Token.expired?(user_token) do
|
||||||
|
Token.update_activity()
|
||||||
conn
|
conn
|
||||||
|> assign(:token, user_token)
|
|> assign(:token, user_token)
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ defmodule Pleroma.Repo.Migrations.CreateOAuthToken do
|
||||||
add(:token, :string)
|
add(:token, :string)
|
||||||
add(:refresh_token, :string)
|
add(:refresh_token, :string)
|
||||||
add(:valid_until, :naive_datetime_usec)
|
add(:valid_until, :naive_datetime_usec)
|
||||||
|
add(:last_active, :naive_datetime_usec)
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
||||||
13
priv/repo/migrations/202611061856_add_token_activity.exs
Normal file
13
priv/repo/migrations/202611061856_add_token_activity.exs
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Repo.Migrations.AddTokenActivity do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
alter table(:oauth_tokens) do
|
||||||
|
add_if_not_exists(:last_active, :naive_datetime_usec)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -63,7 +63,7 @@ defmodule Pleroma.Web.OAuth.TokenControllerTest do
|
||||||
|> Map.keys()
|
|> Map.keys()
|
||||||
|
|
||||||
assert Enum.sort(keys) ==
|
assert Enum.sort(keys) ==
|
||||||
Enum.sort(["id", "app_name", "valid_until", "scopes", "is_current_token"])
|
Enum.sort(["id", "app_name", "valid_until", "scopes", "is_current_token", "last_active"])
|
||||||
end
|
end
|
||||||
|
|
||||||
test "marks which token is being used for the request" do
|
test "marks which token is being used for the request" do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue