Repository navigation
Description
Describe the feature
DeviceCodeGrant.validate_token_request has no RequestValidator hook for loading the stored device authorization grant — specifically, no place to populate request.user and request.scopes from the persisted grant record before token issuance. The Authorization Code Grant solves this with validate_code(), which is explicitly documented to set both fields. DeviceCodeGrant has no equivalent, leaving implementors with no clean extension point.
The consequence is a scope bug. RFC 8628 §3.4 specifies that the device code token request body contains only device_code, client_id, and grant_type there is no scope parameter, because the scope was already negotiated during the device authorization step. This means request.scope is None when the device polls for a token. Because nothing has populated request.scopes before validate_scopes(request) is called in DeviceCodeGrant.validate_token_request, execution falls all the way through to the default-scope fallback in GrantTypeBase.validate_scopes.
request.scope is None --> scope_to_list(None) is falsy --> get_default_scopes wins. Issued tokens silently receive the server's configured default scopes instead of the scopes the user actually authorized, violating RFC 8628 §3.5.
The fix is to add validate_device_authorization_code() to RequestValidator and call it from DeviceCodeGrant.validate_token_request, mirroring exactly what the Authorization Code Grant does with validate_code():
Additional context
Applies to: OAuth2 server-side code, specifically RFC 8628 (Device Authorization Grant).
Does not affect: OAuth1, OIDC directly (though OIDC device flows would benefit transitively).
This issue was discovered while reviewing django-oauth/django-oauth-toolkit#1621, where the workaround is to inject scope-loading logic via custom_validators.pre_token — a hook intended for arbitrary extensibility, not core grant validation. That workaround also requires accessing request._params directly (a private attribute) to retrieve the device_code value, since there is no clean API for it. A first-class validate_device_authorization_code() hook would let downstream libraries eliminate the workaround entirely and provide implementors a self-documenting contract, consistent with every other grant type.
Proposed Change
- Add validate_device_authorization_code() to [RequestValidator]
- Call it in [DeviceCodeGrant.validate_token_request()]
# oauthlib/oauth2/rfc8628/grant_types/device_code.py
self.validate_grant_type(request)
request.client_id = request.client_id or request.client.client_id
# NEW: mirrors validate_code() in auth code flow
if not self.request_validator.validate_device_authorization_code(
request.client_id, request.device_code, request.client, request
):
raise rfc6749_errors.InvalidGrantError(request=request)
# Now request.user and request.scopes are populated from the stored grant
for attr in ('user', 'scopes'):
if getattr(request, attr, None) is None:
log.debug('request.%s was not set on device_code validation.', attr)
self.validate_scopes(request)
Happy to proceed with implementing this as a PR is the maintainers feel the approach is correct. Alternatively open to other direction.
Activity
Ignore client-supplied scope on RFC 8628 device token requests