aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/request.py
diff options
context:
space:
mode:
authorTom Christie2012-04-11 17:38:47 +0100
committerTom Christie2012-04-11 17:38:47 +0100
commit4739e1c012526c3ed9835d908d6d7eff5e3c48f6 (patch)
tree800f2133bf50449e2698f562d5d1d2dd199168e3 /djangorestframework/request.py
parent44df8345f3ffcba141ded3a1bd993971d7199164 (diff)
parent1ff741d1ccc38f099a7159bdef787e5c04dc4f79 (diff)
downloaddjango-rest-framework-4739e1c012526c3ed9835d908d6d7eff5e3c48f6.tar.bz2
Merge work from sebpiq
Diffstat (limited to 'djangorestframework/request.py')
-rw-r--r--djangorestframework/request.py36
1 files changed, 33 insertions, 3 deletions
diff --git a/djangorestframework/request.py b/djangorestframework/request.py
index a6c23fb8..82aed1e0 100644
--- a/djangorestframework/request.py
+++ b/djangorestframework/request.py
@@ -9,12 +9,13 @@ The wrapped request then offers a richer API, in particular :
- full support of PUT method, including support for file uploads
- form overloading of HTTP method, content type and content
"""
+from StringIO import StringIO
+
+from django.contrib.auth.models import AnonymousUser
from djangorestframework import status
from djangorestframework.utils.mediatypes import is_form_media_type
-from StringIO import StringIO
-
__all__ = ('Request',)
@@ -34,6 +35,7 @@ class Request(object):
Kwargs:
- request(HttpRequest). The original request instance.
- parsers(list/tuple). The parsers to use for parsing the request content.
+ - authentications(list/tuple). The authentications used to try authenticating the request's user.
"""
_USE_FORM_OVERLOADING = True
@@ -41,9 +43,10 @@ class Request(object):
_CONTENTTYPE_PARAM = '_content_type'
_CONTENT_PARAM = '_content'
- def __init__(self, request=None, parsers=None):
+ def __init__(self, request=None, parsers=None, authentication=None):
self._request = request
self.parsers = parsers or ()
+ self.authentication = authentication or ()
self._data = Empty
self._files = Empty
self._method = Empty
@@ -56,6 +59,12 @@ class Request(object):
"""
return [parser() for parser in self.parsers]
+ def get_authentications(self):
+ """
+ Instantiates and returns the list of parsers the request will use.
+ """
+ return [authentication() for authentication in self.authentication]
+
@property
def method(self):
"""
@@ -113,6 +122,16 @@ class Request(object):
self._load_data_and_files()
return self._files
+ @property
+ def user(self):
+ """
+ Returns the :obj:`user` for the current request, authenticated
+ with the set of :class:`authentication` instances applied to the :class:`Request`.
+ """
+ if not hasattr(self, '_user'):
+ self._user = self._authenticate()
+ return self._user
+
def _load_data_and_files(self):
"""
Parses the request content into self.DATA and self.FILES.
@@ -205,6 +224,17 @@ class Request(object):
},
status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
+ def _authenticate(self):
+ """
+ Attempt to authenticate the request using each authentication instance in turn.
+ Returns a ``User`` object, which may be ``AnonymousUser``.
+ """
+ for authentication in self.get_authentications():
+ user = authentication.authenticate(self)
+ if user:
+ return user
+ return AnonymousUser()
+
def __getattr__(self, name):
"""
Proxy other attributes to the underlying HttpRequest object.