What is OpenID Connect (OIDC)?

David Lin
3 min readJan 24, 2023

--

Authentication is a crucial aspect of web development, and it can often be a complex and confusing topic for junior developers. One of the more modern and widely used approaches to authentication is OpenID Connect (OIDC) version 2. In this article, we’ll go over the basics of OIDC and explain how it works, so you can have a better understanding of how to handle user authentication in your web projects.

OpenID Connect vs OAuth2.0

First of all, it’s important to understand that OIDC is built on top of OAuth 2.0, but it adds an additional layer of functionality that allows for user authentication. While OAuth 2.0 is primarily used for authorization, OIDC is used for both authentication and authorization.

Basic Flow of OpenID Connect

The basic flow of an OIDC authentication involves three main entities: the client, the OpenID provider, and the user. The client is the application that the user is trying to access. The OpenID provider is a separate service that is responsible for handling user authentication. When a user wants to access the client, they will first be redirected to the OpenID provider to sign in. Once the user has successfully signed in, they will be redirected back to the client, and an ID token will be provided, which can be used to authenticate the user for future requests.

There are different types of grants and token types that are used in OIDC. The authorization code grant is the most commonly used, and it’s used to get an ID token and an access token. The ID token is a JWT (JSON Web Token) that contains information about the user, such as their name and email address. The access token is used to access resources on the client’s behalf.

When implementing OIDC, it is important to handle user authentication with redirects and use best security practices, like validating the signature of the ID token and using https to prevent eavesdropping and tampering of messages.

During an OIDC flow, different types of errors can happen, for example, when the client requests an invalid or unsupported scope. It is important to handle these errors correctly and show appropriate error messages to the user.

Code Snippets

There are multiple packages available to leverage on for implementing OIDC in your web applications.

JavaScript

  • oidc-client.js
  • auth0.js

Python

  • PyOIDC
  • Authlib

Java

  • Spring Security OIDC
  • Pac4j

Here is an example implementation of OpenID Connect (OIDC) in a web project using the JavaScript library oidc-client.js:

First, you’ll need to install the library:

npm install oidc-client

Then, in your JavaScript file, you can configure the OIDC client:

import createAuth0Client from '@auth0/auth0-spa-js';

const config = {
domain: 'yourdomain.auth0.com',
client_id: 'yourclientid',
redirect_uri: 'http://localhost:3000/callback',
audience: 'https://yourdomain.auth0.com/userinfo'
};
const auth0 = await createAuth0Client(config);

In this example, we are importing the createAuth0Client function from the oidc-client library and using it to create an instance of the OIDC client. We pass in an object containing our Auth0 account's domain, client ID, redirect URI, and audience as the configuration object.

Next, in your login page, you can call the loginWithRedirect method to redirect the user to the Auth0 login page:

auth0.loginWithRedirect();

And, in your callback page you can call the handleRedirectCallback method to handle the redirect from Auth0 after the user logs in:

const user = await auth0.handleRedirectCallback();

In the above example, handleRedirectCallback will parse the response from Auth0 and extract the user's information and tokens. With the user information you can handle the user session and authorize the user to access certain parts of your application.

This is a basic example, and you might need to add additional logic, error handling, and adjust the implementation based on your specific requirements. This example uses Implicit grant flow, for more security sensitive scenarios using PKCE flow with createAuth0Client and using the method loginWithPopup or loginWithRedirect are recommended.

Thank you for taking the time to read this! I hope that this article has helped you to gain a better understanding of these technologies and how they can be used to protect user data and provide a seamless experience for your users. If you have any questions or would like to learn more, please don’t hesitate to reach out.
To see what I’m working on, visit my
GitHub or my personal website.

--

--

David Lin
David Lin

Written by David Lin

Aspiring Software Engineer, building for a better future!

No responses yet