Przeglądasz dokumentację Apigee Edge.
Przejdź do
Dokumentacja Apigee X. informacje.

Co
Dekoduje token JWT bez weryfikacji podpisu. Jest to najbardziej przydatne, gdy używasz zgodności z zasadąVerifyJWT, gdy wartość żądania z tokena JWT musi być znana przed zweryfikowaniem podpisu tokena JWT.
Zasada dekodowania JWT działa niezależnie od algorytmu użytego do podpisania tokena JWT. Szczegółowe informacje znajdziesz w omówieniu zasad JWS i JWT.
Wideo
Obejrzyj krótki film, aby dowiedzieć się, jak zdekodować token JWT.
Przykład: dekodowanie tokena JWT
Poniższa zasada dekoduje token JWT znajdujący się w zmiennej przepływu var.jwt. Ten musi być obecna i zawierać realny token JWT (który można dedykować). Zasada może pobierać token JWT z dowolną zmienną przepływu.
<DecodeJWT name="JWT-Decode-HS256"> <DisplayName>JWT Verify HS256</DisplayName> <Source>var.jwt</Source> </DecodeJWT>
Zasada zapisuje dane wyjściowe w zmiennych kontekstowych, aby kolejne zasady lub warunki za pośrednictwem serwera proxy interfejsu API może badać te wartości. W sekcji Zmienne przepływu znajdziesz listę zmiennych ustawionych przez tę zasadę.
Dokumentacja elementu na potrzeby dekodowania tokena JWT
Dokumentacja zasady zawiera opis jej elementów i atrybutów.
Atrybuty, które zastosuj do elementu najwyższego poziomu
<DecodeJWT name="JWT" continueOnError="false" enabled="true" async="false">
Poniższe atrybuty są wspólne dla wszystkich elementów nadrzędnych zasad.
Atrybut | Opis | Domyślnie | Obecność |
---|---|---|---|
nazwa |
Wewnętrzna nazwa zasady. W nazwie można używać tylko następujących znaków:
A-Z0-9._\-$ % Interfejs zarządzania brzegowego wymusza jednak dodatkowe
takich jak automatyczne usuwanie znaków innych niż alfanumeryczne.
Opcjonalnie możesz użyć elementu |
Nie dotyczy | Wymagane |
continueOnError |
Ustaw jako false , aby w przypadku niepowodzenia zasady zwracany był błąd. To normalne
w przypadku większości zasad.
Ustaw jako |
fałsz | Opcjonalnie |
włączone |
Aby egzekwować zasadę, ustaw wartość true .
Ustaw „Wyłącz” na: |
prawda | Opcjonalnie |
asynchroniczny | Ten atrybut został wycofany. | fałsz | Wycofano |
<DisplayName>
<DisplayName>Policy Display Name</DisplayName>
Użyj oprócz atrybutu name [nazwa], aby oznaczyć zasadę etykietą w edytorze serwera proxy w interfejsie zarządzania z inną nazwą w języku naturalnym.
Domyślnie | Jeśli pominiesz ten element, zostanie użyta wartość atrybutu nazwa zasady. |
Obecność | Opcjonalnie |
Typ | Ciąg znaków |
<Source>
<Source>jwt-variable</Source>
Jeśli ten parametr jest używany, określa zmienną przepływu, w której zasada spodziewa się znaleźć token JWT do ich dekodowania.
Domyślnie | request.header.authorization (Ważne informacje znajdziesz w uwagę powyżej
o domyślnej wartości). |
Obecność | Opcjonalnie |
Typ | Ciąg znaków |
Prawidłowe wartości | Nazwa zmiennej przepływu brzegowego |
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. |
Informacje o błędzie
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 |
Zmienne błędów
Te zmienne są ustawiane po wystąpieniu błędu działania. Więcej informacji znajdziesz w artykule Podstawowe informacje o błędach związanych z naruszeniem zasad.
Zmienne | Gdzie | Przykład |
---|---|---|
fault.name="fault_name" |
fault_name to nazwa błędu podana w tabeli Błędy czasu działania powyżej. Nazwa błędu to ostatnia część kodu błędu. | fault.name Matches "TokenExpired" |
JWT.failed |
Wszystkie zasady JWT ustawiają tę samą zmienną w przypadku niepowodzenia. | JWT.failed = true |
Przykładowa odpowiedź na błąd
W przypadku obsługi błędów sprawdzoną metodą jest przechwycenie części błędu errorcode
.
. Nie polegaj na tekście zawartym w pliku faultstring
, ponieważ może się on zmienić.
Przykładowa reguła błędu
<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>