Tweak throttling/permissions/auth docs

This commit is contained in:
Tom Christie 2012-09-13 09:39:16 +01:00
parent 003a65f0e0
commit b16c45aa6d
6 changed files with 67 additions and 39 deletions

View File

@ -1,34 +0,0 @@
import uuid
import hmac
from hashlib import sha1
from django.db import models
class TokenManager(models.Manager):
"""
Manager class to provide `Token.objects.create_token(user=user)`.
"""
def create_token(self, user):
token = Token(user=user)
token.save()
return token
class Token(models.Model):
"""
The default authorization token model.
"""
key = models.CharField(max_length=40, primary_key=True)
user = models.ForeignKey('auth.User')
revoked = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
objects = TokenManager()
def save(self, *args, **kwargs):
if not self.key:
self.key = self.generate_key()
return super(Token, self).save(*args, **kwargs)
def generate_key(self):
unique = str(uuid.uuid4())
return hmac.new(unique, digestmod=sha1).hexdigest()

View File

@ -18,7 +18,7 @@ The `request.auth` property is used for any additional authentication informatio
## How authentication is determined
Authentication is always set as a list of classes. REST framework will attempt to authenticate with each class in the list, and will set `request.user` and `request.auth` using the return value of the first class that successfully authenticates.
The authentication policy is always defined as a list of classes. REST framework will attempt to authenticate with each class in the list, and will set `request.user` and `request.auth` using the return value of the first class that successfully authenticates.
If no class authenticates, `request.user` will be set to an instance of `django.contrib.auth.models.AnonymousUser`, and `request.auth` will be set to `None`.

View File

@ -12,8 +12,9 @@ Permission checks are always run at the very start of the view, before any other
## How permissions are determined
Permissions in REST framework are always defined as a list of permission classes. Before running the main body of the view each permission in the list is checked.
Permissions in REST framework are always defined as a list of permission classes.
Before running the main body of the view each permission in the list is checked.
If any permission check fails an `exceptions.PermissionDenied` exception will be raised, and the main body of the view will not run.
## Object level permissions

View File

@ -8,8 +8,69 @@
[cite]: https://dev.twitter.com/docs/error-codes-responses
## PerUserThrottle
Throttling is similar to [permissions], in that it determines if a request should be authorized. Throttles indicate a temporary state, and are used to control the rate of requests that clients can make to an API.
## PerViewThrottle
As with permissions, multiple throttles may be used. Your API might have a restrictive throttle for unauthenticated requests, and a less restrictive throttle for authenticated requests.
## Custom throttles
Another scenario where you might want to use multiple throttles would be if you need to impose different constraints on different parts of the API, due ato some services being particularly resource-intensive.
Throttles do not necessarily only refer to rate-limiting requests. For example a storage service might also need to throttle against bandwidth.
## How throttling is determined
As with permissions and authentication, throttling in REST framework is always defined as a list of classes.
Before running the main body of the view each throttle in the list is checked.
If any throttle check fails an `exceptions.Throttled` exception will be raised, and the main body of the view will not run.
## Setting the throttling policy
The default throttling policy may be set globally, using the `DEFAULT_THROTTLES` setting. For example.
API_SETTINGS = {
'DEFAULT_THROTTLES': (
'djangorestframework.throttles.AnonThrottle',
'djangorestframework.throttles.UserThrottle',
)
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
You can also set the throttling policy on a per-view basis, using the `APIView` class based views.
class ExampleView(APIView):
throttle_classes = (UserThrottle,)
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
Or, if you're using the `@api_view` decorator with function based views.
@api_view('GET')
@throttle_classes(UserThrottle)
def example_view(request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
## AnonThrottle
The `AnonThrottle` will only ever throttle unauthenticated users. The IP address of the incoming request is used to identify
`AnonThrottle` is suitable if you want to restrict the rate of requests from unknown sources.
## UserThrottle
`UserThrottle` is suitable if you want a simple restriction
## ScopedThrottle
## Custom throttles
[permissions]: permissions.md