Key.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Firebase\JWT;
  3. use InvalidArgumentException;
  4. use OpenSSLAsymmetricKey;
  5. use OpenSSLCertificate;
  6. use TypeError;
  7. class Key
  8. {
  9. /**
  10. * @param string|OpenSSLAsymmetricKey|OpenSSLCertificate $keyMaterial
  11. * @param string $algorithm
  12. */
  13. public function __construct(
  14. #[\SensitiveParameter] private $keyMaterial,
  15. private string $algorithm
  16. ) {
  17. if (
  18. !\is_string($keyMaterial)
  19. && !$keyMaterial instanceof OpenSSLAsymmetricKey
  20. && !$keyMaterial instanceof OpenSSLCertificate
  21. ) {
  22. throw new TypeError('Key material must be a string, OpenSSLCertificate, or OpenSSLAsymmetricKey');
  23. }
  24. if (empty($keyMaterial)) {
  25. throw new InvalidArgumentException('Key material must not be empty');
  26. }
  27. if (empty($algorithm)) {
  28. throw new InvalidArgumentException('Algorithm must not be empty');
  29. }
  30. }
  31. /**
  32. * Return the algorithm valid for this key
  33. *
  34. * @return string
  35. */
  36. public function getAlgorithm(): string
  37. {
  38. return $this->algorithm;
  39. }
  40. /**
  41. * @return string|OpenSSLAsymmetricKey|OpenSSLCertificate
  42. */
  43. public function getKeyMaterial()
  44. {
  45. return $this->keyMaterial;
  46. }
  47. }