概要
JWT の署名を検証せずに JWT をデコードします。このポリシーは、JWT の署名を検証する前に JWT 内のクレームの値を把握しなければならない場合に、Verify JWT ポリシーと一緒に使用すると便利です。
Decode JWT ポリシーは、JWT を署名するために使用したアルゴリズムに関係なく動作します。詳細については、JWS ポリシーと JWT ポリシーの概要をご覧ください。
動画
JWT をデコードする方法について解説する動画をご覧ください。
サンプル: JWT をデコードする
以下に示すポリシーでは、フロー変数 var.jwt 内の JWT をデコードします。この変数が存在し、かつ有効(デコード可能)な JWT が含まれている必要があります。このポリシーは、JWT をあらゆるフロー変数から得ることができます。
<DecodeJWT name="JWT-Decode-HS256"> <DisplayName>JWT Verify HS256</DisplayName> <Source>var.jwt</Source> </DecodeJWT>
ポリシーは出力をコンテキスト変数に書き込み、後続のポリシーまたは API プロキシの条件でその値を調べられるようにします。このポリシーの変数のリストについては、フロー変数をご覧ください。
Decode JWT の要素リファレンス
このポリシー リファレンスでは、Decode JWT ポリシーの要素と属性について説明します。
最上位の要素に適用される属性
<DecodeJWT name="JWT" continueOnError="false" enabled="true" async="false">
次の属性は、すべてのポリシーの親要素に共通です。
| 属性 | 説明 | デフォルト | 要否 |
|---|---|---|---|
| name |
ポリシーの内部名。名前に使用できる文字は A-Z0-9._\-$ % のみです。ただし、Edge 管理 UI では、英数字以外の文字を自動的に削除するなど、追加の制限が適用されます。必要に応じて、 |
なし | 必須 |
| continueOnError |
ポリシーが失敗した場合にエラーを返すには、false に設定します。これはほとんどのポリシーで想定される動作です。
ポリシーが失敗してもフロー実行を続行するには、 |
false | 省略可 |
| enabled |
ポリシーを適用するには true に設定します。
ポリシーを「turn off」するには、 |
true | 省略可 |
| async | この属性は非推奨となりました。 | false | 非推奨 |
<DisplayName>
<DisplayName>Policy Display Name</DisplayName>
name 属性に加えて、管理 UI プロキシ エディタのポリシーに別の自然言語名でラベルを付けるために使います。
| デフォルト | この要素を省略した場合、ポリシーの name 属性の値が使用されます。 |
| 要否 | 省略可 |
| 型 | 文字列 |
<Source>
<Source>jwt-variable</Source>
この要素が存在する場合、デコード対象の JWT が検出されるとポリシーが想定するフロー変数を指定します。
| デフォルト | request.header.authorization(デフォルトに関する重要な情報については、上記の注をご覧ください)。 |
| 要否 | 省略可 |
| 型 | 文字列 |
| 有効な値 | Edge のフロー変数名 |
Flow variables
Upon success, the Verify JWT and Decode JWT policies set context variables according to this pattern:
jwt.{policy_name}.{variable_name}
For example, if the policy name is jwt-parse-token , then the policy will store
the subject specified in the JWT to the context variable named jwt.jwt-parse-token.decoded.claim.sub.
(For backward compatibility, it will also be available in jwt.jwt-parse-token.claim.subject)
| Variable name | Description |
|---|---|
claim.audience |
The JWT audience claim. This value may be a string, or an array of strings. |
claim.expiry |
The expiration date/time, expressed in milliseconds since epoch. |
claim.issuedat |
The Date the token was issued, expressed in milliseconds since epoch. |
claim.issuer |
The JWT issuer claim. |
claim.notbefore |
If the JWT includes a nbf claim, this variable will contain the value, expressed in milliseconds since epoch. |
claim.subject |
The JWT subject claim. |
claim.name |
The value of the named claim (standard or additional) in the payload. One of these will be set for every claim in the payload. |
decoded.claim.name |
The JSON-parsable value of the named claim (standard or additional) in the payload. One variable is set for
every claim in the payload. For example, you can use decoded.claim.iat to
retrieve the issued-at time of the JWT, expressed in seconds since epoch. While you
can also use the claim.name flow variables, this is the
recommended variable to use to access a claim. |
decoded.header.name |
The JSON-parsable value of a header in the payload. One variable is set for
every header in the payload. While you can also use the header.name flow variables,
this is the recommended variable to use to access a header. |
expiry_formatted |
The expiration date/time, formatted as a human-readable string. Example: 2017-09-28T21:30:45.000+0000 |
header.algorithm |
The signing algorithm used on the JWT. For example, RS256, HS384, and so on. See (Algorithm) Header Parameter for more. |
header.kid |
The Key ID, if added when the JWT was generated. See also "Using a JSON Web Key Set (JWKS)" at JWT policies overview to verify a JWT. See (Key ID) Header Parameter for more. |
header.type |
Will be set to JWT. |
header.name |
The value of the named header (standard or additional). One of these will be set for every additional header in the header portion of the JWT. |
header-json |
The header in JSON format. |
is_expired |
true or false |
payload-claim-names |
An array of claims supported by the JWT. |
payload-json |
The payload in JSON format.
|
seconds_remaining |
The number of seconds before the token will expire. If the token is expired, this number will be negative. |
time_remaining_formatted |
The time remaining before the token will expire, formatted as a human-readable string. Example: 00:59:59.926 |
valid |
In the case of VerifyJWT, this variable will be true when the signature is verified, and
the current time is before the token expiry, and after the token notBefore value, if they
are present. Otherwise false.
In the case of DecodeJWT, this variable is not set. |
エラー リファレンス
このセクションでは、このポリシーによってエラーがトリガーされたときに返される障害コードとエラー メッセージ、Edge によって設定される障害変数について説明します。これは、障害に対処する障害ルールを作成するうえで重要な情報です。詳細については、ポリシーエラーについて知っておくべきことと障害の処理をご覧ください。
ランタイム エラー
このエラーは、ポリシーの実行時に発生することがあります。
| 障害コード | HTTP ステータス | 原因 | 解決方法 |
|---|---|---|---|
steps.jwt.FailedToDecode |
401 | ポリシーが JWT をデコードできない場合に発生します。JWT が無効か、形式が正しくないか、それ以外の理由でデコードできない可能性があります。 | build |
steps.jwt.FailedToResolveVariable |
401 | ポリシーの <Source> 要素で指定されたフロー変数が存在しない場合に発生します。 |
|
steps.jwt.InvalidToken |
401 | ポリシーの <Source> 要素で指定されたフロー変数が範囲外であるか、解決できない場合に発生します。 |
build |
デプロイエラー
以下のエラーは、このポリシーを含むプロキシをデプロイするときに発生することがあります。
| エラー名 | 原因 | 修正 |
|---|---|---|
InvalidEmptyElement |
デコードする JWT を含むフロー変数が、ポリシーの <Source> 要素で指定されていない場合に発生します。 |
build |
障害変数
ランタイム エラーが発生すると、次の変数が設定されます。詳細については、ポリシーエラーについて知っておくべきことをご覧ください。
| 変数 | 説明 | 例 |
|---|---|---|
fault.name="fault_name" |
fault_name は、上記のランタイム エラーの表に記載されている障害の名前です。障害コードの最後の部分が障害名になります。 | fault.name Matches "TokenExpired" |
JWT.failed |
障害の場合、すべての JWT ポリシーで同じ変数が設定されます。 | JWT.failed = true |
エラー レスポンスの例
ベスト プラクティスとして、エラー処理では、エラー レスポンスの errorcode の部分をトラップすることをおすすめします。faultstring のテキストには依存しないでください。この部分は変更される可能性があります。
障害ルールの例
<FaultRules>
<FaultRule name="JWT Policy Errors">
<Step>
<Name>JavaScript-1</Name>
<Condition>(fault.name Matches "TokenExpired")</Condition>
</Step>
<Condition>JWT.failed=true</Condition>
</FaultRule>
</FaultRules>