@@ -65,6 +65,40 @@ $decoded_array = (array) $decoded;
65
65
JWT::$leeway = 60; // $leeway in seconds
66
66
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
67
67
```
68
+ Example encode/decode headers
69
+ -------
70
+ Decoding the JWT headers without verifying the JWT first is NOT recommended, and is not supported by
71
+ this library. This is because without verifying the JWT, the header values could have been tampered with.
72
+ Any value pulled from an unverified header should be treated as if it could be any string sent in from an
73
+ attacker. If this is something you still want to do in your application for whatever reason, it's possible to
74
+ decode the header values manually simply by calling ` json_decode ` and ` base64_decode ` on the JWT
75
+ header part:
76
+ ``` php
77
+ use Firebase\JWT\JWT;
78
+
79
+ $key = 'example_key';
80
+ $payload = [
81
+ 'iss' => 'http://example.org',
82
+ 'aud' => 'http://example.com',
83
+ 'iat' => 1356999524,
84
+ 'nbf' => 1357000000
85
+ ];
86
+
87
+ $headers = [
88
+ 'x-forwarded-for' => 'www.google.com'
89
+ ];
90
+
91
+ // Encode headers in the JWT string
92
+ $jwt = JWT::encode($payload, $key, 'HS256', null, $headers);
93
+
94
+ // Decode headers from the JWT string WITHOUT validation
95
+ // **IMPORTANT**: This operation is vulnerable to attacks, as the JWT has not yet been verified.
96
+ // These headers could be any value sent by an attacker.
97
+ list($headersB64, $payloadB64, $sig) = explode('.', $jwt);
98
+ $decoded = json_decode(base64_decode($headersB64), true);
99
+
100
+ print_r($decoded);
101
+ ```
68
102
Example with RS256 (openssl)
69
103
----------------------------
70
104
``` php
0 commit comments