OpenID Connect

Learning Objectives

Lesson

OAuth deals specifically with authorization. OpenID Connect (OIDC) is a protocol which is built on top of OAuth 2.0 and focusses on user authentication. It is widely used to enable user logins on websites and mobile apps.

Open ID Connect flow illustrated in a sequence diagram

OIDC allows apps to verify the identity of the end user and obtain basic profile information such as name and email address. This profile information is held within another JWT known as the ID token.

Use https://jwt.io to find out the name and email hidden in this JWT ID token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkZyZWQgRmxpbnRzdG9uZSIsImVtYWlsIjoiZnJlZC5mbGludHN0b25lQHdoaXRlaGF0Lm9yZy51ayIsImlhdCI6MTUxNjIzOTAyMn0.DlHfHG2qZXpWszZv-X8LwoQkZUlqVgaXoRmnHXE2y_I

Implementing authentication using Auth0

In this lesson we will store our usernames and passwords in Auth0. This is not strictly necessary as Auth0 supports connecting to other datasources to vaidate credentials, however, doing so simplifies our implementation.

Adding usernames & passwords to Auth0

Navigate to User Management->Users and choose to Create User. Enter the credentials and click Create. Repeat for any other users you have. Note that Auth0 will take care of hashing the passwords for you.

Creating a login page

Auth0's login page can be integrated into many different types of apps including mobile, single page apps (SPAs) and regular web apps. For this lesson we will choose to add login/logout and profile functionality to an SPA written using React.

  1. Using the Auth0 Dashboard, navigate to Applications and choose to Create Application. Choose Single Page Web Applications.

    creating a new SPA application using the Auth0 dashboard

  2. Follow the wizard and select React as your technology

  3. Click on Download Sample

    You will be instructed make some configuration changes to the application which will be created. Don't worry about these changes just yet, just click the Download button.

    You should now see a new application called My App in the Auth0 Dashboard and code for a sample SPA should have been downloaded as a zip file. You can read more about how the sample web app works here.

  4. Add the following configuration to My App

    configSPAApp

    This is required to support Cross-Origin Resource Sharing (CORS) and to enable Auth0 to redirect back to a web page after successful login/logout.

  5. Extract the zip and import the code into VSCode

  6. The README.md file walks you through the steps to run your application. Note that there is no need to confgure credentials as the src/auth_config.json file is already configured with your specific Auth0 details.

  7. Add auth_config.json to your .gitignore to avoid details of your Auth0 account being saved to git.

  8. Follow the README instructions to start your application, then navigate to http://localhost:3000. The following web page should be displayed.

    generated sample app home page

  9. Clicking "Login" will display the Auth0 Universal Login page

    Auth0 universal login

  10. Enter the username and password you added to Auth0 and you should now be successfully logged in

    loginCompleteOIDC

  11. From the drop down in the top right corner, select Profile to view your user profile information. This information is held in a JWT ID token.

    Profile information

    Well done, you have successfully authenticated a user using OIDC!

Assignment

In the previous lesson you used OAuth to secure your Messages API. In this lesson, you used OIDC to implement user authentication and add login/logout functionality to a web app. Now we need to connect the two parts and allow our SPA to call the Messages API.

There is already a link to call an External API (a dummy Ping API)in your sample SPA, try it out and locate the JavaScript code which executes the API call.

Modify this code to call your Messages API and display your messages. Hint - you will need to modify the audience value.

If you need any additional help, please refer to the video resources in the "Additional resources" section.

One the integration is complete, commit your code to GitHub and notify your coach that this assignment is complete.

Assignment extension task

Extend your SPA to allow addition and deletion of messages

Additional Resources