oidc-client、oidc-client-ts、openid-client、oidc-provider、react-oidc-context は、OpenID Connect (OIDC) 認証を実装するための主要なライブラリですが、それぞれ役割と実行環境が明確に異なります。oidc-client はブラウザ向けの従来のライブラリですが、現在は oidc-client-ts への移行が推奨されています。openid-client は Node.js サーバー環境での認証フロー(Confidential Client)に特化しています。oidc-provider は認証サーバー自体を構築するためのライブラリであり、クライアント側ではありません。react-oidc-context は、これらのクライアントライブラリを React アプリケーションで簡単に利用するためのラッパーです。本稿では、これらの違いを明確にし、プロジェクトのアーキテクチャに適した選定基準を提供します。
JavaScript で認証システムを構築する際、OpenID Connect (OIDC) 標準をサポートするライブラリの選択はセキュリティとアーキテクチャに直結する重要な決定です。提示された 5 つのパッケージは、名前が似ていても「クライアント側かサーバー側か」「認証を行う側か提供するか」「フレームワーク依存か」という点で明確な役割分担があります。これらを混同すると、セキュリティホールや不適切なアーキテクチャにつながるため、それぞれの特性を深く理解する必要があります。
最も重要な点は、これらがどの環境で動作し、どの役割を担うかです。大きく分けて「ブラウザクライアント」、「サーバーサイドクライアント」、「認証サーバー提供者」、「フレームワークラッパー」の 4 種類に分類できます。
oidc-client-ts と oidc-client は、ブラウザ上で動作する「Public Client」向けのライブラリです。シークレットを保持できない環境のため、PKCE (Proof Key for Code Exchange) 拡張を用いた Authorization Code Flow を使用します。
// oidc-client-ts: ブラウザでの設定例
import { UserManager } from "oidc-client-ts";
const userManager = new UserManager({
authority: "https://accounts.google.com",
client_id: "your-client-id",
redirect_uri: "https://yourapp.com/callback",
response_type: "code",
scope: "openid profile email",
post_logout_redirect_uri: "https://yourapp.com/logout"
});
// ログイン開始
await userManager.signinRedirect();
openid-client は、Node.js 環境で動作する「Confidential Client」向けのライブラリです。サーバー側でクライアントシークレットを安全に保持できるため、より高い信頼性が求められるバックエンド連携に適しています。
// openid-client: Node.js サーバーでの設定例
import { generators, Issuer } from 'openid-client';
const issuer = await Issuer.discover('https://accounts.google.com');
const client = new issuer.Client({
client_id: 'your-client-id',
client_secret: 'your-client-secret', // サーバー側で管理
redirect_uris: ['https://yourapp.com/callback'],
response_types: ['code']
});
const code_verifier = generators.codeVerifier();
const code_challenge = generators.codeChallenge(code_verifier);
const authUrl = client.authorizationUrl({
scope: 'openid profile',
code_challenge,
code_challenge_method: 'S256'
});
oidc-provider は、これらクライアントが接続する「認証サーバー」そのものを実装するためのライブラリです。自前でユーザーデータベースを持ち、トークンを発行する立場になります。
// oidc-provider: 認証サーバーの実装例 (Koa/Express 等)
import Provider from 'oidc-provider';
const configuration = {
clients: [
{
client_id: 'client-id',
client_secret: 'client-secret',
redirect_uris: ['https://client-app.com/callback']
}
],
scopes: ['openid', 'profile'],
features: {
registration: { enabled: true }
}
};
const provider = new Provider('https://your-identity-server.com', configuration);
// provider.listen(3000); // 認証サーバーとして起動
react-oidc-context は、これらのロジックを React のコンテキスト API でラップし、コンポーネント内で useAuth() などのフックを使えるようにする橋渡し役です。
// react-oidc-context: React での利用例
import { AuthProvider, useAuth } from 'react-oidc-context';
const authConfig = {
authority: "https://accounts.google.com",
client_id: "your-client-id",
redirect_uri: "https://yourapp.com/callback",
scope: "openid profile"
};
function App() {
return (
<AuthProvider {...authConfig}>
<Dashboard />
</AuthProvider>
);
}
function Dashboard() {
const auth = useAuth();
if (auth.isLoading) return <div>Loading...</div>;
if (auth.error) return <div>Error: {auth.error.message}</div>;
if (!auth.isAuthenticated) return <button onClick={auth.signinRedirect}>Login</button>;
return <div>Hello {auth.user?.profile.name}</div>;
}
oidc-client vs oidc-client-ts: 移行の必要性oidc-client は長年標準として使われてきましたが、現在はメンテナンスモードに入っており、新規プロジェクトでの使用は避けるべきです。oidc-client-ts はその公式な後継であり、TypeScript で完全に書き直されています。
oidc-client (非推奨)
// oidc-client: 古い書き方
import { UserManager } from 'oidc-client';
const mgr = new UserManager(settings);
mgr.signinRedirect();
oidc-client-ts (推奨)
// oidc-client-ts: 現在の標準
import { UserManager } from 'oidc-client-ts';
const mgr = new UserManager(settings);
await mgr.signinRedirect();
既存の oidc-client 実装がある場合、API はほぼ互換性がありますが、型定義の扱いや一部の非同期処理の戻り値が異なるため、移行時にはテストが必須です。
ライブラリ選びは、そのままトークンの保管場所とセキュリティモデルの選択になります。
ブラウザクライアント (oidc-client-ts, react-oidc-context)
// oidc-client-ts: PKCE は自動処理されるが、設定で明示可能
const settings = {
// ...
response_type: "code", // Implicit ('token') は使用しない
// pkce_method: 'S256' // デフォルトで S256 が使用される
};
サーバーサイドクライアント (openid-client)
// openid-client: サーバー側でコードをトークンに交換
const params = client.callbackParams(req);
const tokenSet = await client.callback('https://yourapp.com/callback', params, { code_verifier });
// tokenSet.access_token をサーバー側セッションで管理
認証サーバー (oidc-provider)
// oidc-provider: 署名鍵の管理が重要
const configuration = {
keys: [
{
kid: 'my-key-id',
kty: 'RSA',
use: 'sig',
// 秘密鍵の管理は環境変数や HSM 経由で行うべき
crv: 'RS256',
n: '...',
e: '...',
d: '...',
}
]
};
認証状態の維持と更新(サイレントリニュー)は、ユーザー体験に直結します。
oidc-client-ts / react-oidc-context
// oidc-client-ts: サイレントリニュー設定
const settings = {
// ...
automaticSilentRenew: true,
// 最近では refreshToken 使用が推奨されるケースが多い
useRefreshToken: true,
scope: "openid profile offline_access"
};
openid-client
// openid-client: リフレッシュトークンでの更新
if (tokenSet.expired() || needsRefresh(tokenSet)) {
const newTokenSet = await client.refresh(tokenSet.refresh_token);
// セッションストアを更新
}
プロジェクトの要件に応じて、以下の基準で選択を行います。
| 要件 | 推奨パッケージ | 理由 |
|---|---|---|
| React SPA (新規) | react-oidc-context + oidc-client-ts | React 統合が容易で、基盤ライブラリも最新のため。 |
| Vanilla JS SPA | oidc-client-ts | 框架に依存せず、型安全な標準実装のため。 |
| Node.js Backend | openid-client | シークレット管理とサーバー側セッション処理に最適化されているため。 |
| 自前認証サーバー | oidc-provider | 認証ロジックを完全に自作・制御する必要があるため。 |
| 既存レガシー維持 | oidc-client | 移行コストがリスクを上回る場合のみ。新規は不可。 |
認証はアプリケーションの心臓部です。ライブラリを選ぶ際は、単に「使えるか」だけでなく、「メンテナンスされ続けているか」「セキュリティ標準に準拠しているか」を最優先してください。
フロントエンド開発者が最も頻繁に選ぶべき組み合わせは、oidc-client-ts (またはそれをラップした react-oidc-context)です。oidc-client からの移行は避けられない流れであり、TypeScript 環境での開発体験とセキュリティの両面で優れています。
一方で、バックエンドエンジニアが API サーバーの認証を担う場合は openid-client が唯一の正解です。ブラウザとサーバーでライブラリを混在させないよう、アーキテクチャ図面上で境界を明確にすることが重要です。
もし「認証サーバー自体を作りたい」という要件であれば、oidc-provider が出番ですが、これは認証専門のエンジニアリングを要求されるため、通常は Auth0 や Cognito などのマネージドサービスの利用を先に検討すべきです。
適切なツールを適切な場所に配置することで、堅牢でメンテナンスしやすい認証システムを構築できます。
新規プロジェクトでの使用は推奨されません。このパッケージはメンテナンスモードに入っており、既存のレガシーなブラウザベースの SPA を維持する場合にのみ検討してください。セキュリティアップデートや新機能のサポートは oidc-client-ts に移行しています。
ブラウザ上で動作する SPA(Single Page Application)で、外部の認証サーバー(Auth0, Azure AD, Keycloak など)を利用する場合に選択します。TypeScript で書かれており、モダンな PKCE フローをサポートするため、新規のフロントエンド認証実装の標準となります。
自社で認証サーバー(Identity Provider)を構築・運用する必要がある場合に選択します。他社の認証サービスを使わず、ユーザー管理やトークン発行のロジックを完全に制御したいバックエンドエンジニアリング向けのライブラリです。
Node.js で動作するサーバーサイドアプリケーション(SSR, API サーバー)が、外部認証サーバーと連携する場合に選択します。クライアントシークレットを安全に扱える環境(Confidential Client)での認証フローに適しています。
React アプリケーションで認証状態を管理し、コンポーネント内で簡単にユーザー情報やログイン機能を利用したい場合に選択します。内部で oidc-client-ts などをラッパーしており、認証ロジックのカプセル化とコードの簡素化を目的とします。
Library to provide OpenID Connect (OIDC) and OAuth2 protocol support for client-side, browser-based JavaScript client applications. Also included is support for user session and access token management.
Node.js v4.4 or later required.
npm install oidc-client --save
NOTE: if you're not already using babel-polyfill make sure you run
npm install --save babel-polyfill as well. Then include it in your build.
If you don't use a package manager or a module loader, then you can get the library from the dist folder on github here.
If you intend to use this library directly in a browser and are not using UMD/AMD then there is a compiled version in the ~/dist folder. It is already bundled/minified and contains the necessary dependencies and polyfills (mainly for ES6 features such as Promises).
If you are using UMD/AMD and/or you already have included an ES6 polyfill (such as babel-polyfill.js) then you can include the UMD packaged version of the file from the ~/lib folder.
git clone https://github.com/IdentityModel/oidc-client-js.git
cd oidc-client-js
npm install
npm run build
npm start
and then browse to http://localhost:15000.
npm test
Some initial docs are here.
All are welcome on the issue tracker.