diff options
| author | Tom Christie | 2013-01-28 07:36:57 +0000 |
|---|---|---|
| committer | Tom Christie | 2013-01-28 07:36:57 +0000 |
| commit | e649f2ec61a51beddf56132b3f9fa0a3d4fe086b (patch) | |
| tree | 70b182d92bab3ced7e565f78aeed6250902aa0dc /docs/api-guide/authentication.md | |
| parent | cb219fa04f6a4d4ae0d99920380416f62126b87d (diff) | |
| download | django-rest-framework-e649f2ec61a51beddf56132b3f9fa0a3d4fe086b.tar.bz2 | |
Example custom authentication. Fixes #301.
Diffstat (limited to 'docs/api-guide/authentication.md')
| -rw-r--r-- | docs/api-guide/authentication.md | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/docs/api-guide/authentication.md b/docs/api-guide/authentication.md index ac690bdc..1795cfaf 100644 --- a/docs/api-guide/authentication.md +++ b/docs/api-guide/authentication.md @@ -194,6 +194,24 @@ You *may* also override the `.authentication_header(self, request)` method. If If the `.authentication_header()` method is not overridden, the authentication scheme will return `HTTP 403 Forbidden` responses when an unauthenticated request is denied access. +## Example + +The following example will authenticate any incoming request as the user given by the username in a custom request header named 'X_USERNAME'. + + class ExampleAuthentication(authentication.BaseAuthentication): + def has_permission(self, request, view, obj=None): + username = request.META.get('X_USERNAME') + if not username: + return None + + try: + user = User.objects.get(username=username) + except User.DoesNotExist: + raise authenticate.AuthenticationFailed('No such user') + + return (user, None) + + [cite]: http://jacobian.org/writing/rest-worst-practices/ [http401]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 [http403]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4 |
