מוצג המסמך של Apigee Edge.
עוברים אל
מסמכי תיעוד של Apigee X. מידע
מה
מפענח JWT בלי לאמת את החתימה ב-JWT. האפשרות הזו שימושית במיוחד כשמשתמשים תואם למדיניות VerifyJWT, כאשר הערך של תביעה מתוך ה-JWT חייב להיות ידוע לפני אימות החתימה ב-JWT.
המדיניות של JWT פענוח קוד פועלת בלי קשר לאלגוריתם ששימש לחתימה על ה-JWT. מבוא מפורט זמין במאמר סקירה כללית של המדיניות של JWS ו-JWT.
וידאו
בסרטון קצר זה מוסבר איך לפענח את הקוד של JWT.
דוגמה: Decode a JWT
המדיניות שמוצגת בהמשך מפענחת JWT שנמצא במשתנה הזרימה var.jwt. הזה חייב להיות קיים ולהכיל ערך JWT בר-קיימא (ניתן לפענוח). המדיניות יכולה לקבל את ה-JWT מ: כל משתנה של זרימה.
<DecodeJWT name="JWT-Decode-HS256"> <DisplayName>JWT Verify HS256</DisplayName> <Source>var.jwt</Source> </DecodeJWT>
הפלט של המדיניות נכתב במשתני ההקשר, כך שכללי המדיניות או התנאים הבאים נכתבים בשרת ה-proxy ל-API יכול לבדוק את הערכים האלה. בקטע משתני זרימה אפשר למצוא של המשתנים שהוגדרו במדיניות הזו.
מסמך עזר של רכיב לפענוח קוד של JWT
בחומר העזר בנושא המדיניות מתוארים הרכיבים והמאפיינים של מדיניות 'פענוח הקידוד של JWT'.
מאפיינים החלה על הרכיב ברמה העליונה
<DecodeJWT name="JWT" continueOnError="false" enabled="true" async="false">
המאפיינים הבאים משותפים לכל רכיבי ההורה של המדיניות.
| מאפיין | תיאור | ברירת מחדל | נוכחות |
|---|---|---|---|
| שם |
השם הפנימי של המדיניות. תווים שבהם אפשר להשתמש בשם מוגבלים ל:
A-Z0-9._\-$ % עם זאת, בממשק המשתמש לניהול Edge אוכפים פקודות נוספות
הגבלות, כגון הסרה אוטומטית של תווים שאינם אלפאנומריים.
אפשר להשתמש ברכיב |
לא רלוונטי | חובה |
| continueOnError |
צריך להגדיר את הערך false כדי להחזיר שגיאה כשמדיניות נכשלת. המצב הזה צפוי
של רוב כללי המדיניות.
יש להגדיר ל- |
false | אופציונלי |
| פעיל |
צריך להגדיר את הערך true כדי לאכוף את המדיניות.
הגדרת הערך |
true | אופציונלי |
| אסינכרוני | המאפיין הזה הוצא משימוש. | false | הוצא משימוש |
<DisplayName>
<DisplayName>Policy Display Name</DisplayName>
שימוש בנוסף למאפיין השם כדי להוסיף תווית למדיניות בעורך ה-Proxy של ממשק המשתמש לניהול בשם אחר בשפה טבעית.
| ברירת מחדל | אם משמיטים את הרכיב הזה, המערכת תשתמש בערך של מאפיין השם של המדיניות. |
| נוכחות | אופציונלי |
| סוג | מחרוזת |
<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. |
התייחסות לשגיאות
This section describes the fault codes and error messages that are returned and fault variables that are set by Edge 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. | build |
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. |
build |
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.
|
build |
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
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>