hiidef / oauth2app

Django OAuth 2.0 Server App. Please fork and improve!
https://github.com/hiidef/oauth2app
MIT License
271 stars 115 forks source link

Do not read from request body unless we have to #35

Open pilt opened 12 years ago

pilt commented 12 years ago

HttpRequest's read() method is called when we do self.request.REQUEST.get('bearer_token'). This makes it impossible to access the request body at a later point.

With this change we avoid trying to read a bearer token from the request body if the Authorization header is set.

In django.http.HttpRequest:

def read(self, *args, **kwargs):
    self._read_started = True
    return self._stream.read(*args, **kwargs)

@property
def body(self):
    if not hasattr(self, '_body'):
        if self._read_started:
            raise Exception("You cannot access body after reading from request's data stream")
        try:
            self._body = self.read()
        except IOError, e:
            raise UnreadablePostError, e, sys.exc_traceback
        self._stream = StringIO(self._body)
    return self._body