Kebijakan DecodeJWT

Halaman ini berlaku untuk Apigee dan Apigee hybrid.

Lihat dokumentasi Apigee Edge.

Kebijakan DecodeJWT mendekode JWT tanpa memverifikasi tanda tangan pada JWT. Hal ini paling berguna saat digunakan bersama dengan kebijakan VerifyJWT, saat nilai klaim dari dalam JWT harus diketahui sebelum memverifikasi tanda tangan JWT.

Kebijakan JWT Decode berfungsi terlepas dari algoritma yang digunakan untuk menandatangani JWT. Lihat Ringkasan kebijakan JWS dan JWT untuk pengantar mendetail.

Kebijakan ini adalah Kebijakan standar dan dapat di-deploy ke jenis lingkungan apa pun. Untuk mengetahui informasi tentang jenis dan ketersediaan kebijakan dengan setiap jenis lingkungan, lihat Jenis kebijakan.

Contoh: Mendekode JWT

Kebijakan yang ditampilkan di bawah ini mendekode JWT yang ditemukan dalam variabel alur var.jwt. Variabel ini harus ada dan berisi JWT yang valid (dapat didekode). Kebijakan ini dapat memperoleh JWT dari variabel alur mana pun.

<DecodeJWT name="JWT-Decode-HS256">
    <DisplayName>JWT Verify HS256</DisplayName>
    <Source>var.jwt</Source>
</DecodeJWT>

Kebijakan menulis outputnya ke variabel konteks sehingga kebijakan atau kondisi berikutnya di proxy API dapat memeriksa nilai tersebut. Lihat Variabel alur untuk mengetahui daftar variabel yang ditetapkan oleh kebijakan ini.

Referensi elemen untuk Decode JWT

Referensi kebijakan menjelaskan elemen dan atribut kebijakan Decode JWT.

Atribut yang diterapkan ke elemen tingkat teratas

<DecodeJWT name="JWT" continueOnError="false" enabled="true" async="false">

Atribut berikut umum untuk semua elemen induk kebijakan.

Atribut Deskripsi Default Kehadiran
nama Nama internal kebijakan. Karakter yang dapat Anda gunakan dalam nama dibatasi menjadi: A-Z0-9._\-$ %. Namun, UI Apigee menerapkan batasan tambahan, seperti menghapus karakter yang bukan alfanumerik secara otomatis.

Secara opsional, gunakan elemen <displayname></displayname> untuk melabeli kebijakan di editor proxy UI Apigee dengan nama bahasa alami yang berbeda.

T/A Wajib
continueOnError Disetel ke false untuk menampilkan error saat kebijakan gagal. Hal ini adalah perilaku yang diharapkan untuk sebagian besar kebijakan.

Setel ke true agar eksekusi alur berlanjut meskipun setelah kebijakan gagal.

false Opsional
diaktifkan Setel ke true untuk menerapkan kebijakan.

Setel ke false untuk "menonaktifkan" kebijakan. Kebijakan tidak akan diterapkan meskipun tetap terlampir pada alur.

true Opsional
asinkron Atribut ini tidak digunakan lagi. false Tidak digunakan lagi

<DisplayName>

<DisplayName>Policy Display Name</DisplayName>

Gunakan selain atribut nama untuk memberi label kebijakan di editor proxy UI Apigee dengan nama bahasa alami yang berbeda.

Default Jika Anda menghapus elemen ini, nilai atribut nama kebijakan akan digunakan.
Kehadiran Opsional
Jenis String

<Source>

<Source>jwt-variable</Source>

Jika ada, menentukan variabel alur tempat kebijakan mengharapkan untuk menemukan JWT yang akan didekode.

Default request.header.authorization (Lihat catatan di atas untuk mengetahui informasi penting tentang default).
Kehadiran Opsional
Jenis String
Nilai yang valid Nama variabel alur Apigee

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.

Referensi error

This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.

Runtime errors

These errors can occur when the policy executes.

Fault code HTTP status Cause Fix
steps.jwt.FailedToDecode 401 Occurs when the policy is unable to decode the JWT. The JWT may be malformed, invalid or otherwise not decodable.
steps.jwt.FailedToResolveVariable 401 Occurs when the flow variable specified in the <Source> element of the policy does not exist.
steps.jwt.InvalidToken 401 Occurs when the flow variable specified in the <Source> element of the policy is out of scope or can't be resolved.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name Cause Fix
InvalidEmptyElement Occurs when the flow variable containing the JWT to be decoded is not specified in the <Source> element of the policy.

Variabel error

Variabel ini ditetapkan saat error runtime terjadi. Untuk mengetahui informasi selengkapnya, lihat Yang perlu Anda ketahui tentang error kebijakan.

Variabel Dari mana Contoh
fault.name="fault_name" fault_name adalah nama error, seperti yang tercantum dalam tabel Runtime errors di atas. Nama error adalah bagian terakhir dari kode error. fault.name Matches "InvalidToken"
JWT.failed Semua kebijakan JWT menetapkan variabel yang sama jika terjadi kegagalan. JWT.failed = true

Contoh respons error

Kode Error Kebijakan JWT

Untuk penanganan error, praktik terbaiknya adalah menangkap bagian errorcode dari respons error. Jangan mengandalkan teks di faultstring, karena teks tersebut dapat berubah.

Contoh aturan error

    <FaultRules>
        <FaultRule name="JWT Policy Errors">
            <Step>
                <Name>JavaScript-1</Name>
                <Condition>(fault.name Matches "InvalidToken")</Condition>
            </Step>
            <Condition>JWT.failed=true</Condition>
        </FaultRule>
    </FaultRules>