WIP: OAuth: identify the current token and revoke other sessions #7998

Draft
lambadalambda wants to merge 3 commits from lambadalambda/pleroma:feature/oauth-token-management into develop

Summary

  • Add a boolean current to GET /api/oauth_tokens: true only for the exact token authenticating the request, not other tokens for the same app. Password-only requests return false on all entries. No token secrets are exposed.
  • Add DELETE /api/oauth_tokens/others to revoke every other OAuth session for the authenticated account. The preserved token comes exclusively from request authentication; client-supplied preservation IDs are ignored. The operation refuses requests without a current user-bound OAuth token.
  • Perform one account-scoped database deletion excluding the current token, returning deleted IDs for asynchronous stream closure. Access and refresh credentials for the deleted sessions become unusable; current and foreign-account tokens remain valid.
  • Return an empty 204 on bulk success, including when no other sessions remain. Single-ID revocation keeps its existing empty 201 response and authorization behavior.
  • Route single-ID revocation through the same deletion/stream-closure helper. Only streams using actually deleted tokens are closed; requests for another account's token remain no-ops. Preserve the model's existing {count, nil} result for single-ID deletion.
  • Document the API and include a changelog fragment. No schema migration or deployment is part of this PR.

Testing

TDD: baseline 6 tests passed; new current-token/bulk-revocation regressions failed before implementation, as did the stream-closure regression. The original focused suites then passed all 16 tests. For the single-ID extension, the selective stream-closure regression failed before implementation while bulk and cross-account controls passed (3 tests, 1 failure). Updated focused suites pass all 21 tests.

Using Erlang 28.3 / Elixir 1.19.0:

mix format --check-formatted \
  lib/pleroma/web/o_auth/token.ex \
  lib/pleroma/web/o_auth/token_controller.ex \
  lib/pleroma/web/o_auth/token_view.ex \
  lib/pleroma/web/router.ex \
  test/pleroma/web/o_auth/token_controller_test.exs \
  test/pleroma/web/o_auth/token_revocation_test.exs \
  test/pleroma/web/o_auth/token_test.exs

MIX_ENV=test mix test \
  test/pleroma/web/o_auth \
  test/pleroma/web/plugs \
  test/pleroma/web/streamer_test.exs

Result: 367 tests, 0 failures. Coverage includes bearer-token identification, same-app sessions, real Basic-auth listing and bulk refusal, ignored preservation IDs, current/foreign account protection, missing/mismatched/invalid authentication (including direct model guards), refresh-token invalidation, repeated calls, selective stream closure for both single and bulk revocation, single-ID cross-account no-op protection, and the single-delete result shape. Independent correctness/security and architecture/DRY review of both the original feature and the single-ID extension found no blockers.

Scope and follow-up

This is backend support for a future frontend “revoke all other tokens” action. No frontend implementation or production token revocations are included.

Stream closure uses the existing asynchronous, local-registry mechanism. Tokens issued after the bulk deletion are not covered. Single-ID stream closure is included in this PR; the global all-token deletion helper is unchanged.

## Summary - Add a boolean `current` to `GET /api/oauth_tokens`: true only for the exact token authenticating the request, not other tokens for the same app. Password-only requests return false on all entries. No token secrets are exposed. - Add `DELETE /api/oauth_tokens/others` to revoke every other OAuth session for the authenticated account. The preserved token comes exclusively from request authentication; client-supplied preservation IDs are ignored. The operation refuses requests without a current user-bound OAuth token. - Perform one account-scoped database deletion excluding the current token, returning deleted IDs for asynchronous stream closure. Access and refresh credentials for the deleted sessions become unusable; current and foreign-account tokens remain valid. - Return an empty 204 on bulk success, including when no other sessions remain. Single-ID revocation keeps its existing empty 201 response and authorization behavior. - Route single-ID revocation through the same deletion/stream-closure helper. Only streams using actually deleted tokens are closed; requests for another account's token remain no-ops. Preserve the model's existing `{count, nil}` result for single-ID deletion. - Document the API and include a changelog fragment. No schema migration or deployment is part of this PR. ## Testing TDD: baseline 6 tests passed; new current-token/bulk-revocation regressions failed before implementation, as did the stream-closure regression. The original focused suites then passed all 16 tests. For the single-ID extension, the selective stream-closure regression failed before implementation while bulk and cross-account controls passed (3 tests, 1 failure). Updated focused suites pass all 21 tests. Using Erlang 28.3 / Elixir 1.19.0: ```sh mix format --check-formatted \ lib/pleroma/web/o_auth/token.ex \ lib/pleroma/web/o_auth/token_controller.ex \ lib/pleroma/web/o_auth/token_view.ex \ lib/pleroma/web/router.ex \ test/pleroma/web/o_auth/token_controller_test.exs \ test/pleroma/web/o_auth/token_revocation_test.exs \ test/pleroma/web/o_auth/token_test.exs MIX_ENV=test mix test \ test/pleroma/web/o_auth \ test/pleroma/web/plugs \ test/pleroma/web/streamer_test.exs ``` Result: **367 tests, 0 failures**. Coverage includes bearer-token identification, same-app sessions, real Basic-auth listing and bulk refusal, ignored preservation IDs, current/foreign account protection, missing/mismatched/invalid authentication (including direct model guards), refresh-token invalidation, repeated calls, selective stream closure for both single and bulk revocation, single-ID cross-account no-op protection, and the single-delete result shape. Independent correctness/security and architecture/DRY review of both the original feature and the single-ID extension found no blockers. ## Scope and follow-up This is backend support for a future frontend “revoke all other tokens” action. No frontend implementation or production token revocations are included. Stream closure uses the existing asynchronous, local-registry mechanism. Tokens issued after the bulk deletion are not covered. Single-ID stream closure is included in this PR; the global all-token deletion helper is unchanged.
feat(oauth): identify current token and revoke other sessions
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
607908843e
fix(oauth): close streams when revoking a single session
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
0f81645fb1
lambadalambda force-pushed feature/oauth-token-management from 0f81645fb1
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
to 1025341eac
Some checks failed
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.17 Pipeline failed
2026-10-29 07:02:56 +00:00
Compare
Author
Owner

