88from django .urls import reverse
99
1010import oauth2_provider .models
11- from oauth2_provider .models import get_access_token_model , get_application_model , get_device_model
11+ from oauth2_provider .models import (
12+ get_access_token_model ,
13+ get_application_model ,
14+ get_device_model ,
15+ get_refresh_token_model ,
16+ )
17+ from oauth2_provider .utils import set_oauthlib_user_to_device_request_user
1218
1319from . import presets
1420from .common_testing import OAuth2ProviderTestCase as TestCase
1521
1622
1723Application = get_application_model ()
1824AccessToken = get_access_token_model ()
25+ RefreshToken = get_refresh_token_model ()
1926UserModel = get_user_model ()
2027DeviceModel : oauth2_provider .models .Device = get_device_model ()
2128
@@ -122,6 +129,8 @@ def test_device_flow_authorization_user_code_confirm_and_access_token(self):
122129 # -----------------------
123130 self .oauth2_settings .OAUTH_DEVICE_VERIFICATION_URI = "example.com/device"
124131 self .oauth2_settings .OAUTH_DEVICE_USER_CODE_GENERATOR = lambda : "xyz"
132+ self .oauth2_settings .OAUTH_DEVICE_USER_CODE_GENERATOR = lambda : "xyz"
133+ self .oauth2_settings .OAUTH_PRE_TOKEN_VALIDATION = [set_oauthlib_user_to_device_request_user ]
125134
126135 request_data : dict [str , str ] = {
127136 "client_id" : self .application .client_id ,
@@ -193,6 +202,7 @@ def test_device_flow_authorization_user_code_confirm_and_access_token(self):
193202 "client_id" : self .application .client_id ,
194203 "grant_type" : "urn:ietf:params:oauth:grant-type:device_code" ,
195204 }
205+
196206 token_response = self .client .post (
197207 "/o/token/" ,
198208 data = urlencode (token_payload ),
@@ -207,6 +217,17 @@ def test_device_flow_authorization_user_code_confirm_and_access_token(self):
207217 assert token_data ["token_type" ].lower () == "bearer"
208218 assert "scope" in token_data
209219
220+ # ensure the access token and refresh token have the same user as the device that just authenticated
221+ access_token : oauth2_provider .models .AccessToken = AccessToken .objects .get (
222+ token = token_data ["access_token" ]
223+ )
224+ assert access_token .user == device .user
225+
226+ refresh_token : oauth2_provider .models .RefreshToken = RefreshToken .objects .get (
227+ token = token_data ["refresh_token" ]
228+ )
229+ assert refresh_token .user == device .user
230+
210231 @mock .patch (
211232 "oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token" ,
212233 lambda : "abc" ,
0 commit comments