Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Add grant revoke logic to OAuth core (#6701)

Artur Patab04e60123206
diff --git a/lib/plausible/oauth.ex b/lib/plausible/oauth.ex
index bd31b576d1..24b724df0d 100644
--- a/lib/plausible/oauth.ex
+++ b/lib/plausible/oauth.ex
@@ -213,6 +213,42 @@ defmodule Plausible.OAuth do
end
end
+ @doc """
+ Revokes a grant, invalidating its access and refresh tokens.
+
+ Recorded on the row rather than deleted, so a revoked connection can still be
+ displayed and its `previous_refresh_token_hash` still recognised. Idempotent:
+ an already-revoked grant keeps its original `revoked_at`.
+ """
+ @spec revoke_grant(Grant.t()) :: :ok
+ def revoke_grant(%Grant{} = grant) do
+ now = now()
+
+ Repo.update_all(
+ from(g in Grant, where: g.id == ^grant.id and is_nil(g.revoked_at)),
+ set: [revoked_at: now, updated_at: now]
+ )
+
+ :ok
+ end
+
+ @doc """
+ Revokes every live grant a user holds against a team, when they stop being a member of it.
+ """
+ @spec after_user_removed_from_team(Plausible.Teams.Team.t(), Plausible.Auth.User.t()) :: :ok
+ def after_user_removed_from_team(team, user) do
+ now = now()
+
+ Repo.update_all(
+ from(g in Grant,
+ where: g.team_id == ^team.id and g.user_id == ^user.id and is_nil(g.revoked_at)
+ ),
+ set: [revoked_at: now, updated_at: now]
+ )
+
+ :ok
+ end
+
defp normalize_resource(resource) do
case ProtectedResources.get_by_url(resource) do
{:ok, protected_resource} -> {:ok, protected_resource}
diff --git a/lib/plausible/teams/memberships/leave.ex b/lib/plausible/teams/memberships/leave.ex
index 18436fcf6b..e81a0779fd 100644
--- a/lib/plausible/teams/memberships/leave.ex
+++ b/lib/plausible/teams/memberships/leave.ex
@@ -27,6 +27,11 @@ defmodule Plausible.Teams.Memberships.Leave do
team_membership.team,
team_membership.user
)
+
+ Plausible.OAuth.after_user_removed_from_team(
+ team_membership.team,
+ team_membership.user
+ )
end)
if Keyword.get(opts, :send_email?, true) do
diff --git a/lib/plausible/teams/memberships/remove.ex b/lib/plausible/teams/memberships/remove.ex
index 52261f3650..cd05892605 100644
--- a/lib/plausible/teams/memberships/remove.ex
+++ b/lib/plausible/teams/memberships/remove.ex
@@ -28,6 +28,11 @@ defmodule Plausible.Teams.Memberships.Remove do
team_membership.team,
team_membership.user
)
+
+ Plausible.OAuth.after_user_removed_from_team(
+ team_membership.team,
+ team_membership.user
+ )
end)
if Keyword.get(opts, :send_email?, true) do
diff --git a/test/plausible/oauth_test.exs b/test/plausible/oauth_test.exs
index 116106e611..fbdf687247 100644
--- a/test/plausible/oauth_test.exs
+++ b/test/plausible/oauth_test.exs
@@ -6,6 +6,7 @@ defmodule Plausible.OAuthTest do
team, scopes and resource
- `Step 2: consume_authorization_code/2` redeems that code, exactly once
- `Step 3: issue_token/1 and find_access_token/2`: opens a grant from the redeemed code, returns token AND resolves an access token back to its grant
+ - `Step 4: revoke_grant/1 and after_user_removed_from_team/2` invalidates both tokens
"""
use Plausible.DataCase
@@ -359,6 +360,82 @@ defmodule Plausible.OAuthTest do
end
end
+ describe "Step 4: revoke_grant/1 and after_user_removed_from_team/2" do
+ test "invalidates the grant's access token", %{user: user, team: team} do
+ {grant, access_token} = issue_grant(user, team)
+
+ assert :ok = OAuth.revoke_grant(grant)
+
+ assert {:error, :invalid_token} = OAuth.find_access_token(access_token, mcp())
+ end
+
+ test "keeps the original revoked_at when repeated", %{user: user, team: team} do
+ {grant, _access_token} = issue_grant(user, team)
+
+ assert :ok = OAuth.revoke_grant(grant)
+
+ revoked_at = past()
+
+ Plausible.Repo.update_all(
+ from(g in Grant, where: g.id == ^grant.id),
+ set: [revoked_at: revoked_at]
+ )
+
+ assert :ok = OAuth.revoke_grant(grant)
+
+ assert_matches %Grant{revoked_at: ^revoked_at} = Plausible.Repo.get!(Grant, grant.id)
+ end
+
+ test "removing a member revokes the grants they held against the team" do
+ owner = new_user()
+ {:ok, team} = Plausible.Teams.get_or_create(owner)
+ member = add_member(team, role: :editor)
+
+ {grant, access_token} = issue_grant(member, team)
+
+ other_owner = new_user()
+ {:ok, other_team} = Plausible.Teams.get_or_create(other_owner)
+ add_member(other_team, role: :editor, user: member)
+
+ {kept, kept_token} = issue_grant(member, other_team)
+
+ assert {:ok, _} =
+ Plausible.Teams.Memberships.Remove.remove(team, member.id, owner,
+ send_email?: false
+ )
+
+ assert %Grant{revoked_at: revoked_at} = Plausible.Repo.get!(Grant, grant.id)
+ assert revoked_at
+ assert {:error, :invalid_token} = OAuth.find_access_token(access_token, mcp())
+
+ assert_matches %Grant{revoked_at: nil} = Plausible.Repo.get!(Grant, kept.id)
+ assert {:ok, _} = OAuth.find_access_token(kept_token, mcp())
+ end
+
+ test "a member leaving revokes the grants they held against the team" do
+ owner = new_user()
+ {:ok, team} = Plausible.Teams.get_or_create(owner)
+ member = add_member(team, role: :editor)
+
+ {grant, _access_token} = issue_grant(member, team)
+
+ other_owner = new_user()
+ {:ok, other_team} = Plausible.Teams.get_or_create(other_owner)
+ add_member(other_team, role: :editor, user: member)
+
+ {kept, kept_token} = issue_grant(member, other_team)
+
+ assert {:ok, _} =
+ Plausible.Teams.Memberships.Leave.leave(team, member, send_email?: false)
+
+ assert %Grant{revoked_at: revoked_at} = Plausible.Repo.get!(Grant, grant.id)
+ assert revoked_at
+
+ assert_matches %Grant{revoked_at: nil} = Plausible.Repo.get!(Grant, kept.id)
+ assert {:ok, _} = OAuth.find_access_token(kept_token, mcp())
+ end
+ end
+
defp pkce do
verifier = :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
challenge = :crypto.hash(:sha256, verifier) |> Base.url_encode64(padding: false)