Skip to content

Commit 6ad6d78

Browse files
cristiprgdopry
authored andcommitted
Use term DeviceGrant over Device
Perferred to use the Grant suffix in order to keep the naming consistent with other grant models.
1 parent 9691097 commit 6ad6d78

File tree

7 files changed

+33
-27
lines changed

7 files changed

+33
-27
lines changed

oauth2_provider/migrations/0013_alter_application_authorization_grant_type_device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Migration(migrations.Migration):
1919
field=models.CharField(choices=[('authorization-code', 'Authorization code'), ('urn:ietf:params:oauth:grant-type:device_code', 'Device Code'), ('implicit', 'Implicit'), ('password', 'Resource owner password-based'), ('client-credentials', 'Client credentials'), ('openid-hybrid', 'OpenID connect hybrid')], max_length=44),
2020
),
2121
migrations.CreateModel(
22-
name='Device',
22+
name='DeviceGrant',
2323
fields=[
2424
('id', models.BigAutoField(primary_key=True, serialize=False)),
2525
('device_code', models.CharField(max_length=100, unique=True)),
@@ -34,8 +34,8 @@ class Migration(migrations.Migration):
3434
],
3535
options={
3636
'abstract': False,
37-
'swappable': 'OAUTH2_PROVIDER_DEVICE_MODEL',
38-
'constraints': [models.UniqueConstraint(fields=('device_code',), name='oauth2_provider_device_unique_device_code')],
37+
'swappable': 'OAUTH2_PROVIDER_DEVICE_GRANT_MODEL',
38+
'constraints': [models.UniqueConstraint(fields=('device_code',), name='oauth2_provider_devicegrant_unique_device_code')],
3939
},
4040
),
4141
]

oauth2_provider/models.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ class Meta(AbstractIDToken.Meta):
655655
swappable = "OAUTH2_PROVIDER_ID_TOKEN_MODEL"
656656

657657

