Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAuth support #709

Merged
merged 47 commits into from
Mar 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1062d71
add tests for OAuth authentication
swistakm Feb 25, 2013
ced22db
add django-oauth-plus & oauth2 to installed apps in runtests settings.py
swistakm Feb 25, 2013
1aed9c1
add OAuthAuthentication class
swistakm Feb 25, 2013
e2b11a2
add django-oauth-plus & oauth2 to .travis.yml
swistakm Feb 25, 2013
cfce455
add django-oauth-plus & oauth2 to optionals.txt
swistakm Feb 25, 2013
5d9ed34
add OAuthAuthentication documentation stub
swistakm Feb 25, 2013
59a6f5f
Move oauth2 and django-oauth-plus imports to compat and fix some mino…
swistakm Feb 26, 2013
d84c2cf
OAuth tests now are skipped unless django-oauth-plus and oauth2 are i…
swistakm Feb 26, 2013
a430445
runtest.settings fixed if django-oauth-plus or oauth2 are not installed
swistakm Feb 26, 2013
dd355d5
oauth2 & django-oauth-plus installed only on 2.x
swistakm Feb 27, 2013
55ea5b9
import compat version of unittest
swistakm Feb 27, 2013
2eabc5c
rfc5849 link with anchor
swistakm Feb 27, 2013
468b5e4
Add tests for OAuth2 authentication
dulacp Mar 1, 2013
4e1f77d
Add django-oauth2-provider to the installed apps
dulacp Mar 1, 2013
592a0a5
Add django-oauth2-provider to optionals.txt
dulacp Mar 1, 2013
da9d7fb
Add the OAuth2Authentication class
dulacp Mar 1, 2013
d8f455b
Add OAuth2Authentication documentation
dulacp Mar 1, 2013
aed3c13
Merge branch 'master' into oauth2-authentication
dulacp Mar 1, 2013
9d5c306
Improve the `django-oauth2-provider` import block
dulacp Mar 1, 2013
653fcf7
Use the correct doc link style
dulacp Mar 1, 2013
d4c2267
Clean up some print and comments
dulacp Mar 1, 2013
182edb3
Add django-oauth2-provider to .travis.yml
dulacp Mar 1, 2013
721dc51
Use django.utils to import the unittest module
dulacp Mar 1, 2013
8809c46
Add new OAuth2 tests
dulacp Mar 2, 2013
c449dd4
Properly fail to wrong Authorization token type
dulacp Mar 2, 2013
6f57641
Use the PyPI django-oauth2-provider version
dulacp Mar 2, 2013
cda21a3
Only add the django-oauth2-provider apps if the module is installed
dulacp Mar 2, 2013
30e3775
Update the documentation
dulacp Mar 2, 2013
8845c0b
Fix import errors
dulacp Mar 3, 2013
5a56f92
Update the package dependency url style in tox.ini
dulacp Mar 3, 2013
d4e3610
Merge & clean OAuth support
tomchristie Mar 7, 2013
44930f3
Fix Py3k syntax errors
tomchristie Mar 7, 2013
1d62594
Clean ups.
tomchristie Mar 7, 2013
a4b3399
Merge OAuth2 work.
tomchristie Mar 7, 2013
c2eb276
Update docs for OAuth 2.0
tomchristie Mar 7, 2013
e42e498
Tweak docs
tomchristie Mar 7, 2013
650d8e6
More bits of cleanup
tomchristie Mar 8, 2013
1016c14
Added @dulaccc.
tomchristie Mar 8, 2013
2596c12
Fixes for auth header checking.
tomchristie Mar 8, 2013
5e993f3
Merge
tomchristie Mar 8, 2013
fd9d6c6
Fix crazy typo.
tomchristie Mar 8, 2013
a34f45b
Docs polishing.
tomchristie Mar 9, 2013
e03906a
Add TokenHasReadWriteScope class for permissions based on scopes
dulacp Mar 10, 2013
eec8efa
Add the implementation for TokenHasReadWriteScope permissions w/ oauth 1
dulacp Mar 10, 2013
12ac357
Merge pull request #721 from dulaccc/token-scope-permission
tomchristie Mar 12, 2013
e8db504
Merge master
tomchristie Mar 12, 2013
f513db7
Clean up TokenHasReadWriteScope slightly
tomchristie Mar 12, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ env:
install:
- pip install $DJANGO
- pip install defusedxml==0.3
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install oauth2==1.5.211 --use-mirrors; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth-plus==2.0 --use-mirrors; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth2-provider==0.2.3 --use-mirrors; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-filter==0.5.4 --use-mirrors; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} == '3' ]]; then pip install https://github.com/alex/django-filter/tarball/master; fi"
- export PYTHONPATH=.
Expand Down
98 changes: 97 additions & 1 deletion docs/api-guide/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Unauthenticated responses that are denied permission will result in an `HTTP 401

## TokenAuthentication

This authentication scheme uses a simple token-based HTTP Authentication scheme. Token authentication is appropriate for client-server setups, such as native desktop and mobile clients.
This authentication scheme uses a simple token-based HTTP Authentication scheme. Token authentication is appropriate for client-server setups, such as native desktop and mobile clients.

To use the `TokenAuthentication` scheme, include `rest_framework.authtoken` in your `INSTALLED_APPS` setting:

Expand Down Expand Up @@ -207,6 +207,97 @@ Unauthenticated responses that are denied permission will result in an `HTTP 403

