> ## Documentation Index
> Fetch the complete documentation index at: https://docs-dev-docs-event-stream-action-templates.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP：Auth0-PHPを使用したJWT（JSON Web Token）の検証

> PHPアプリケーション内にJWT（JSON Web Token）検証を統合して、トークンを解析、検証し、妥当性を確認します。

Auth0 PHP SDKは、<Tooltip data-tooltip-id="react-containers-DefinitionTooltip-2" href="/docs/ja-jp/glossary?term=json-web-token" tip="JSON Web Token（JWT）: 二者間のクレームを安全に表現するために使用される標準IDトークン形式（および多くの場合、アクセストークン形式）。" cta="用語集の表示">JSON Web Token</Tooltip>（JWT）の処理に使用される`Auth0\SDK\Token`クラスを提供しています。これにより、アプリケーションで使用するためにトークンのデコード、検証、および妥当性の確認が行えます。JWTについての詳細と、それらの構築およびデコードの方法は、「[jwt.io](https://jwt.io/)」でご覧いただけます。

このクラスは、HS256とRS256の両方を処理することができます。どちらのタイプも、処理を行う前にSDKでアルゴリズムと有効なオーディエンスを構成する必要があります。HS256トークンは、クライアントシークレットを構成する必要があります。RS256トークンは、認可済み発行者が必要で、デコードする過程でJWKファイルの取得に使用されます（[署名アルゴリズムの詳細はこちら](https://auth0.com/blog/navigating-rs256-and-jwks/)）。

## 前提条件

以下のドキュメントは、「[PHP：入門ガイド](/docs/ja-jp/libraries/auth0-php)」の手順を終えていることを前提としており、そこで提供されたコードを使用します。

## 使用例

以下は、SDKの`Token`クラスに基づいた、小さなURLベースのJSON Web Tokenプロセッサーの例です。

```php lines theme={null}
<?php

// Import the Composer Autoloader to make the SDK classes accessible:
require 'vendor/autoload.php';

// Load our environment variables from the .env file:
(Dotenv\Dotenv::createImmutable(__DIR__))->load();

$token = filter_var($_GET['token'] ?? null, FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);
$algorithm = filter_var($_GET['algorithm'] ?? 'HS256', FILTER_UNSAFE_RAW, FILTER_NULL_ON_FAILURE);

if ($token === null) {
    die('No `token` request parameter.');
}

if (! in_array($algorithm, ['HS256', 'RS256'])) {
    die('Invalid `algorithm` supplied.');
}

// The Auth0 SDK includes a helpful token processing utility we'll leverage for this:
$token = new \Auth0\SDK\Token([
    'domain' => $env['AUTH0_DOMAIN'],
    'clientId' => $env['AUTH0_CLIENT_ID'],
    'clientSecret' => $env['AUTH0_CLIENT_SECRET'],
    'tokenAlgorithm' => $algorithm
], $token, \Auth0\SDK\Token::TYPE_ID_TOKEN);

// Verify the token: (This will throw an \Auth0\SDK\Exception\InvalidTokenException if verification fails.)
$token->verify();

// Validate the token claims: (This will throw an \Auth0\SDK\Exception\InvalidTokenException if validation fails.)
$token->validate();

echo '<pre>';
print_r($token->toArray(), true);
echo '</pre>';
```

`verify()`と`validate()`の両方が、動作をカスタマイズするために使用できる多数のオプション引数を提供します。これには、<Tooltip data-tooltip-id="react-containers-DefinitionTooltip-0" href="/docs/ja-jp/glossary?term=nonce" tip="Nonce: リプレイ攻撃を検出および防止するために、認証プロトコルで1回だけ発行される任意の数値。" cta="用語集の表示">nonce</Tooltip>クレームの検証、トークンの`auth_time`からの最大経過時間の制限、時間チェックのための`leeway`クロック許容誤差などが含まれます。これらのメソッドには、これらのオプションを確認するために、ソースコードまたはお好みのIDEを通じて完全にコメントが付けられています。

## もっと詳しく

* [PHP：Auth0-PHPを使用したログイン、ログアウト、ユーザープロファイルの返送](/docs/ja-jp/libraries/auth0-php/auth0-php-basic-use)
* [PHP：Auth0-PHPでAuthentication APIを使用する](/docs/ja-jp/libraries/auth0-php/using-the-authentication-api-with-auth0-php)
* [PHP：Auth0-PHPでManagement APIを使用する](/docs/ja-jp/libraries/auth0-php/using-the-management-api-with-auth0-php)
* [PHPAuth0-PHP統合のトラブルシューティング](/docs/ja-jp/libraries/auth0-php/troubleshoot-auth0-php-library)