658-
class AbstractDevice(models.Model):
658+
class AbstractDeviceGrant(models.Model):
659659
class Meta:
660660
abstract = True
661661
constraints = [
@@ -718,11 +718,11 @@ def get_by_natural_key(self, client_id, device_code, user_code):
718718
return self.get(client_id=client_id, device_code=device_code, user_code=user_code)
719719

720720

721-
class Device(AbstractDevice):
721+
class DeviceGrant(AbstractDeviceGrant):
722722
objects = DeviceManager()
723723

724-
class Meta(AbstractDevice.Meta):
725-
swappable = "OAUTH2_PROVIDER_DEVICE_MODEL"
724+
class Meta(AbstractDeviceGrant.Meta):
725+
swappable = "OAUTH2_PROVIDER_DEVICE_GRANT_MODEL"
726726

727727
def natural_key(self):
728728
return (self.client_id, self.device_code, self.user_code)
@@ -746,10 +746,10 @@ class DeviceCodeResponse:
746746
verification_uri_complete: Optional[Union[str, Callable]] = None
747747

748748

749-
def create_device(device_request: DeviceRequest, device_response: DeviceCodeResponse) -> Device:
749+
def create_device_grant(device_request: DeviceRequest, device_response: DeviceCodeResponse) -> DeviceGrant:
750750
now = datetime.now(tz=dt_timezone.utc)
751751

752-
return Device.objects.create(
752+
return DeviceGrant.objects.create(
753753
client_id=device_request.client_id,
754754
device_code=device_response.device_code,
755755
user_code=device_response.user_code,
@@ -763,9 +763,9 @@ def get_application_model():
763763
return apps.get_model(oauth2_settings.APPLICATION_MODEL)
764764

765765

766-
def get_device_model():
767-
"""Return the Device model that is active in this project."""
768-
return apps.get_model(oauth2_settings.DEVICE_MODEL)
766+
def get_device_grant_model():
767+
"""Return the DeviceGrant model that is active in this project."""
768+
return apps.get_model(oauth2_settings.DEVICE_GRANT_MODEL)
769769

770770

771771
def get_grant_model():

oauth2_provider/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
USER_SETTINGS = getattr(settings, "OAUTH2_PROVIDER", None)
3131

3232
APPLICATION_MODEL = getattr(settings, "OAUTH2_PROVIDER_APPLICATION_MODEL", "oauth2_provider.Application")
33-
DEVICE_MODEL = getattr(settings, "OAUTH2_PROVIDER_DEVICE_MODEL", "oauth2_provider.Device")
33+
DEVICE_GRANT_MODEL = getattr(settings, "OAUTH2_PROVIDER_DEVICE_GRANT_MODEL", "oauth2_provider.DeviceGrant")
3434
ACCESS_TOKEN_MODEL = getattr(settings, "OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL", "oauth2_provider.AccessToken")
3535
ID_TOKEN_MODEL = getattr(settings, "OAUTH2_PROVIDER_ID_TOKEN_MODEL", "oauth2_provider.IDToken")
3636
GRANT_MODEL = getattr(settings, "OAUTH2_PROVIDER_GRANT_MODEL", "oauth2_provider.Grant")
@@ -68,7 +68,7 @@
6868
"APPLICATION_MODEL": APPLICATION_MODEL,
6969
"ACCESS_TOKEN_MODEL": ACCESS_TOKEN_MODEL,
7070
"ID_TOKEN_MODEL": ID_TOKEN_MODEL,
71-
"DEVICE_MODEL": DEVICE_MODEL,
71+
"DEVICE_GRANT_MODEL": DEVICE_GRANT_MODEL,
7272
"DEVICE_FLOW_INTERVAL": 5,
7373
"GRANT_MODEL": GRANT_MODEL,
7474
"REFRESH_TOKEN_MODEL": REFRESH_TOKEN_MODEL,

oauth2_provider/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def set_oauthlib_user_to_device_request_user(request: Request) -> None:
9494
"""
9595
# Since this function is used in the settings module, it will lead to circular imports
9696
# since django isn't fully initialised yet when settings run
97-
from oauth2_provider.models import Device, get_device_model
97+
from oauth2_provider.models import DeviceGrant, get_device_grant_model
9898

99-
device: Device = get_device_model().objects.get(device_code=request._params["device_code"])
99+
device: DeviceGrant = get_device_grant_model().objects.get(device_code=request._params["device_code"])
100100
request.user = device.user

oauth2_provider/views/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from django.views.generic import FormView, View
1616
from oauthlib.oauth2.rfc8628 import errors as rfc8628_errors
1717

18-
from oauth2_provider.models import Device
18+
from oauth2_provider.models import DeviceGrant
1919

2020
from ..compat import login_not_required
2121
from ..exceptions import OAuthToolkitError
@@ -318,8 +318,8 @@ def device_flow_token_response(
318318
self, request: http.HttpRequest, device_code: str, *args, **kwargs
319319
) -> http.HttpResponse:
320320
try:
321-
device = Device.objects.get(device_code=device_code)
322-
except Device.DoesNotExist:
321+
device = DeviceGrant.objects.get(device_code=device_code)
322+
except DeviceGrant.DoesNotExist:
323323
# The RFC does not mention what to return when the device is not found,
324324
# but to keep it consistent with the other errors, we return the error
325325
# in json format with an "error" key and the value formatted in the same

oauth2_provider/views/device.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
from oauthlib.oauth2 import DeviceApplicationServer
1212

1313
from oauth2_provider.compat import login_not_required
14-
from oauth2_provider.models import Device, DeviceCodeResponse, DeviceRequest, create_device, get_device_model
14+
from oauth2_provider.models import (
15+
DeviceCodeResponse,
16+
DeviceGrant,
17+
DeviceRequest,
18+
create_device_grant,
19+
get_device_grant_model,
20+
)
1521
from oauth2_provider.views.mixins import OAuthLibMixin
1622

1723

@@ -29,7 +35,7 @@ def post(self, request, *args, **kwargs):
2935
return http.JsonResponse(data=json.loads(response), status=status, headers=headers)
3036

3137
device_response = DeviceCodeResponse(**response)
32-
create_device(device_request, device_response)
38+
create_device_grant(device_request, device_response)
3339

3440
return http.JsonResponse(data=response, status=status, headers=headers)
3541

@@ -61,8 +67,8 @@ def device_user_code_view(request):
6167

6268
user_code: str = form.cleaned_data["user_code"]
6369
try:
64-
device: Device = get_device_model().objects.get(user_code=user_code)
65-
except Device.DoesNotExist:
70+
device: DeviceGrant = get_device_grant_model().objects.get(user_code=user_code)
71+
except DeviceGrant.DoesNotExist:
6672
form.add_error("user_code", "Incorrect user code")
6773
return render(request, "oauth2_provider/device/user_code.html", {"form": form}, status=404)
6874

@@ -91,11 +97,11 @@ def device_user_code_view(request):
9197
@login_required
9298
def device_confirm_view(request: http.HttpRequest, client_id: str, user_code: str):
9399
try:
94-
device: Device = get_device_model().objects.get(
100+
device: DeviceGrant = get_device_grant_model().objects.get(
95101
# there is a db index on client_id
96102
Q(client_id=client_id) & Q(user_code=user_code)
97103
)
98-
except Device.DoesNotExist:
104+
except DeviceGrant.DoesNotExist:
99105
return http.HttpResponseNotFound("<h1>Device not found</h1>")
100106

101107
if device.status != device.AUTHORIZATION_PENDING:

tests/test_device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from oauth2_provider.models import (
1414
get_access_token_model,
1515
get_application_model,
16-
get_device_model,
16+
get_device_grant_model,
1717
get_refresh_token_model,
1818
)
1919
from oauth2_provider.utils import set_oauthlib_user_to_device_request_user
@@ -26,7 +26,7 @@
2626
AccessToken = get_access_token_model()
2727
RefreshToken = get_refresh_token_model()
2828
UserModel = get_user_model()
29-
DeviceModel: oauth2_provider.models.Device = get_device_model()
29+
DeviceModel: oauth2_provider.models.DeviceGrant = get_device_grant_model()
3030

3131

3232
@pytest.mark.usefixtures("oauth2_settings")

0 commit comments

Comments
 (0)