Skip to content

Conversation

@amanning9
Copy link
Contributor

Description of the Change

The new device code grant does not give the resulting token the scopes that were asked for. This PR fixes that bug and adds a test.

I'm not /completely/ sure that I've fixed it in the correct place- If There is a better way or place to fix the problem please let me know.

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name in AUTHORS
  • tests/app/idp updated to demonstrate new features
  • tests/app/rp updated to demonstrate new features

@codecov
Copy link

codecov bot commented Nov 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@dopry
Copy link
Member

dopry commented Nov 14, 2025

@amanning9 this generally looks right... to help make yourself more confident and assist me in validating could you help me understand how we are using set_oauthlib_user_to_device_request_user upstream? Do all the contexts in which is it used make sense to also set scope? Maybe we should rename it if we're setting both user and scope? Should request.scope even be set or should developer be loading the device grant or token to lookup the scope?

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug in the device code grant flow where the issued access token was not respecting the scopes requested during device authorization. Instead, tokens were being issued with the DEFAULT_SCOPES regardless of what was requested.

Changes:

  • Added logic to set the request scopes from the device grant's stored scope in the set_oauthlib_user_to_device_request_user function
  • Added a comprehensive test to verify that requested scopes are used instead of default scopes

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
oauth2_provider/utils.py Added scope handling to the set_oauthlib_user_to_device_request_user function to set request.scopes from the device grant's scope field
tests/test_device.py Added test test_device_flow_uses_requested_scope_not_default to verify that specific requested scopes are used in the issued token rather than default scopes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


device: DeviceGrant = get_device_grant_model().objects.get(device_code=request._params["device_code"])
request.user = device.user
request.scopes = device.scope.split() if device.scope else []
Copy link

Copilot AI Jan 10, 2026

Choose a reason for hiding this comment

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

The scope splitting should be consistent with other grant types. In oauth2_validators.py line 491, the authorization code grant uses split(" ") with an explicit space delimiter. Consider using device.scope.split(" ") instead of device.scope.split() for consistency, though both will work for space-separated scopes.

Suggested change
request.scopes = device.scope.split() if device.scope else []
request.scopes = device.scope.split(" ") if device.scope else []

Copilot uses AI. Check for mistakes.
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.

2 participants