Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
cb9fb6ef
Commit
cb9fb6ef
authored
Apr 11, 2011
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring of authentication/permissions
parent
94174259
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
27 deletions
+50
-27
djangorestframework/authenticators.py
+0
-20
djangorestframework/request.py
+40
-1
djangorestframework/resource.py
+7
-3
djangorestframework/response.py
+3
-3
No files found.
djangorestframework/authenticators.py
View file @
cb9fb6ef
...
@@ -10,26 +10,6 @@ from djangorestframework.utils import as_tuple
...
@@ -10,26 +10,6 @@ from djangorestframework.utils import as_tuple
import
base64
import
base64
class
AuthenticatorMixin
(
object
):
"""Adds pluggable authentication behaviour."""
"""The set of authenticators to use."""
authenticators
=
None
def
authenticate
(
self
,
request
):
"""Attempt to authenticate the request, returning an authentication context or None.
An authentication context may be any object, although in many cases it will simply be a :class:`User` instance."""
# Attempt authentication against each authenticator in turn,
# and return None if no authenticators succeed in authenticating the request.
for
authenticator
in
as_tuple
(
self
.
authenticators
):
auth_context
=
authenticator
(
self
)
.
authenticate
(
request
)
if
auth_context
:
return
auth_context
return
None
class
BaseAuthenticator
(
object
):
class
BaseAuthenticator
(
object
):
"""All authenticators should extend BaseAuthenticator."""
"""All authenticators should extend BaseAuthenticator."""
...
...
djangorestframework/request.py
View file @
cb9fb6ef
...
@@ -9,7 +9,7 @@ from django.http.multipartparser import LimitBytes
...
@@ -9,7 +9,7 @@ from django.http.multipartparser import LimitBytes
from
StringIO
import
StringIO
from
StringIO
import
StringIO
class
RequestMixin
(
object
):
class
RequestMixin
(
object
):
"""Mixin
behaviour to deal with requests
."""
"""Mixin
class to provide request parsing behaviour
."""
USE_FORM_OVERLOADING
=
True
USE_FORM_OVERLOADING
=
True
METHOD_PARAM
=
"_method"
METHOD_PARAM
=
"_method"
...
@@ -214,3 +214,42 @@ class RequestMixin(object):
...
@@ -214,3 +214,42 @@ class RequestMixin(object):
class
AuthMixin
(
object
):
"""Mixin class to provide authentication and permissions."""
authenticators
=
()
permitters
=
()
@property
def
auth
(
self
):
if
not
hasattr
(
self
,
'_auth'
):
self
.
_auth
=
self
.
_authenticate
()
return
self
.
_auth
# TODO?
#@property
#def user(self):
# if not has_attr(self, '_user'):
# auth = self.auth
# if isinstance(auth, User...):
# self._user = auth
# else:
# self._user = getattr(auth, 'user', None)
# return self._user
def
check_permissions
(
self
):
if
not
self
.
permissions
:
return
auth
=
self
.
auth
for
permitter_cls
in
self
.
permitters
:
permitter
=
permission_cls
(
self
)
permitter
.
permit
(
auth
)
def
_authenticate
(
self
):
for
authenticator_cls
in
self
.
authenticators
:
authenticator
=
authenticator_cls
(
self
)
auth
=
authenticator
.
authenticate
(
self
.
request
)
if
auth
:
return
auth
return
None
djangorestframework/resource.py
View file @
cb9fb6ef
...
@@ -6,7 +6,7 @@ from djangorestframework.emitters import EmitterMixin
...
@@ -6,7 +6,7 @@ from djangorestframework.emitters import EmitterMixin
from
djangorestframework.authenticators
import
AuthenticatorMixin
from
djangorestframework.authenticators
import
AuthenticatorMixin
from
djangorestframework.validators
import
FormValidatorMixin
from
djangorestframework.validators
import
FormValidatorMixin
from
djangorestframework.response
import
Response
,
ResponseException
from
djangorestframework.response
import
Response
,
ResponseException
from
djangorestframework.request
import
RequestMixin
from
djangorestframework.request
import
RequestMixin
,
AuthMixin
from
djangorestframework
import
emitters
,
parsers
,
authenticators
,
status
from
djangorestframework
import
emitters
,
parsers
,
authenticators
,
status
...
@@ -18,7 +18,7 @@ from djangorestframework import emitters, parsers, authenticators, status
...
@@ -18,7 +18,7 @@ from djangorestframework import emitters, parsers, authenticators, status
__all__
=
[
'Resource'
]
__all__
=
[
'Resource'
]
class
Resource
(
EmitterMixin
,
Auth
enticator
Mixin
,
FormValidatorMixin
,
RequestMixin
,
View
):
class
Resource
(
EmitterMixin
,
AuthMixin
,
FormValidatorMixin
,
RequestMixin
,
View
):
"""Handles incoming requests and maps them to REST operations,
"""Handles incoming requests and maps them to REST operations,
performing authentication, input deserialization, input validation, output serialization."""
performing authentication, input deserialization, input validation, output serialization."""
...
@@ -139,7 +139,7 @@ class Resource(EmitterMixin, AuthenticatorMixin, FormValidatorMixin, RequestMixi
...
@@ -139,7 +139,7 @@ class Resource(EmitterMixin, AuthenticatorMixin, FormValidatorMixin, RequestMixi
# Typically the context will be a user, or None if this is an anonymous request,
# Typically the context will be a user, or None if this is an anonymous request,
# but it could potentially be more complex (eg the context of a request key which
# but it could potentially be more complex (eg the context of a request key which
# has been signed against a particular set of permissions)
# has been signed against a particular set of permissions)
auth_context
=
self
.
auth
enticate
(
request
)
auth_context
=
self
.
auth
# If using a form POST with '_method'/'_content'/'_content_type' overrides, then alter
# If using a form POST with '_method'/'_content'/'_content_type' overrides, then alter
# self.method, self.content_type, self.CONTENT appropriately.
# self.method, self.content_type, self.CONTENT appropriately.
...
@@ -174,6 +174,10 @@ class Resource(EmitterMixin, AuthenticatorMixin, FormValidatorMixin, RequestMixi
...
@@ -174,6 +174,10 @@ class Resource(EmitterMixin, AuthenticatorMixin, FormValidatorMixin, RequestMixi
except
ResponseException
,
exc
:
except
ResponseException
,
exc
:
response
=
exc
.
response
response
=
exc
.
response
except
:
import
traceback
traceback
.
print_exc
()
# Always add these headers.
# Always add these headers.
#
#
# TODO - this isn't actually the correct way to set the vary header,
# TODO - this isn't actually the correct way to set the vary header,
...
...
djangorestframework/response.py
View file @
cb9fb6ef
...
@@ -8,7 +8,7 @@ class NoContent(object):
...
@@ -8,7 +8,7 @@ class NoContent(object):
"""Used to indicate no body in http response.
"""Used to indicate no body in http response.
(We cannot just use None, as that is a valid, serializable response object.)
(We cannot just use None, as that is a valid, serializable response object.)
TODO: On re
lflection I'm going to get rid of this and just not support serai
lized 'None' responses.
TODO: On re
flection I'm going to get rid of this and just not support seria
lized 'None' responses.
"""
"""
pass
pass
...
@@ -23,8 +23,8 @@ class Response(object):
...
@@ -23,8 +23,8 @@ class Response(object):
@property
@property
def
status_text
(
self
):
def
status_text
(
self
):
"""Return reason text corr
o
sponding to our HTTP response status code.
"""Return reason text corr
e
sponding to our HTTP response status code.
Provided for conv
i
enience."""
Provided for convenience."""
return
STATUS_CODE_TEXT
.
get
(
self
.
status
,
''
)
return
STATUS_CODE_TEXT
.
get
(
self
.
status
,
''
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment