Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 28, 2025
1 parent 6685b62 commit 9d4ea26
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions oauth2_provider/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass
from datetime import datetime, timedelta
from datetime import timezone as dt_timezone
from typing import Optional, Callable
from typing import Callable, Optional
from urllib.parse import parse_qsl, urlparse

from django.apps import apps
Expand Down Expand Up @@ -734,7 +734,7 @@ class DeviceCodeResponse:
user_code: int
device_code: str
interval: int
verification_uri_complete: Optional[str| Callable] = None
verification_uri_complete: Optional[str | Callable] = None


def create_device(device_request: DeviceRequest, device_response: DeviceCodeResponse) -> Device:
Expand Down
4 changes: 2 additions & 2 deletions oauth2_provider/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from django.utils.module_loading import import_string
from oauthlib.common import Request

from oauth2_provider.utils import user_code_generator, set_oauthlib_user_to_device_request_user
from oauth2_provider.utils import set_oauthlib_user_to_device_request_user, user_code_generator


USER_SETTINGS = getattr(settings, "OAUTH2_PROVIDER", None)
Expand Down Expand Up @@ -281,7 +281,7 @@ def server_kwargs(self):
("verification_uri_complete", "OAUTH_DEVICE_VERIFICATION_URI_COMPLETE"),
("interval", "DEVICE_FLOW_INTERVAL"),
("user_code_generator", "OAUTH_DEVICE_USER_CODE_GENERATOR"),
("pre_token","OAUTH_PRE_TOKEN_VALIDATION")
("pre_token", "OAUTH_PRE_TOKEN_VALIDATION"),
]
}
kwargs.update(self.EXTRA_SERVER_KWARGS)
Expand Down
2 changes: 1 addition & 1 deletion oauth2_provider/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from oauthlib.common import Request



@functools.lru_cache()
def jwk_from_pem(pem_string):
"""
Expand Down Expand Up @@ -96,5 +95,6 @@ def set_oauthlib_user_to_device_request_user(request: Request) -> None:
# Since this function is used in the settings module, it will lead to circular imports
# since django isn't fully initialised yet when settings run
from oauth2_provider.models import Device, get_device_model

device: Device = get_device_model().objects.get(device_code=request._params["device_code"])
request.user = device.user
4 changes: 3 additions & 1 deletion tests/app/idp/idp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import environ

from oauth2_provider.utils import user_code_generator, set_oauthlib_user_to_device_request_user
from oauth2_provider.utils import set_oauthlib_user_to_device_request_user, user_code_generator


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand Down
18 changes: 13 additions & 5 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
from django.urls import reverse

import oauth2_provider.models
from oauth2_provider.models import get_access_token_model, get_application_model, get_device_model, get_refresh_token_model
from oauth2_provider.utils import user_code_generator, set_oauthlib_user_to_device_request_user
from oauth2_provider.models import (
get_access_token_model,
get_application_model,
get_device_model,
get_refresh_token_model,
)
from oauth2_provider.utils import set_oauthlib_user_to_device_request_user

from . import presets
from .common_testing import OAuth2ProviderTestCase as TestCase
Expand Down Expand Up @@ -213,13 +218,16 @@ def test_device_flow_authorization_user_code_confirm_and_access_token(self):
assert "scope" in token_data

# ensure the access token and refresh token have the same user as the device that just authenticated
access_token: oauth2_provider.models.AccessToken = AccessToken.objects.get(token=token_data["access_token"])
access_token: oauth2_provider.models.AccessToken = AccessToken.objects.get(
token=token_data["access_token"]
)
assert access_token.user == device.user

refresh_token: oauth2_provider.models.RefreshToken = RefreshToken.objects.get(token=token_data["refresh_token"])
refresh_token: oauth2_provider.models.RefreshToken = RefreshToken.objects.get(
token=token_data["refresh_token"]
)
assert refresh_token.user == device.user


@mock.patch(
"oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
lambda: "abc",
Expand Down

0 comments on commit 9d4ea26

Please sign in to comment.