ถอดรหัสนโยบาย JWT

คุณกำลังดูเอกสารประกอบ Apigee Edge
ไปที่ เอกสารประกอบเกี่ยวกับ Apigee X.
ข้อมูล

อะไร

ถอดรหัส JWT โดยไม่ยืนยันลายเซ็นบน JWT ซึ่งจะมีประโยชน์ที่สุดเมื่อใช้ใน ร่วมกับนโยบาย VerifyJWT เมื่อต้องทราบมูลค่าของการอ้างสิทธิ์จากภายใน JWT ก่อนยืนยันลายเซ็นของ JWT

นโยบายการถอดรหัส JWT จะทำงานโดยไม่คำนึงถึงอัลกอริทึมที่ใช้ในการลงนาม JWT ดูรายละเอียดข้อมูลเบื้องต้นได้ที่ภาพรวมของนโยบายของ JWS และ JWT

วิดีโอ

ดูวิดีโอสั้นๆ เพื่อดูวิธีถอดรหัส JWT

ตัวอย่าง: ถอดรหัส JWT

นโยบายที่แสดงด้านล่างจะถอดรหัส JWT ที่พบในตัวแปรโฟลว์ var.jwt ช่วงเวลานี้ จะต้องแสดงตัวแปรและมี JWT ที่ทำงานได้ (ถอดรหัสได้) นโยบายสามารถรับ JWT ได้จาก ตัวแปรใดก็ตามของโฟลว์ได้

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

นโยบายจะเขียนเอาต์พุตไปยังตัวแปรบริบทเพื่อให้นโยบายหรือเงื่อนไขที่ตามมา ในพร็อกซี API จะสามารถตรวจสอบค่าเหล่านั้นได้ โปรดดูตัวแปรโฟลว์สำหรับ รายการตัวแปรที่กำหนดโดยนโยบายนี้

การอ้างอิงองค์ประกอบสำหรับการถอดรหัส JWT

ข้อมูลอ้างอิงของนโยบายอธิบายองค์ประกอบและแอตทริบิวต์ของนโยบาย Decode JWT

แอตทริบิวต์ที่ ใช้กับองค์ประกอบระดับบนสุด

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

แอตทริบิวต์ต่อไปนี้มีอยู่ในองค์ประกอบระดับบนสุดของนโยบายทั้งหมด

แอตทริบิวต์ คำอธิบาย ค่าเริ่มต้น การตรวจหาบุคคล
ชื่อ ชื่อภายในของนโยบาย อักขระที่คุณสามารถใช้ในชื่อจะถูกจำกัดไว้เฉพาะ: A-Z0-9._\-$ % อย่างไรก็ตาม UI การจัดการ Edge จะบังคับใช้เพิ่มเติม เช่น การนำอักขระที่ไม่ใช่ตัวอักษรและตัวเลขคละกันออกโดยอัตโนมัติ

(ไม่บังคับ) ใช้องค์ประกอบ <displayname></displayname> เพื่อ ติดป้ายกำกับนโยบายในเครื่องมือแก้ไขพร็อกซีของ UI การจัดการด้วยภาษาอื่นที่เป็นภาษาธรรมชาติ ชื่อ

ไม่มี ต้องระบุ
continueOnError ตั้งค่าเป็น false เพื่อแสดงผลข้อผิดพลาดเมื่อนโยบายล้มเหลว เป็นเรื่องปกติ พฤติกรรมสำหรับนโยบายส่วนใหญ่

ตั้งค่าเป็น true เพื่อให้ดำเนินการตามขั้นตอนได้อย่างต่อเนื่องแม้จะมีนโยบายแล้วก็ตาม ล้มเหลว

เท็จ ไม่บังคับ
เปิดใช้อยู่ ตั้งค่าเป็น true เพื่อบังคับใช้นโยบาย

ตั้งค่าเป็น false เป็น "ปิด" นโยบาย นโยบายนี้จะไม่บังคับใช้ แม้ว่าจะยังมีการเคลื่อนไหว

จริง ไม่บังคับ
ไม่พร้อมกัน แอตทริบิวต์นี้เลิกใช้งานแล้ว เท็จ เลิกใช้

&lt;DisplayName&gt;

<DisplayName>Policy Display Name</DisplayName>

ใช้เพิ่มเติมจากแอตทริบิวต์ชื่อเพื่อติดป้ายกำกับนโยบายในเครื่องมือแก้ไขพร็อกซี UI การจัดการ ด้วยชื่อที่เป็นภาษาธรรมชาติต่างกัน

ค่าเริ่มต้น หากคุณไม่ใส่องค์ประกอบนี้ ระบบจะใช้ค่าของแอตทริบิวต์ชื่อนโยบาย
การตรวจหาบุคคล ไม่บังคับ
ประเภท สตริง

&lt;Source&gt;

<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 อาจมีรูปแบบไม่ถูกต้อง ไม่ถูกต้อง หรือถอดรหัสไม่ได้
steps.jwt.FailedToResolveVariable 401 เกิดขึ้นเมื่อไม่มีตัวแปรโฟลว์ที่ระบุในองค์ประกอบ <Source> ของนโยบายอยู่
steps.jwt.InvalidToken 401 เกิดขึ้นเมื่อตัวแปรโฟลว์ที่ระบุในองค์ประกอบ <Source> ของนโยบายอยู่นอกขอบเขตหรือแก้ไขไม่ได้

ข้อผิดพลาดในการทำให้ใช้งานได้

ข้อผิดพลาดเหล่านี้อาจเกิดขึ้นเมื่อคุณใช้พร็อกซีที่มีนโยบายนี้

ชื่อข้อผิดพลาด สาเหตุ แก้ไข
InvalidEmptyElement เกิดขึ้นเมื่อไม่ได้ระบุตัวแปรโฟลว์ที่มี JWT ที่จะถอดรหัสในองค์ประกอบ <Source> ของนโยบาย

Fault variables

These variables are set when a runtime error occurs. For more information, see What you need to know about policy errors.

Variables Where Example
fault.name="fault_name" fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. fault.name Matches "TokenExpired"
JWT.failed All JWT policies set the same variable in the case of a failure. JWT.failed = true

Example error response

JWT Policy Fault Codes

For error handling, the best practice is to trap the errorcode part of the error response. Do not rely on the text in the faultstring, because it could change.

Example fault rule

    <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>