FirebaseJwt.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace OAuth2\Encryption;
  3. /**
  4. * Bridge file to use the firebase/php-jwt package for JWT encoding and decoding.
  5. * @author Francis Chuang <francis.chuang@gmail.com>
  6. */
  7. class FirebaseJwt implements EncryptionInterface
  8. {
  9. public function __construct()
  10. {
  11. if (!class_exists('\JWT')) {
  12. throw new \ErrorException('firebase/php-jwt must be installed to use this feature. You can do this by running "composer require firebase/php-jwt"');
  13. }
  14. }
  15. public function encode($payload, $key, $alg = 'HS256', $keyId = null)
  16. {
  17. return \JWT::encode($payload, $key, $alg, $keyId);
  18. }
  19. public function decode($jwt, $key = null, $allowedAlgorithms = null)
  20. {
  21. try {
  22. //Maintain BC: Do not verify if no algorithms are passed in.
  23. if (!$allowedAlgorithms) {
  24. $key = null;
  25. }
  26. return (array)\JWT::decode($jwt, $key, $allowedAlgorithms);
  27. } catch (\Exception $e) {
  28. return false;
  29. }
  30. }
  31. public function urlSafeB64Encode($data)
  32. {
  33. return \JWT::urlsafeB64Encode($data);
  34. }
  35. public function urlSafeB64Decode($b64)
  36. {
  37. return \JWT::urlsafeB64Decode($b64);
  38. }
  39. }