Django OAuth2 provider
Build a Python Identity and Access Management (IAM) using Django, OAuth Toolkit, REST Framework and OAuthLib.
What we will build?
The plan is to build an IAM from ground up starting simple and adding features along the way.
On this first part we will:
- Create the Django project.
- Install and configure Django OAuth Toolkit.
- Create two OAuth2 applications.
- Use Authorization code grant flow.
- Use Client Credential grant flow.
What is an IAM?
Is the discipline that enables the right individuals to access the right resources at the right times for the right reasons. – Gartner Glossary
What is OAuth?
OAuth is an open standard for access delegation, commonly used as a way for Internet users to grant websites or applications access to their information on other websites but without giving them the passwords. – Whitson Gordon
Django
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. – Django website
Let’s get started by creating a virtual environment:
This will create, activate and change directory to the new Python virtual environment.
Install Django:
Create a Django project:
This will create a mysite directory in your current directory. With the following estructure:
Create a Django application:
That’ll create a directory users
, which is laid out like this:
If you’re starting a new project, it’s highly recommended to set up a custom user model, even if the default User model is sufficient for you. This model behaves identically to the default user model, but you’ll be able to customize it in the future if the need arises. – Django documentation
Edit users/models.py
adding the code bellow:
Change iam/settings.py
to add users
application to INSTALLED_APPS
:
Configure users.User
to be the model used for the auth
application adding AUTH_USER_MODEL
to iam/settings.py
:
Create inital migration for users
application User
model:
The command above will create the migration:
Finally execute the migration:
The migrate
output:
Django OAuth Toolkit
Django OAuth Toolkit can help you providing out of the box all the endpoints, data and logic needed to add OAuth2 capabilities to your Django projects. – Django OAuth Toolkit Documentation
Install Django OAuth Toolkit:
Add oauth2_provider
to INSTALLED_APPS
in iam/settings.py
:
Execute the migration:
The migrate
output:
Include oauth2_provider.urls
to iam/urls.py
as follows:
This will make available endpoints to authorize, generate token and create OAuth applications.
Last change, add LOGIN_URL
to iam/settings.py
:
We will use Django Admin login to make our life easy.
Create a user:
OAuth2 Authorization Grants
An authorization grant is a credential representing the resource owner’s authorization (to access its protected resources) used by the client to obtain an access token. – RFC6749
The OAuth framework specifies several grant types for different use cases. – Grant types
We will start by given a try to the grant types listed below:
- Authorization code
- Client credential
This two grant types cover the most initially used uses cases.
Authorization Code
The Authorization Code flow is best used in web and mobile apps. This is the flow used for third party integration, the user authorize your partner to access its products in your APIs.
Start the development server:
Point your browser to http://127.0.0.1:8000/o/applications/register/ lets create an application.
Fill the form as show in the screenshot bellow and before save take note of Client id
and Client secret
we will use it in a minute.
Export Client id
and Client secret
as environment variable:
To start the Authorization code flow got to this URL with is the same as show bellow:
Note the parameters we pass:
response_type
:code
client_id
:vW1RcAl7Mb0d5gyHNQIAcH110lWoOW2BmWJIero8
redirect_uri
:http://127.0.0.1:8000/noexist/callback
This identifies your application, the user is asked to authorize your application to access its resources.
Go ahead and authorize the web-app
Remenber we used http://127.0.0.1:8000/noexist/callback
as redirect_uri
you will get a Page not found (404)
but it worked if you get a url like:
This is the OAuth2 provider trying to give you a code
in this case uVqLxiHDKIirldDZQfSnDsmYW1Abj2
.
Export it as environment variable:
Now that you have the user authorization is time to get an access token.
To be more easy to visualize:
The OAuth2 provider will return the follow response:
To access the user resources we just use the access_token
:
Client Credential
The Client Credential grant is suitable for machine-to-machine authentication. You authorize your own service or worker to change a bank account transaction status to accepted.
Point your browser to http://127.0.0.1:8000/o/applications/register/ lets create an application.
Fill the form as show in the screenshot bellow and before save take note of Client id
and Client secret
we will use it in a minute.
Export Client id
and Client secret
as environment variable:
The Client Credential flow is simpler than the Authorization Code flow.
We need to encode client_id and client_secret as HTTP base authentication encoded in base64 I use the following code to do that.
Export the credential as environment variable:
To start the Client Credential flow you call /token/
endpoint direct:
To be more easy to visualize:
The OAuth2 provider will return the follow response:
The end.
At next part we will build a simple API to protect with OAuth2.