Custom Third Party OAuth2 Connections with Auth0

December 20, 2015
software auth0 development javascript authentication

Understanding Auth0 authentication

Before we start with creating a custom OAuth2 connection in Auth0, it’s worth to spend some time understanding the OAuth2 authentication process in Auth0. An OAuth2 authentication process starts with the application requesting Auth0 for authentication and ends with either the browser or the application server having an access token to represent the user. The access token can then be used to call the third party API on behalf of the user.

Pop-up mode Auth0 popup mode

Redirect mode Auth0 redirect mode

In both authentication modes, the process begins with the browser directing to Auth0 for authentication via the Auth0 Lock (Step 1). Auth0 will then request an authentication to the third party provider, via the third party’s Authorization URL (Step 2).

After verifying the user by asking the user to log in (Step 3), the third party will return a code to Auth0’s Callback URL configured in the third party’s application setting (Step 4). Upon receiving the code, Auth0 will now exchange it for an access_token by making a POST request to the third party’s Token URL (Step 5) and receive the access_token at the Callback URL (Step 6).

Once Auth0 has the user’s access_token, it would return a id_token corresponding to the user to the application. This is where the pop-up mode and redirect mode differ. In pop-up mode, the id_token will be sent to the browser by calling a callback function, which eventually saves the id_token in the browser’s local storage (Step 7 of pop-up mode).

In redirect mode, another sequence of code and id_token exchange happens between Auth0 and the application server, where Auth0 sends a code to the application server (Step 7 of redirect mode). The application server now needs to exchange the code for the id_token (Step 8 and 9).

Setting up a custom OAuth2 connection in Auth0

  1. Create a new Auth0 application
  1. Register a new developer application with third party

Checkpoint

By now, you should have the following ids/keys and secrets noted down and ideally saved as environment variables for later use:

And the following URLs:

  1. Create a custom connection to third party with Auth0

Checkpoint

At this point, you’d have your Auth0 client set up, third party application set up and a connection set up between Auth0 and the third party.

Using the custom OAuth2 connection

Now that the connection is set up, let’s use it in an application to get a user token. There are 2 ways of using the custom connection, depending on where you need the user token.

  1. Pop-up mode In popup mode, the application will request an authentication to Auth0 and the end result will be that Auth0 provides a user token to the application which can then be saved in the browser’s local storage. This method is suitable for use by a single page application to call the third party API directly on the browser. Sample code for popup mode

  2. Redirect mode The user token will be sent to the application server after a series of code and token exchanges between the application, Auth0 and the third party provider. This is suitable when you need the server to make API requests on behalf of the user. Sample code for redirect mode

Stubbing Dependencies in Go

A quick guide to stubbing dependencies in Go
software development go test tdd

Decorating React Components Using Higher Order Components and Inheritance Inversion

March 5, 2017
react higher order component javascript inheritance inversion

GRPC vs REST on Node.js

Performance comparsion between GRPC and REST on Node.js
grpc node.js javascript