Rebased on develop after #8013: DELETE /api/oauth_tokens/others now needs write:security like DELETE /api/oauth_tokens/:id (docs updated, test added). Tests adjusted to the scope checks; the #8013 changelog entry is corrected (password logins still skip the scope check, so Basic auth keeps working).

Rebased on develop after #8013: `DELETE /api/oauth_tokens/others` now needs `write:security` like `DELETE /api/oauth_tokens/:id` (docs updated, test added). Tests adjusted to the scope checks; the #8013 changelog entry is corrected (password logins still skip the scope check, so Basic auth keeps working).
Author
Owner

/oc review

/oc review
opencode-fix-bot left a comment

opencode-agent:job=140:generation=1

Review of develop -> feature/oauth-token-management (current-token flag + DELETE /api/oauth_tokens/others + single-ID stream closure). No concrete defects introduced by this PR -- no P0/P1/P2 findings.

What was checked:

  • GET /api/oauth_tokens marks current: true only for the exact authenticating token ID; password-auth requests get false everywhere and no token/refresh secrets are rendered. Comparison is by integer token ID, not by app.
  • Bulk revocation preserves the current token exclusively from request authentication (client-supplied IDs ignored), is scoped to the authenticated account in both the controller pattern match and the model function head, and refuses token-less/mismatched authentication with 403. Access and refresh credentials die with the deleted rows; current and foreign-account tokens are untouched.
  • Route order (/others before /:id) is correct, so the static route is reachable.
  • Single-ID revocation keeps its {count, nil} result shape and 201 behavior (including cross-account no-ops, so no existence oracle); bulk returns empty 204 including the nothing-left case.
  • Stream closure is selective to actually-deleted token IDs for both single and bulk paths; the cross-account no-op closes nothing.
  • write:security gating on both revocation actions matches the existing single-ID behavior (broad write still implies it via the established descendant-scope semantics), while password auth skips the scope plug and is then refused by the controller guard -- matching the documented behavior.
  • Docs and changelog fragments match the implemented behavior.

Validation run against the exact PR head (Erlang 28 / Elixir 1.19): mix format --check-formatted on all 7 touched source/test files passes; mix test on token/controller/revocation suites passes (26 tests, 0 failures); broader test/pleroma/web/o_auth + test/pleroma/web/plugs + streamer_test.exs passes (373 tests, 0 failures).

Merge-readiness: 5/5 -- production-ready with no known findings.

opencode-agent:job=140:generation=1 Review of `develop` -> `feature/oauth-token-management` (current-token flag + `DELETE /api/oauth_tokens/others` + single-ID stream closure). No concrete defects introduced by this PR -- no P0/P1/P2 findings. What was checked: - `GET /api/oauth_tokens` marks `current: true` only for the exact authenticating token ID; password-auth requests get `false` everywhere and no token/refresh secrets are rendered. Comparison is by integer token ID, not by app. - Bulk revocation preserves the current token exclusively from request authentication (client-supplied IDs ignored), is scoped to the authenticated account in both the controller pattern match and the model function head, and refuses token-less/mismatched authentication with 403. Access and refresh credentials die with the deleted rows; current and foreign-account tokens are untouched. - Route order (`/others` before `/:id`) is correct, so the static route is reachable. - Single-ID revocation keeps its `{count, nil}` result shape and 201 behavior (including cross-account no-ops, so no existence oracle); bulk returns empty 204 including the nothing-left case. - Stream closure is selective to actually-deleted token IDs for both single and bulk paths; the cross-account no-op closes nothing. - `write:security` gating on both revocation actions matches the existing single-ID behavior (broad `write` still implies it via the established descendant-scope semantics), while password auth skips the scope plug and is then refused by the controller guard -- matching the documented behavior. - Docs and changelog fragments match the implemented behavior. Validation run against the exact PR head (Erlang 28 / Elixir 1.19): `mix format --check-formatted` on all 7 touched source/test files passes; `mix test` on token/controller/revocation suites passes (26 tests, 0 failures); broader `test/pleroma/web/o_auth` + `test/pleroma/web/plugs` + `streamer_test.exs` passes (373 tests, 0 failures). Merge-readiness: 5/5 -- production-ready with no known findings.
Some checks failed
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.17 Pipeline failed
This pull request has changes conflicting with the target branch.
  • lib/pleroma/web/o_auth/token_controller.ex
  • lib/pleroma/web/o_auth/token_view.ex
  • test/pleroma/web/o_auth/token_controller_test.exs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u feature/oauth-token-management:lambadalambda-feature/oauth-token-management
git switch lambadalambda-feature/oauth-token-management

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch develop
git merge --no-ff lambadalambda-feature/oauth-token-management
git switch lambadalambda-feature/oauth-token-management
git rebase develop
git switch develop
git merge --ff-only lambadalambda-feature/oauth-token-management
git switch lambadalambda-feature/oauth-token-management
git rebase develop
git switch develop
git merge --no-ff lambadalambda-feature/oauth-token-management
git switch develop
git merge --squash lambadalambda-feature/oauth-token-management
git switch develop
git merge --ff-only lambadalambda-feature/oauth-token-management
git switch develop
git merge lambadalambda-feature/oauth-token-management
git push origin develop
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
pleroma/pleroma!7998
No description provided.