Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Token Reuse Detection #567

Merged
merged 2 commits into from
Mar 8, 2021

Conversation

svrakitin
Copy link
Contributor

Related issue

ory/hydra#2022

Proposed changes

This change makes refresh_token grant handler to check if we received fosite.ErrInactiveToken error from storage, which means there is an attempt to refresh access token using previously-used or revoked refresh token.

This is similar to what is done in Authorization Code Flow.

Checklist

  • I have read the contributing guidelines
    and signed the CLA.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security vulnerability, I
    confirm that I got green light (please contact
    security@ory.sh) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added necessary documentation within the code base (if
    appropriate).

Further comments

These are changes just for fosite. I have another PR to make for hydra, so its persistent storage stops deleting refresh tokens from database on revocation. Instead it will mark them as inactive similar to what's done for auth codes.

@svrakitin svrakitin changed the title Token Reuse Detection feat: Token Reuse Detection Mar 5, 2021
Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work on this, this looks great already!

I think it would make sense to add an e2e / integration test to https://github.com/ory/fosite/tree/master/integration to check that everything works well together!

Apart from that I think this is pretty much good to go as I think that this should not have any breaking changes, as ErrNotFound still behaves as before!

@@ -64,7 +64,14 @@ func (c *RefreshTokenGrantHandler) HandleTokenEndpointRequest(ctx context.Contex
refresh := request.GetRequestForm().Get("refresh_token")
signature := c.RefreshTokenStrategy.RefreshTokenSignature(refresh)
originalRequest, err := c.TokenRevocationStorage.GetRefreshTokenSession(ctx, signature, request.GetSession())
if errors.Is(err, fosite.ErrNotFound) {
if errors.Is(err, fosite.ErrInactiveToken) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, the idea is that the storage would return a new error code ErrInactiveToken when a refresh token was used or revoked - am I correct with that assumption?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly, same way it does for auth codes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice makes sense!

@svrakitin
Copy link
Contributor Author

@aeneasr Thanks. I will add some integration tests.

@aeneasr
Copy link
Member

aeneasr commented Mar 5, 2021

Awesome, thank you!

@svrakitin svrakitin requested a review from aeneasr March 5, 2021 18:36
Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again and sorry for the late review, I am on holidays ;)

integration/refresh_token_grant_test.go Show resolved Hide resolved
@svrakitin
Copy link
Contributor Author

@aeneasr Enjoy your holidays. This is not urgent.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you! 🎉 Your contribution makes Ory better :)

@aeneasr aeneasr merged commit db7f981 into ory:master Mar 8, 2021
@svrakitin
Copy link
Contributor Author

@aeneasr Thanks for pleasant contributor experience. I will prepare a PR for hydra as soon as there is a new fosite release.

@aeneasr
Copy link
Member

aeneasr commented Mar 8, 2021

:) v0.39.0 is out now!

@mitar
Copy link
Contributor

mitar commented Mar 10, 2021

@svrakitin Yes, I must say I always admire how well @aeneasr manages to handle maintaining this project. At the same time pretty opinionated, but still welcoming. :-) A rare mix.

@aeneasr
Copy link
Member

aeneasr commented Mar 11, 2021

Thank you two, I'm blushing ☺️

aeneasr pushed a commit to ory/hydra that referenced this pull request Mar 23, 2021
This patch adds support for Refresh Token reuse Detection introduced by ory/fosite#567. Ory Hydra's persister no longer deletes refresh tokens when using them, but instead deactivates them - similar to how authorization codes work.

Closes #2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants