diff --git a/lib/pleroma/web/o_auth/token.ex b/lib/pleroma/web/o_auth/token.ex index d96425094..28f1f2dee 100644 --- a/lib/pleroma/web/o_auth/token.ex +++ b/lib/pleroma/web/o_auth/token.ex @@ -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 diff --git a/lib/pleroma/web/o_auth/token_view.ex b/lib/pleroma/web/o_auth/token_view.ex index ee54280ec..09e0a18bc 100644 --- a/lib/pleroma/web/o_auth/token_view.ex +++ b/lib/pleroma/web/o_auth/token_view.ex @@ -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 diff --git a/lib/pleroma/web/plugs/o_auth_plug.ex b/lib/pleroma/web/plugs/o_auth_plug.ex index b59ac9d3e..6f366d3cb 100644 --- a/lib/pleroma/web/plugs/o_auth_plug.ex +++ b/lib/pleroma/web/plugs/o_auth_plug.ex @@ -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) diff --git a/priv/repo/migrations/20170906152508_create_o_auth_token.exs b/priv/repo/migrations/20170906152508_create_o_auth_token.exs index 14ce55b5a..5b662a5a3 100644 --- a/priv/repo/migrations/20170906152508_create_o_auth_token.exs +++ b/priv/repo/migrations/20170906152508_create_o_auth_token.exs @@ -12,6 +12,7 @@ defmodule Pleroma.Repo.Migrations.CreateOAuthToken do add(:token, :string) add(:refresh_token, :string) add(:valid_until, :naive_datetime_usec) + add(:last_active, :naive_datetime_usec) timestamps() end diff --git a/priv/repo/migrations/202611061856_add_token_activity.exs b/priv/repo/migrations/202611061856_add_token_activity.exs new file mode 100644 index 000000000..98f4b0184 --- /dev/null +++ b/priv/repo/migrations/202611061856_add_token_activity.exs @@ -0,0 +1,13 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# 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 diff --git a/test/pleroma/web/o_auth/token_controller_test.exs b/test/pleroma/web/o_auth/token_controller_test.exs index e41584cd0..75167c346 100644 --- a/test/pleroma/web/o_auth/token_controller_test.exs +++ b/test/pleroma/web/o_auth/token_controller_test.exs @@ -63,7 +63,7 @@ defmodule Pleroma.Web.OAuth.TokenControllerTest do |> Map.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 test "marks which token is being used for the request" do