Problem/Motivation

Deleting a consumer does not delete its tokens. The oauth2_token.client reference now points to a missing entity, and the next request that uses one of those access tokens fails with a 500:

TypeError: Cannot assign null to property Drupal\simple_oauth\Authentication\TokenAuthUser::$consumer of type Drupal\consumers\Entity\Consumer

TokenAuthUser::__construct() assigns $token->get('client')->entity to a typed Consumer property without checking for NULL. The token still passes JWT validation, because the signature and expiry are fine and the token row exists and isn't revoked.

Simple OAuth already deletes tokens when a consumer is updated (simple_oauth_consumer_update() → TokenExpiryTriggerHandler::handleConsumerUpdate()). Nothing handles delete.

Steps to reproduce

  1. Create a confidential consumer with the client_credentials grant and a default user.
  2. Request a token: POST /oauth/token with grant_type=client_credentials.
  3. Delete the consumer.
  4. Request GET /oauth/debug?_format=json with Authorization: Bearer <token>.

Expected: 401. Actual: 500 with the TypeError above.

Proposed resolution

  • Implement hook_consumer_delete() and delete the consumer's tokens, mirroring handleConsumerUpdate().
  • In TokenAuthUser::__construct(), throw OAuthServerException::invalidClient() when the client reference doesn't load. Sites with tokens already orphaned by past deletes then get a 401 instead of a 500.

Remaining tasks

  • MR with a kernel test covering both changes.

AI disclosure

AI-Generated: Yes. I drafted this issue summary with Claude Code and reviewed it before filing.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mglaman created an issue. See original summary.

mglaman’s picture

Issue summary: View changes

mglaman’s picture

Status: Active » Needs review
attilatilman’s picture

Status: Needs review » Reviewed & tested by the community
vidit.anjaria’s picture

bojan_dev made their first commit to this issue’s fork.

  • bojan_dev committed 374f24ff on 6.1.x authored by mglaman
    fix: #3630880 Delete tokens when their consumer is deleted
    
    By: mglaman...
bojan_dev’s picture

Status: Reviewed & tested by the community » Fixed

Merged, thanks! Since #3630881 has been merged, refresh tokens now store their client, so I tightened testOnConsumerDelete() to also check that refresh tokens are deleted.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.