If you're using an AJAX style API with SessionAuthentication, you'll need to make sure you include a valid CSRF token for any "unsafe" HTTP method calls, such as `PUT`, `PATCH`, `POST` or `DELETE` requests. See the [Django CSRF documentation][csrf-ajax] for more details.

## OAuthAuthentication

This authentication uses [OAuth 1.0a][oauth-1.0a] authentication scheme. OAuth 1.0a provides signature validation which provides a reasonable level of security over plain non-HTTPS connections. However, it may also be considered more complicated than OAuth2, as it requires clients to sign their requests.

This authentication class depends on the optional `django-oauth-plus` and `oauth2` packages. In order to make it work you must install these packages and add `oauth_provider` to your `INSTALLED_APPS`:

INSTALLED_APPS = (
...
`oauth_provider`,
)

Don't forget to run `syncdb` once you've added the package.

python manage.py syncdb

#### Getting started with django-oauth-plus

The OAuthAuthentication class only provides token verification and signature validation for requests. It doesn't provide authorization flow for your clients. You still need to implement your own views for accessing and authorizing tokens.

The `django-oauth-plus` package provides simple foundation for classic 'three-legged' oauth flow. Please refer to [the documentation][django-oauth-plus] for more details.

## OAuth2Authentication

This authentication uses [OAuth 2.0][rfc6749] authentication scheme. OAuth2 is more simple to work with than OAuth1, and provides much better security than simple token authentication. It is an unauthenticated scheme, and requires you to use an HTTPS connection.

This authentication class depends on the optional [django-oauth2-provider][django-oauth2-provider] project. In order to make it work you must install this package and add `provider` and `provider.oauth2` to your `INSTALLED_APPS`:

INSTALLED_APPS = (
...
'provider',
'provider.oauth2',
)

You must also include the following in your root `urls.py` module:

url(r'^oauth2/', include('provider.oauth2.urls', namespace='oauth2')),

Note that the `namespace='oauth2'` argument is required.

Finally, sync your database.

python manage.py syncdb
python manage.py migrate

---

**Note:** If you use `OAuth2Authentication` in production you must ensure that your API is only available over `https` only.

---

#### Getting started with django-oauth2-provider

The `OAuth2Authentication` class only provides token verification for requests. It doesn't provide authorization flow for your clients.

The OAuth 2 authorization flow is taken care by the [django-oauth2-provider][django-oauth2-provider] dependency. A walkthrough is given here, but for more details you should refer to [the documentation][django-oauth2-provider-docs].

To get started:

##### 1. Create a client

You can create a client, either through the shell, or by using the Django admin.

Go to the admin panel and create a new `Provider.Client` entry. It will create the `client_id` and `client_secret` properties for you.

##### 2. Request an access token

To request an access token, submit a `POST` request to the url `/oauth2/access_token` with the following fields:

