OAuth 2 Framework for RESTful APIs


What is RESTful API?

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
A RESTful API -- also referred to as a RESTful web service -- is based on representational state transfer (REST) technology, an architectural style, and approach to communications often used in web services development.

How RESTful APIs work?

A RESTful API breaks down a transaction to create a series of small modules. Each module addresses a particular underlying part of the transaction. This modularity provides developers with a lot of flexibility, but it can be challenging for developers to design from scratch.

Let's get started...

I have created an authorization server and resource server both in a single API. There is an endpoint that you can call in order to retrieve the resources for the demonstration purposes.
This is written using node.js. In order to run this on your computer, you have to have node.js installed on your computer.

You can download the source codes from here: https://github.com/csandeepa/OAuth-2.0-RESTful-API

app.js>>>>>>


As you can see OAuth grant type I have given is client_credentials. This has to be mentioned in the request body when you try to get the access token from the authorization server.
Also, this app tuns on port 4000. You can give any port number here.
There are two endpoints I have created in this. One to get the access token which is "/oauth/token" and the other one is to get resources which is "/profile".
As resources, I have hardcoded one value which is the name ("") and this comes as a JSON object. 

model.js>>>>>>


Here I have created a sample user. (username = test, password = test) and all the functions that handle requests from the client are written in this file.

Now, just run the app.js


To make all get and post requests to the resource server we use RESTclient Mozilla Firefox Add-on. You can use other similar products such as Postman for this.

First of all, We have to make a POST request to get the access token from the authorization server.
For that, we have to send the authorization key in the header.

Authorization : Bearer XXXXXXXXXXXXXXX
And also we have to mention the content type in the header.

I'll demonstrate with RestClient on Mozilla Firefox with creating all the requests manually and of course how to retrieve resources.

Then we have to mention these 3 parameters in the body.

username=test
password=test
grant_type=client_credentials

The URL should be the endpoint that gives us the access token.

http://localhost:4000/oauth/token 


When we send this we get the response which has access token in it. This access token also has an expiration time.
Then we have to make a GET request to retrieve the resources we need.


Now our URL is different because we have to call a different endpoint to get these resources which is "http://localhost:4000/profile".
We do not have to mention anything in the body.
In the request header, we should send the access token we got in the previous step.

Authization: Bearer XXXXXXXXXXXXXXX

Make sure that the access token is not expired. Otherwise, you will get an error message saying that it has expired.
When you sent this request you get a response that contains the resources we specified in the code.

~CS

Comments

Post a Comment

If you got something from my writings, just put your thoughts out to words...

Popular posts from this blog

Best Ways to Make Money Online 2019

Cross-site Request Forgery Protection in Web Applications via Synchronizer Token

Amazing Places To Visit In The World