Refresh token handling
I've been working on our oauth2-server implementation recently, and refresh tokens became a subject of interest. Research made me conclude that there are some other things that could be improved upon regarding refresh tokens.
It seems a bit more fitting to require a special scope from a client before it is included
Most implementations in the wild seem to require the offline (e.g. Google) or offline_access (e.g. Okta) scope or similar to enable refresh tokens.
This seems to stem from an OpenID thing, which this library does not implement, but that also means Google is not OpenID spec compliant but does implement it similarly. https://openid.net/specs/openid-connect-core-1_0.html#OfflineAccess
In any case, the approach makes sense in an OAuth2 context. As a scope, a user needs to grant access to this permission explicitly, and it makes it possible to allow/deny refresh tokens on a client-by-client basis.
Expiration of access tokens should be communicated to clients In issue #946 we're discussing if expiration for access tokens should be optional. Regardless of what becomes of that, if a token has an expiration we should include an expires_in so clients can anticipate whether a refresh token should still be usable.
This is not specified in IETF RFCs afaik but I've seen several approaches. The most sleek one I encountered simple adds a refresh_token_expires_in property in the code exchange response. This is an implementation by a dutch bank.
https://developer.ing.com/api-marketplace/marketplace/2d00fd5f-88cd-4416-bbca-f309ebb03bfe/reference#endpoint__1
Refresh tokens should probably be optional and maybe even not included by default This is somewhat possible through the PR #1000 but a more explicit system and changing the default would probably be a good idea. The PR makes it possible, but more as a hack rather than a feature to improve security.
In conclusion I would propose the following changes; And I am willing to take a stab at making a PR for it, but I wanted input first before I spent too much effort into something that will not be accepted.
- Make an option to configure a scope required for clients to receive a refresh token
- If the above config is set, an auth code grant no longer includes a refresh token by default
- If after running
finalizeScopes()the configured scope is present, include the refresh token - Add
refresh_token_expires_inas a property in the authorization code exchange response
As this changes the behaviour only after a settings is set, it is backwards compatible enough to not need a major release.
I am very interested in thoughts about this.
Source: thephpleague/oauth2-server