* `client_id` the client id you've just configured at the previous step.
* `client_secret` again configured at the previous step.
* `username` the username with which you want to log in.
* `password` well, that speaks for itself.

You can use the command line to test that your local configuration is working:

curl -X POST -d "client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=password&username=YOUR_USERNAME&password=YOUR_PASSWORD" http://localhost:8000/oauth2/access_token/

You should get a response that looks something like this:

{"access_token": "<your-access-token>", "scope": "read", "expires_in": 86399, "refresh_token": "<your-refresh-token>"}

##### 3. Access the API

The only thing needed to make the `OAuth2Authentication` class work is to insert the `access_token` you've received in the `Authorization` request header.

The command line to test the authentication looks like:

curl -H "Authorization: Bearer <your-access-token>" http://localhost:8000/api/?client_id=YOUR_CLIENT_ID\&client_secret=YOUR_CLIENT_SECRET

---

# Custom authentication

To implement a custom authentication scheme, subclass `BaseAuthentication` and override the `.authenticate(self, request)` method. The method should return a two-tuple of `(user, auth)` if authentication succeeds, or `None` otherwise.
Expand Down Expand Up @@ -262,3 +353,8 @@ HTTP digest authentication is a widely implemented scheme that was intended to r
[south-dependencies]: http://south.readthedocs.org/en/latest/dependencies.html
[juanriaza]: https://github.com/juanriaza
[djangorestframework-digestauth]: https://github.com/juanriaza/django-rest-framework-digestauth
[oauth-1.0a]: http://oauth.net/core/1.0a
[django-oauth-plus]: http://code.larlet.fr/django-oauth-plus
[django-oauth2-provider]: https://github.com/caffeinehit/django-oauth2-provider
[django-oauth2-provider-docs]: https://django-oauth2-provider.readthedocs.org/en/latest/
[rfc6749]: http://tools.ietf.org/html/rfc6749
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ The following packages are optional:
* [PyYAML][yaml] (3.10+) - YAML content-type support.
* [defusedxml][defusedxml] (0.3+) - XML content-type support.
* [django-filter][django-filter] (0.5.4+) - Filtering support.
* [django-oauth-plus][django-oauth-plus] (2.0+) and [oauth2][oauth2] (1.5.211+) - OAuth 1.0a support.
* [django-oauth2-provider][django-oauth2-provider] (0.2.3+) - OAuth 2.0 support.

**Note**: The `oauth2` python package is badly misnamed, and actually provides OAuth 1.0a support. Also note that packages required for both OAuth 1.0a, and OAuth 2.0 are not yet Python 3 compatible.

## Installation

Expand Down Expand Up @@ -180,6 +184,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[yaml]: http://pypi.python.org/pypi/PyYAML
[defusedxml]: https://pypi.python.org/pypi/defusedxml
[django-filter]: http://pypi.python.org/pypi/django-filter
[oauth2]: https://github.com/simplegeo/python-oauth2
[django-oauth-plus]: https://bitbucket.org/david/django-oauth-plus/wiki/Home
[django-oauth2-provider]: https://github.com/caffeinehit/django-oauth2-provider
[0.4]: https://github.com/tomchristie/django-rest-framework/tree/0.4.X
[image]: img/quickstart.png
[sandbox]: http://restframework.herokuapp.com/
Expand Down
3 changes: 2 additions & 1 deletion docs/topics/credits.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The following people have helped make REST framework great.
* Jonas Braun - [iekadou]
* Ian Dash - [bitmonkey]
* Bouke Haarsma - [bouke]
* Pierre Dulac - [dulaccc]

Many thanks to everyone who's contributed to the project.

Expand Down Expand Up @@ -254,4 +255,4 @@ You can also contact [@_tomchristie][twitter] directly on twitter.
[iekadou]: https://github.com/iekadou
[bitmonkey]: https://github.com/bitmonkey
[bouke]: https://github.com/bouke

[dulaccc]: https://github.com/dulaccc
3 changes: 3 additions & 0 deletions optionals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ markdown>=2.1.0
PyYAML>=3.10
defusedxml>=0.3
django-filter>=0.5.4
django-oauth-plus>=2.0
oauth2>=1.5.211
django-oauth2-provider>=0.2.3
Loading