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
f3e65eab
Commit
f3e65eab
authored
Sep 07, 2012
by
Mjumbe Wawatu Poe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a TokenAuthentication class in a sub-application
parent
72bdd0fc
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
95 additions
and
0 deletions
+95
-0
djangorestframework/runtests/settings.py
+1
-0
djangorestframework/tests/authentication.py
+42
-0
djangorestframework/tokenauth/__init__.py
+0
-0
djangorestframework/tokenauth/authentication.py
+33
-0
djangorestframework/tokenauth/models.py
+19
-0
No files found.
djangorestframework/runtests/settings.py
View file @
f3e65eab
...
...
@@ -90,6 +90,7 @@ INSTALLED_APPS = (
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'djangorestframework'
,
'djangorestframework.tokenauth'
,
)
STATIC_URL
=
'/static/'
...
...
djangorestframework/tests/authentication.py
View file @
f3e65eab
...
...
@@ -8,6 +8,9 @@ from django.http import HttpResponse
from
djangorestframework.views
import
APIView
from
djangorestframework
import
permissions
from
djangorestframework.tokenauth.models
import
Token
from
djangorestframework.tokenauth.authentication
import
TokenAuthentication
import
base64
...
...
@@ -20,6 +23,8 @@ class MockView(APIView):
def
put
(
self
,
request
):
return
HttpResponse
({
'a'
:
1
,
'b'
:
2
,
'c'
:
3
})
MockView
.
authentication
+=
(
TokenAuthentication
,)
urlpatterns
=
patterns
(
''
,
(
r'^$'
,
MockView
.
as_view
()),
)
...
...
@@ -104,3 +109,40 @@ class SessionAuthTests(TestCase):
"""
response
=
self
.
csrf_client
.
post
(
'/'
,
{
'example'
:
'example'
})
self
.
assertEqual
(
response
.
status_code
,
403
)
class
TokenAuthTests
(
TestCase
):
"""Token authentication"""
urls
=
'djangorestframework.tests.authentication'
def
setUp
(
self
):
self
.
csrf_client
=
Client
(
enforce_csrf_checks
=
True
)
self
.
username
=
'john'
self
.
email
=
'lennon@thebeatles.com'
self
.
password
=
'password'
self
.
user
=
User
.
objects
.
create_user
(
self
.
username
,
self
.
email
,
self
.
password
)
self
.
key
=
'abcd1234'
self
.
token
=
Token
.
objects
.
create
(
key
=
self
.
key
,
user
=
self
.
user
)
def
test_post_form_passing_token_auth
(
self
):
"""Ensure POSTing json over token auth with correct credentials passes and does not require CSRF"""
auth
=
self
.
key
response
=
self
.
csrf_client
.
post
(
'/'
,
{
'example'
:
'example'
},
HTTP_AUTHORIZATION
=
auth
)
self
.
assertEqual
(
response
.
status_code
,
200
)
def
test_post_json_passing_token_auth
(
self
):
"""Ensure POSTing form over token auth with correct credentials passes and does not require CSRF"""
auth
=
self
.
key
response
=
self
.
csrf_client
.
post
(
'/'
,
json
.
dumps
({
'example'
:
'example'
}),
'application/json'
,
HTTP_AUTHORIZATION
=
auth
)
self
.
assertEqual
(
response
.
status_code
,
200
)
def
test_post_form_failing_token_auth
(
self
):
"""Ensure POSTing form over token auth without correct credentials fails"""
response
=
self
.
csrf_client
.
post
(
'/'
,
{
'example'
:
'example'
})
self
.
assertEqual
(
response
.
status_code
,
403
)
def
test_post_json_failing_token_auth
(
self
):
"""Ensure POSTing json over token auth without correct credentials fails"""
response
=
self
.
csrf_client
.
post
(
'/'
,
json
.
dumps
({
'example'
:
'example'
}),
'application/json'
)
self
.
assertEqual
(
response
.
status_code
,
403
)
djangorestframework/tokenauth/__init__.py
0 → 100644
View file @
f3e65eab
djangorestframework/tokenauth/authentication.py
0 → 100644
View file @
f3e65eab
from
djangorestframework.authentication
import
BaseAuthentication
from
.models
import
Token
class
TokenAuthentication
(
BaseAuthentication
):
"""
Use a token model for authentication.
A custom token model may be used here, but must have the following minimum
properties:
* key -- The string identifying the token
* user -- The user to which the token belongs
* revoked -- The status of the token
The BaseToken class is available as an abstract model to be derived from.
The token key should be passed in as a string to the "Authorization" HTTP
header.
"""
model
=
Token
def
authenticate
(
self
,
request
):
key
=
request
.
META
.
get
(
'HTTP_AUTHORIZATION'
,
''
)
.
strip
()
if
not
key
:
return
None
try
:
token
=
self
.
model
.
objects
.
get
(
key
=
key
)
except
self
.
model
.
DoesNotExist
:
return
None
if
token
.
user
.
is_active
and
not
token
.
revoked
:
return
(
token
.
user
,
token
)
djangorestframework/tokenauth/models.py
0 → 100644
View file @
f3e65eab
from
django.db
import
models
class
BaseToken
(
models
.
Model
):
"""
The base abstract authorization token model class.
"""
key
=
models
.
CharField
(
max_length
=
32
,
primary_key
=
True
)
user
=
models
.
ForeignKey
(
'auth.User'
)
revoked
=
models
.
BooleanField
(
default
=
False
)
class
Meta
:
abstract
=
True
class
Token
(
BaseToken
):
"""
The default authorization token model class.
"""
pass
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