Problem/Motivation

We need to revoke both access and refresh tokens from the resource servers in a generic way. One use case is to revoke tokens in the back-end when the user logs out in front-end.

Proposed resolution

Implement a solution that is compliant with the specification RFC 7009. The PHP library we are using already has support for token revocation an issue for token revocation with a pull request.

Remaining tasks

User interface changes

  1. Implement the feature.
  2. Provide functional testing

Comments

skorzh created an issue. See original summary.

skdrupal88’s picture

Title: Revoke token » Revoke tokens
skdrupal88’s picture

Issue summary: View changes
e0ipso’s picture

Hello! Thanks for dropping by.

You will need to use Entity Query to find the tokens associated with the user and delete those entities.

e0ipso’s picture

Status: Active » Fixed
spleshka’s picture

Status: Fixed » Active

Hi @e0ipso,

Would you agree that it worth having the endpoint in OAuth to revoke the token? It feels like quite a security measure to be able to expire access and refresh tokens from remote.

(Setting the issue state to Active, since I believe it may require additional discussion / further dev actioning).

e0ipso’s picture

@Spleshka would you be able to do it with REST or JSON API? You can PATCH or DELETE the token entities, right?

ndobromirov’s picture

I am pretty sure it's responsibility of this module. Exposing the tokens form another module sounds like breaking the standard for that: https://tools.ietf.org/html/rfc7009

e0ipso’s picture

@ndobromirov that's a good point. However what I mean is: "the feature is not supported at the moment, but you can work around that with other modules. If that fits your use case, then great."

That being said, I'll be happy to review a patch that implements spec-compliant token revocation.

e0ipso’s picture

Issue summary: View changes
arnoldbird’s picture

"You will need to use Entity Query to find the tokens associated with the user and delete those entities."

This does not appear to be possible, because tokens are not associated with users in the database. All tokens are associated with an anonymous (zero) UID.

There must be some way, programmatically, to log a user out. I see that saving a user's account has the effect of invalidating (deleting?) their access token, so perhaps I just need to figure out where that happens in Drupal's code and then copy that approach.

In the meantime, does anyone have a solution to support a "Log out" button in an app? Can someone give me a hint? : )

Thanks

e0ipso’s picture

Take a look at the https://cgit.drupalcode.org/simple_oauth/tree/src/ExpiredCollector.php for more info on how that is accomplished.

arnoldbird’s picture

The collectForAccount method in that class seems to assume we can look up a user's tokens by the 'auth_user_id'. However, that column is always zero in the database.

Is there something I need to do so that, when the token is created, it gets associated with a Drupal UID? Is it normal for auth_user_id.oauth2_token to always be zero? I figure this is either something I'm doing wrong in my /oauth/token call, or it's an incomplete implementation in simple_oauth.

berdir’s picture

@e0ipso: The problem is that only works for *access tokens*, refresh tokens have no uid so it is impossible to identify them.

That said, a Log out button in the app should be easy, you just forget about the tokens in the app. they won't be cleaned up on the server but you will be logged out.

e0ipso’s picture

Yes, that was a response to:

There must be some way, programmatically, to log a user out. I see that saving a user's account has the effect of invalidating (deleting?) their access token, so perhaps I just need to figure out where that happens in Drupal's code and then copy that approach.

arnoldbird’s picture

Apologies! I just looked at my oauth2_token table and I see that the auth_user_id value is indeed set to the user ID for access tokens. I was wrong when I said...

that column is always zero in the database.

I swear I sorted on that column forward and back and could not find anything but zeros yesterday, but I must have missed it somehow.

Now that I see the auth_user_id column has the user ID in it, this becomes an easy problem to solve.

jyoti.singh’s picture

Can we have a endpoint to revoke the access token and the refresh token to make it generic.For example, if we have a headless system where the drupal endpoints are consumed by angular framework, we need to have a endpoint to revoke the tokens.

e0ipso’s picture

I think that what we need first is a patch to add the user identifier to the refresh tokens: #2974963: Link refresh tokens to the user they will identidy for query filtering

e0ipso’s picture

Title: Revoke tokens » [PP-1] Revoke refresh tokens
Status: Active » Postponed
jyoti.singh’s picture

