2014-12-19 15:27:43 +00:00
|
|
|
######################
|
|
|
|
Django OpenID Provider
|
|
|
|
######################
|
|
|
|
|
2015-01-05 20:32:58 +00:00
|
|
|
Important things that you should know:
|
|
|
|
|
2015-01-05 20:40:40 +00:00
|
|
|
- Although OpenID was built on top of OAuth2, this isn't an OAuth2 server. Maybe in a future it will be.
|
|
|
|
- This just cover the ``authorization_code`` flow, no support for ``implicit`` flow at this moment.
|
|
|
|
- Only support for requesting Claims using Scope Values.
|
2015-01-05 20:32:58 +00:00
|
|
|
|
2014-12-19 15:27:43 +00:00
|
|
|
************
|
|
|
|
Installation
|
|
|
|
************
|
|
|
|
|
|
|
|
Install the package using pip.
|
|
|
|
|
2014-12-19 15:59:11 +00:00
|
|
|
.. code:: bash
|
|
|
|
|
2015-01-05 20:32:58 +00:00
|
|
|
pip install https://github.com/juanifioren/django-openid-provider/archive/master.zip
|
2014-12-19 15:59:11 +00:00
|
|
|
|
|
|
|
|
2014-12-19 15:27:43 +00:00
|
|
|
Add it to your proyect apps.
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
INSTALLED_APPS = (
|
|
|
|
'django.contrib.admin',
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
'django.contrib.sessions',
|
|
|
|
'django.contrib.messages',
|
|
|
|
'django.contrib.staticfiles',
|
|
|
|
'openid_provider',
|
|
|
|
# ...
|
|
|
|
)
|
|
|
|
|
|
|
|
Add the provider urls to your proyect.
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
# ...
|
|
|
|
url(r'^openid/', include('openid_provider.urls', namespace='openid_provider')),
|
|
|
|
# ...
|
|
|
|
)
|
|
|
|
|
|
|
|
Finally, add a login view and ensure that has the same url defined in `LOGIN_URL` setting.
|
|
|
|
|
|
|
|
See: https://docs.djangoproject.com/en/1.7/ref/settings/#login-url
|
|
|
|
|
|
|
|
********************
|
|
|
|
Create User & Client
|
|
|
|
********************
|
|
|
|
|
|
|
|
First of all, we need to create a user: ``python manage.py createsuperuser``.
|
|
|
|
|
|
|
|
Then let's create a Client. Start django shell: ``python manage.py shell``.
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
>>> from openid_provider.models import Client
|
2015-01-06 19:29:31 +00:00
|
|
|
>>> c = Client(name='Some Client', client_id='123', client_secret='456', client_type='public', grant_type='authorization_code', response_type='code', redirect_uris=['http://example.com/'])
|
2014-12-19 15:27:43 +00:00
|
|
|
>>> from django.contrib.auth.models import User
|
|
|
|
>>> c.user = User.objects.all()[0]
|
|
|
|
>>> c.save()
|
|
|
|
|
|
|
|
*******************
|
|
|
|
/authorize endpoint
|
|
|
|
*******************
|
|
|
|
|
|
|
|
.. code:: curl
|
|
|
|
|
|
|
|
GET /openid/authorize?client_id=123&redirect_uri=http%3A%2F%2Fexample.com%2F&response_type=code&scope=openid%20profile%20email&state=abcdefgh HTTP/1.1
|
|
|
|
Host: localhost:8000
|
|
|
|
Cache-Control: no-cache
|
|
|
|
Content-Type: application/x-www-form-urlencoded
|
|
|
|
|
|
|
|
****
|
|
|
|
Code
|
|
|
|
****
|
|
|
|
|
|
|
|
After the user accepts and authorizes the client application, the server redirects to:
|
|
|
|
|
|
|
|
.. code:: curl
|
|
|
|
|
|
|
|
http://example.com/?code=5fb3b172913448acadce6b011af1e75e&state=abcdefgh
|
|
|
|
|
|
|
|
We extract the ``code`` param and use it to obtain access token.
|
|
|
|
|
|
|
|
***************
|
|
|
|
/token endpoint
|
|
|
|
***************
|
|
|
|
|
|
|
|
.. code:: curl
|
|
|
|
|
|
|
|
POST /openid/token/ HTTP/1.1
|
|
|
|
Host: localhost:8000
|
|
|
|
Cache-Control: no-cache
|
|
|
|
Content-Type: application/x-www-form-urlencoded
|
|
|
|
|
|
|
|
client_id=123&client_secret=456&redirect_uri=http%253A%252F%252Fexample.com%252F&grant_type=authorization_code&code=[CODE]&state=abcdefgh
|
|
|
|
|
|
|
|
******************
|
|
|
|
/userinfo endpoint
|
|
|
|
******************
|
|
|
|
|
|
|
|
.. code:: curl
|
|
|
|
|
|
|
|
POST /openid/userinfo/ HTTP/1.1
|
|
|
|
Host: localhost:8000
|
|
|
|
Authorization: Bearer [ACCESS_TOKEN]
|