How to implement OAuth 2.0 server with Redis

Index:

Step 1: Install XAMPP
Step 2: Get OAuth 2.0 server
Step 3: Setup OAuth 2.0 server in XAMPP
Step 4: Install Redis
Step 5: Create and feed token and users structures in Redis
Step 6: Test OAuth 2.0

First of all


Before you start with the implementation, take a look to this video tutorial.
Understanding OAuth

Community site : http://oauth.net/


Additional to the videos above, I'll share you the OAuth 2.0 authorization flow to have a better idea how this thing works.

















Step 1: Install XAMPP

To test your OAuth 2.0 server you can install XAMPP from here (skip this step if you have another application server that supports PHP 5.3.9+)

Step 2: Get OAuth 2.0 server

Get a functional OAuth 2.0 server from here

     Or clone it :)
git clone https://github.com/Kodran/OAuth-2.0-server-with-redis.git

Step 3: Setup OAuth 2.0 server in the web application root folder

If you are working with XAMPP, go to XAMPP's application folder ("htdocs" in this case) then paste OAuth server in there.
MAC OSX : /Applications/XAMPP/xamppfiles/htdocs
Windows: c:\{XAMPP installation path}\htdocs
If you are using another web application server, paste OAuth server in the correct path of it.

Step 4: Install Redis

Redis will help us to store in memory (cache) your tokens and users of OAuth.

Download here

Step 5: Create and feed token and users structures in Redis

Now let's feed our Redis memory with all needed structures of OAuth.

To start Redis, open your console and type this:
~ redis-server

Then you'll see something like this:
















After that, open another console windows and type this:
~ redis-cli

With the command above we start Redis client, now we can add our OAuth structures. 
















Copy the lines below to the console to add new structures (key command: "Set")

//OAuth clients json structure
set oauth_clients:client_id_123 '{"client_id":"client_id_123","client_secret":"client_password", "redirect_uri":"http://some-awesome-uri.com", "grant_types":"authorization_code", "scope":"default","user_id":""}'

//OAuth access tokens structure
set oauth_access_tokens: '{"access_token":"","client_id":"", "user_id":"", "expires":"", "scope":""}'

//OAuth authorization codes structure
set oauth_authorization_codes: '{"authorization_code":"","client_id":"", "user_id":"", "redirect_uri":"", "expires":"", "scope":""}'

//OAuth refresh tokens structure
set oauth_access_tokens: '{"refresh_token":"","client_id":"", "user_id":"", "expires":"", "scope":""}'

//OAuth users structure
set oauth_users:user_123 '{"username":"user_123", "password":"user_password", "first_name":"Jorge", "last_name":"Castro"}'

//OAuth scopes structure
set oauth_scopes:default '{"scope":"default", "is_default":true}'

//OAuth JWT structure
set oauth_jwt:client_id_123 '{"client_id":"client_id", "subjects":"", "public_key":""}'

Step 6: Test OAuth 2.0

First, let's take a look to Redis just to know that our user is in there:

In Redis client type this:
~ get oauth_clients:client_id_123

Redis should return something like this:
















Ok, so our user is in there, let's request OAuth

Open your browser then copy this:

Method: Get
http://localhost/OAuth-2.0-server-with-redis/authorize.php?response_type=code&client_id=client_id_123&state=xyz&scope=default
Then you will see something like this:












This mean that OAuth it's waiting for an authorization, so when you press "yes" OAuth will create a Authorization Code (code). 




Now let's see Redis just to make sure that our authorization code is in there, do it fast because authorization codes expires in 15 seg :)

~ get oauth_authorization_codes:{authorization_code}










Once we have an authorization code we are ready to get an Access Token.
Let's use Curl to test or OAuth token endpoint. 


(Do it fast, authorization codes expires in 15 seg)
Method: Post
~ curl -u client_id_123:client_password http://localhost:80/OAuth-2.0-server-with-redis/token.php -d 'grant_type=authorization_code&code=YOUR_CODE'

Response:
















Finally, let's test our access_token and wait for OAuth's authorization:

Method: Post
~ curl http://localhost:80/OAuth-2.0-server-with-redis/resource.php -d
'access_token=YOUR_TOKEN'

Response:








And that's it, with the steps above, you will implement OAuth 2.0 connected with Radis for sure 

Cheers :)


Comments

Popular posts from this blog

Configurar y desplegar una Web API en Azure App Service Environment

Patrones de diseño para aplicaciones de alta disponibilidad en Azure - Resilient Applications (Parte I: Retry Policy)

Conectar .NET Web API con Azure API Management

Despliegue de contenedores Docker a Azure Container Services usando Visual Studio y .NET Core

Despliegue de contenedores Docker a Azure Web Apps