The action to revoke all the access token should be done on the logout.

ayalon’s picture

I created a patch for this issue here:
https://www.drupal.org/project/simple_oauth/issues/2974963

Feel free to test it.

e0ipso’s picture

We still want this. Bubbling it up.

briangonzalezmia’s picture

@e0ipso Yes, please! #22

e0ipso’s picture

Just to be 💯 clear, I don't have plans to work on this. I was bubbling it up in #22 so anyone else could pick it up. I hope you understand.

matt_paz’s picture

Should this be closed now that #2974963 has been committed or are there other things in here #2974963 didn't resolve?

robertom’s picture

@matt_paz this issue is still valid.

I've found this issue on oauth2-server that could be useful.

robertom’s picture

Issue summary: View changes
robertom’s picture

Hi, sorry for my bad english.

I need to logout from an angular frontend and, at the same time, log out also from the drupal backend (I use drupal also as the Authentication Server for the Authorization code flow ).

I added two routes (only on POST):
- /oauth/revoke (revoke all access_token and refresh_token for the current user)
- /oauth/logout (same as /oauht/revoke but remove also session for the current user)

The /oauth/revoke isn't fully compliant with the RFC7009, but remove the tokens :D

This patch REQUIRE also the patch from comment #50 #2946882-50: Auth revoke on profile update otherwise it would not remove the refresh tokens.

robertom’s picture

Status: Postponed » Needs review

@e0ipso I know it's not the implementation required in the summary, but what do you think as a "temporary" solution?

robertom’s picture

Version: 8.x-3.x-dev » 8.x-4.x-dev
jainv18’s picture

@robertom -- Thanks for writing this patch. Much needed.

Can you please help me with the headers for invoking /oauth/logout -- for example, withCredentials: True or Authorization header etc.

Also, I assume that if we use the endpoint 'oauth/logout', it will also delete the cookie and ends the authentication of User.

harishst’s picture

+1 for the patch @robertom. I have used this in my custom module for creating Logout API.

kevishie’s picture

Is there anything holding this up? Seems like an OAuth implementation should have a revoke endpoint.

kevishie’s picture

Fixed @robertom's patch for 5.x

kevishie’s picture

After looking at this code more, the revoke endpoint should follow the RFC spec and take a token and token_type_hint parameter. This patch tries to get the current logged in user ($this->currentUser()) but in a headless, stateless API this wouldn't return a user.

bradjones1’s picture

Version: 8.x-4.x-dev » 5.x-dev
Status: Needs review » Needs work
Related issues: +#2946882: Auth revoke on profile update

Bumping major versions and linking related issue.

pq’s picture

Trying out this patch, I'm finding that using the revoke endpoint, access tokens are getting deleted but refresh tokens are not. It looks like this is because ExpiredCollector->collectForAccount() adds the condition $query->condition('bundle', 'refresh_token', '!='); (in 5.2 at least).

The patch passes a second parameter TRUE to ExpiredCollector->collectForAccount(), but this doesn't appear to do anything. Was the intention for that parameter to be something like $include_refresh_tokens and to bypass the condition if set to TRUE?

pq’s picture

Ah sorry, just read #28.

Ignore me. :)

realityloop’s picture

patch against 6.x

mediabounds’s picture

If it's helpful to others, we recently updated the Simple OAuth Revoke module which just adds the /oauth/revoke route as outlined by the RFC 7009.

(Note: The patch in this issue also adds an /oauth/logout endpoint; that's not part of the RFC 7009 spec so it's not in the Simple OAuth Revoke module.)

berenddeboer’s picture

To add to this long chain: here a merge request for the simple_oauth_revoke module that adds support for 6.x-dev and makes the module compatible with Drupal 11: https://www.drupal.org/project/simple_oauth_revoke/issues/3541068#commen...

bojan_dev’s picture

Version: 5.x-dev » 6.1.x-dev
Category: Support request » Feature request
Status: Needs work » Postponed

I don’t want to add any new features to versions of simple_oauth below 6.0. I see that there has been good progress in thephpleague/oauth2-server to support this feature: https://github.com/thephpleague/oauth2-server/pull/1473. Once it’s merged, we can start implementing it in version 6.1.x, including full test coverage.