You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

4 years ago
  1. <?php
  2. include __DIR__ . "/../vendor/autoload.php";
  3. use \Firebase\JWT\JWT;
  4. $key = "example_key";
  5. $payload = array(
  6. "iss" => "http://example.org",
  7. "aud" => "http://example.com",
  8. "iat" => 1356999524,
  9. "nbf" => 1357000000
  10. );
  11. /**
  12. * IMPORTANT:
  13. * You must specify supported algorithms for your application. See
  14. * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
  15. * for a list of spec-compliant algorithms.
  16. */
  17. echo $jwt = JWT::encode($payload, $key);
  18. $decoded = JWT::decode($jwt, $key, array('HS256'));
  19. print_r($decoded);
  20. /*
  21. NOTE: This will now be an object instead of an associative array. To get
  22. an associative array, you will need to cast it as such:
  23. */
  24. $decoded_array = (array) $decoded;
  25. /**
  26. * You can add a leeway to account for when there is a clock skew times between
  27. * the signing and verifying servers. It is recommended that this leeway should
  28. * not be bigger than a few minutes.
  29. *
  30. * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
  31. */
  32. JWT::$leeway = 60; // $leeway in seconds
  33. $decoded = JWT::decode($jwt, $key, array('HS256'));
  34. ?>