1
- Part 1 - make a provider in a minute
1
+ Part 1 - Make a Provider in a Minute
2
2
====================================
3
3
4
4
Scenario
5
5
--------
6
- You want to make your own :term: `Authorization Server `, managing the client applications which will have access to a
7
- certain API, releasing the tokens and so on...
6
+ You want to make your own :term: `Authorization Server ` to issue access tokens to client applications for a certain API.
8
7
9
- Start your app
8
+ Start Your App
10
9
--------------
11
- During this tutorial you will make and XHR POST from an Heroku deployed app to your localhost instance.
12
- To achieve this operation you need a properly configured Django server with `django-cors-headers ` app installed, since
13
- the domain that originated the request (the app on Heroku) is different from the destination domain (your local instance).
14
- Such "cross-domain" requests are by default forbidden by web browsers unless you use CORS.
15
- You can read more about `CORS here <http://en.wikipedia.org/wiki/Cross-origin_resource_sharing >`_.
10
+ During this tutorial you will make an XHR POST from a Heroku deployed app to your localhost instance.
11
+ Since the domain that will originate the request (the app on Heroku) is different than the destination domain (your local instance),
12
+ you will need to install the `django-cors-headers <https://github.com/ottoyiu/django-cors-headers >`_ app.
13
+ These "cross-domain" requests are by default forbidden by web browsers unless you use `CORS <http://en.wikipedia.org/wiki/Cross-origin_resource_sharing >`_.
16
14
17
15
Create a virtualenv and install `django-oauth-toolkit ` and `django-cors-headers `:
18
16
19
17
::
20
18
21
19
pip install django-oauth-toolkit django-cors-headers
22
20
23
- start a Django project, add `oauth2_provider ` and `corsheaders ` to the installed apps, enable the admin.
21
+ Start a Django project, add `oauth2_provider ` and `corsheaders ` to the installed apps, and enable admin:
24
22
25
23
.. code-block :: python
26
24
@@ -31,7 +29,7 @@ start a Django project, add `oauth2_provider` and `corsheaders` to the installed
31
29
' corsheaders' ,
32
30
}
33
31
34
- Include the Django OAuth Toolkit urls in your `urls.py `, choose the urlspace you prefer, for example:
32
+ Include the Django OAuth Toolkit urls in your `urls.py `, choosing the urlspace you prefer. For example:
35
33
36
34
.. code-block :: python
37
35
@@ -42,7 +40,7 @@ Include the Django OAuth Toolkit urls in your `urls.py`, choose the urlspace you
42
40
# ...
43
41
)
44
42
45
- Include this middleware in your `settings.py `:
43
+ Include the CORS middleware in your `settings.py `:
46
44
47
45
.. code-block :: python
48
46
@@ -52,7 +50,7 @@ Include this middleware in your `settings.py`:
52
50
# ...
53
51
)
54
52
55
- Configure this setting to allow CORS requests from all domains (just for the scope of this tutorial):
53
+ Allow CORS requests from all domains (just for the scope of this tutorial):
56
54
57
55
.. code-block :: python
58
56
@@ -62,30 +60,31 @@ Configure this setting to allow CORS requests from all domains (just for the sco
62
60
63
61
Include the required hidden input in your login template, `registration/login.html `.
64
62
The ``{{ next }} `` template context variable will be populated with the correct
65
- redirect value. Django provides more information on ` login templates here
66
- <https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views. login> `_ .
63
+ redirect value. See the ` Django documentation < https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.views. login>`_
64
+ for details on using login templates .
67
65
68
66
.. code-block :: html
69
67
70
68
<input type =" hidden" name =" next" value =" {{ next }}" />
71
69
72
- As a final step, make a syncdb, start the internal server and login with your credentials.
70
+ As a final step, execute syncdb, start the internal server, and login with your credentials.
73
71
74
72
Create an OAuth2 Client Application
75
73
-----------------------------------
76
- An application which wants to perform API requests must be registered in the :term: `Authorization Server ` to be properly
77
- identified. This operation is usually done manually by a developer, who asks for an account in the
78
- :term: `Authorization Server ` and gets access to some sort of backoffice where she can register her application, which
79
- will act as a :term: `Client ` (or :term: `Application ` in the Django OAuth Toolkit lingo).
80
- Let's perform exactly this operation.
81
- Point your browser to `http://localhost:8000/o/applications/ ` and add an Application instance.
74
+ Before your :term: `Application ` can use the :term: `Authorization Server ` for user login,
75
+ you must first register the app (also known as the :term: `Client `.) Once registered, your app will be granted access to
76
+ the API, subject to approval by its users.
77
+
78
+ Let's register your application.
79
+
80
+ Point your browser to http://localhost:8000/o/applications/ and add an Application instance.
82
81
`Client id ` and `Client Secret ` are automatically generated, you have to provide the rest of the informations:
83
82
84
- * `User `: the owner of the Application (tipically a developer), could be the current logged in user.
83
+ * `User `: the owner of the Application (e.g. a developer, or the currently logged in user.)
85
84
86
- * `Redirect uris `: at a certain point of the token request process, the :term: ` Authorization Server ` needs to know a
87
- list of url (must be at least one) in the client application service where delivering the :term: ` Authorization Token `.
88
- Developers have the responsibility to correctly provide this value . For this tutorial, paste verbatim the value
85
+ * `Redirect uris `: Applications must register at least one redirection endpoint prior to utilizing the
86
+ authorization endpoint. The :term: ` Authorization Server ` will deliver the access token to the client only if the client
87
+ specifies one of the verified redirection uris . For this tutorial, paste verbatim the value
89
88
`http://django-oauth-toolkit.herokuapp.com/consumer/exchange/ `
90
89
91
90
* `Client type `: this value affects the security level at which some communications between the client application and
@@ -99,28 +98,28 @@ Point your browser to `http://localhost:8000/o/applications/` and add an Applica
99
98
Take note of the `Client id ` and the `Client Secret ` then logout (this is needed only for testing the authorization
100
99
process we'll explain shortly)
101
100
102
- Test your authorization server
101
+ Test Your Authorization Server
103
102
------------------------------
104
- Your authorization server is ready and can start releasing access tokens. To test the process you need an OAuth2
105
- consumer: if you know OAuth2 enough you can use curl, requests or anything can speak http. For the rest of us, we have
106
- a `consumer service <http://django-oauth-toolkit.herokuapp.com/consumer/ >`_ deployed on Heroku you can use to test your
107
- provider.
103
+ Your authorization server is ready and can begin issuing access tokens. To test the process you need an OAuth2
104
+ consumer; if you are familiar enough with OAuth2, you can use curl, requests, or anything that speaks http. For the rest
105
+ of us, there is a `consumer service <http://django-oauth-toolkit.herokuapp.com/consumer/ >`_ deployed on Heroku to test
106
+ your provider.
108
107
109
- Build an authorization link for your users
108
+ Build an Authorization Link for Your Users
110
109
++++++++++++++++++++++++++++++++++++++++++
111
- The process of authorizing an application to access OAuth2 protected data in an :term: `Authorization Code ` flow is always
112
- started by the user. You have to prompt your users with a special link they click to start the process. Go to the
113
- `Consumer <http://django-oauth-toolkit.herokuapp.com/consumer/ >`_ page and fill the form with the data of the
114
- application you created earlier on this tutorial. Submit the form, you'll get the link your users should follow to get
115
- to the authorization page.
110
+ Authorizing an application to access OAuth2 protected data in an :term: `Authorization Code ` flow is always initiated
111
+ by the user. Your application can prompt users to click a special link to start the process. Go to the
112
+ `Consumer <http://django-oauth-toolkit.herokuapp.com/consumer/ >`_ page and complete the form by filling in your
113
+ application's details obtained from the steps in this tutorial. Submit the form, and you'll receive a link your users can
114
+ use to access the authorization page.
116
115
117
- Authorize the application
116
+ Authorize the Application
118
117
+++++++++++++++++++++++++
119
- When the user clicks the link, she is redirected to your (possibly local) :term: `Authorization Server `.
120
- If you're not logged in, at this point you should be prompted for username and password. This is because the authorization
121
- page is login protected by django-oauth-toolkit. Login, then you should see the not so cute form user can use to give
118
+ When a user clicks the link, she is redirected to your (possibly local) :term: `Authorization Server `.
119
+ If you're not logged in, you will be prompted for username and password. This is because the authorization
120
+ page is login protected by django-oauth-toolkit. Login, then you should see the not so cute form users can use to give
122
121
her authorization to the client application. Flag the *Allow * checkbox and click *Authorize *, you will be redirected
123
- again on the consumer service.
122
+ again on to the consumer service.
124
123
125
124
__ loginTemplate _
126
125
@@ -141,7 +140,7 @@ Refresh the token
141
140
+++++++++++++++++
142
141
The page showing the access token retrieved from the :term: `Authorization Server ` also let you make a POST request to
143
142
the server itself to swap the refresh token for another, brand new access token.
144
- Just fill in the missing form fields and click the Refresh button: if everything goes smooth you will se the access and
143
+ Just fill in the missing form fields and click the Refresh button: if everything goes smooth you will see the access and
145
144
refresh token change their values, otherwise you will likely see an error message.
146
145
When finished playing with your authorization server, take note of both the access and refresh tokens, we will use them
147
146
for the next part of the tutorial.
0 commit comments