-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add SessionCSRFAuthentication
- Loading branch information
Showing
3 changed files
with
64 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework.authentication import SessionAuthentication | ||
|
||
|
||
class SessionCSRFAuthentication(SessionAuthentication): | ||
|
||
def authenticate(self, request): | ||
user = getattr(request._request, 'user', None) # noqa: E501 pylint: disable=protected-access | ||
self.enforce_csrf(request) | ||
return (user, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import contextlib | ||
|
||
from django.test.utils import override_settings | ||
from rest_framework.settings import api_settings | ||
from rest_framework.views import APIView | ||
|
||
|
||
@contextlib.contextmanager | ||
def override_rest_framework_settings_dict(settings_dict): | ||
try: | ||
with override_settings(REST_FRAMEWORK=settings_dict): | ||
_update_api_view_class_attrs() | ||
yield | ||
finally: | ||
_update_api_view_class_attrs() | ||
|
||
|
||
def _update_api_view_class_attrs(): | ||
attrs = [ | ||
'renderer_classes', 'parser_classes', 'authentication_classes', | ||
'throttle_classes', 'permission_classes', 'content_negotiation_class', | ||
'metadata_class', 'versioning_class', | ||
] | ||
for attr in attrs: | ||
attr_upper = attr.upper() | ||
api_settings_key = 'DEFAULT_{attr_upper}'.format(attr_upper=attr_upper) | ||
setattr(APIView, attr, getattr(api_settings, api_settings_key)) |