OpenID Connect Client

6.8.3 · active · verified Sun Apr 19

openid-client is an OpenID Certified JavaScript client library designed for implementing OAuth 2.0 and OpenID Connect flows. It offers a comprehensive API for common authentication and authorization patterns, including Authorization Code, Refresh Token, Device Authorization, Client-Initiated Backchannel Authentication (CIBA), and Client Credentials grants. The library also supports advanced features like Demonstrating Proof-of-Possession (DPoP), Token Introspection and Revocation, Pushed Authorization Requests (PAR), and various JWT Secured operations (JAR, JARM, UserInfo). It is built for a wide range of JavaScript runtimes, including Node.js, browsers, Deno, and Cloudflare Workers. Currently at version 6.8.3, openid-client is actively maintained with a regular release cadence, ensuring compliance with the latest protocol specifications. A key differentiator is its OpenID Certification for Basic, FAPI 1.0, and FAPI 2.0 Relying Party Conformance Profiles, guaranteeing high standards of protocol interoperability.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to discover an OpenID Provider, register a client, and generate an authorization URL for the Authorization Code Flow with PKCE and OIDC.

import { Issuer, generators } from 'openid-client';

const main = async () => {
  const issuerUrl = process.env.OIDC_ISSUER_URL ?? 'https://accounts.google.com';
  const clientId = process.env.OIDC_CLIENT_ID ?? 'YOUR_CLIENT_ID';
  const clientSecret = process.env.OIDC_CLIENT_SECRET ?? 'YOUR_CLIENT_SECRET';
  const redirectUri = process.env.OIDC_REDIRECT_URI ?? 'http://localhost:3000/callback';

  try {
    // Discover the OpenID Provider's configuration
    const googleIssuer = await Issuer.discover(issuerUrl);
    console.log('Discovered issuer: %s %O', googleIssuer.issuer, googleIssuer.metadata);

    // Register a new client with the issuer
    const client = new googleIssuer.Client({
      client_id: clientId,
      client_secret: clientSecret,
      redirect_uris: [redirectUri],
      response_types: ['code'],
    });

    // Generate parameters for the authorization request
    const code_verifier = generators.codeVerifier();
    const code_challenge = generators.codeChallenge(code_verifier);
    const state = generators.state();
    const nonce = generators.nonce();

    const authorizationUrl = client.authorizationUrl({
      scope: 'openid email profile',
      code_challenge,
      code_challenge_method: 'S256',
      state,
      nonce,
      redirect_uri: redirectUri,
    });

    console.log(`
Navigate to this URL to start the login flow:\n${authorizationUrl}
`);

  } catch (error) {
    console.error('Error during OpenID Connect setup:', error);
  }
};

main();

view raw JSON →