Skip to content

Conversation

@pwnage101
Copy link

IDAs which enable this middleware should still be able to use DRF permission composition. This PR reinforces the stated promise that this middleware is a no-op for any ViewSet that does not actually use LoginRedirectIfUnauthenticated.

…fUnauthenticatedMiddleware

IDAs which enable this middleware should still be able to use DRF
permission composition. This PR reinforces the stated promise that this
middleware is a no-op for any ViewSet that does not actually use
LoginRedirectIfUnauthenticated.
@pwnage101 pwnage101 force-pushed the pwnage101/redirect-to-login-middleware-supports-perm-composition branch from 9980c6d to 38870ea Compare July 14, 2025 21:42
@pwnage101 pwnage101 requested review from awais786 and Copilot July 14, 2025 21:45
Copy link

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 enhances DRF permission composition support within JWT-related middleware, ensuring composite permissions are correctly handled and the JWT authentication middleware no-ops when login-redirection isn’t used.

  • Extracted _iter_included_base_classes to the module level for reuse
  • Updated both EnsureJWTAuthSettingsMiddleware and login-redirect middleware to leverage composed-permission detection
  • Bumped version to 10.6.1 and added a corresponding changelog entry

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
edx_rest_framework_extensions/auth/jwt/middleware.py Extracted helper and updated permission-check calls for composition
edx_rest_framework_extensions/init.py Updated __version__ to 10.6.1
CHANGELOG.rst Added [10.6.1] entry describing the permission composition fix
Comments suppressed due to low confidence (2)

edx_rest_framework_extensions/auth/jwt/middleware.py:38

  • Consider adding unit tests for _iter_included_base_classes, covering both simple and nested composed permissions to verify it yields all expected base classes.
def _iter_included_base_classes(view_permissions):

CHANGELOG.rst:17

  • [nitpick] The changelog mentions JwtRedirectToLoginIfUnauthenticatedMiddleware but the code changes affect EnsureJWTAuthSettingsMiddleware and the login-redirect middleware. Update the entry to accurately reflect both class names or the actual class being modified.
* fix: Expand DRF permission composition support to JwtRedirectToLoginIfUnauthenticatedMiddleware.

@pwnage101
Copy link
Author

@awais786 would you consider reviewing?

@pwnage101
Copy link
Author

hm, who can review and has write access?

Copy link
Contributor

@robrap robrap left a comment

Choose a reason for hiding this comment

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

Can we get a unit test that requires your fix to pass?

view_class = _get_view_class(view_func)
view_permission_classes = getattr(view_class, 'permission_classes', tuple())
is_login_required_found = _includes_base_class(view_permission_classes, LoginRedirectIfUnauthenticated)
view_permissions = getattr(view_class, 'permission_classes', tuple())
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Not really introduced by you, but above we have:

view_permissions = list(getattr(view_class, 'permission_classes', []))

Is there a reason for the inconsistency?

Copy link
Author

Choose a reason for hiding this comment

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

It looks like an older version of this code used to iterate over the permission_classes using list.pop():

https://github.com/pwnage101/edx-drf-extensions/blob/1d470a29a16bcaf5c9247c8236def8a4be56b077/edx_rest_framework_extensions/auth/jwt/middleware.py#L56-L57

The recursive helper function _iter_included_base_classes appears to support any iterable, so it shouldn't matter whether a list or tuple is passed. That said, I'm happy to make it consistently a list.

@pwnage101
Copy link
Author

pwnage101 commented Aug 25, 2025

I'm going to pause work on this PR as we've found a workaround by setting permission_classes = [] to block this middleware from executing, then just returning the intended list of classes by overriding self.get_permissions(): https://github.com/openedx/enterprise-access/blob/1052e7eb9e2bd59e4aec852d9c040ed859ea6078/enterprise_access/apps/api/v1/views/bffs/checkout.py#L47

@robrap
Copy link
Contributor

robrap commented Aug 27, 2025

@pwnage101: Would it make sense to create an issue in this repo describing the bug and pointing to this PR as a potential fix? Would you mind taking care of that? It could even get a "good first issue" label, since you handled much of it already.

@pwnage101
Copy link
Author

done: #533

@robrap
Copy link
Contributor

robrap commented Aug 30, 2025

Thank you.

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.

4 participants