Configuration.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. namespace Lcobucci\JWT;
  4. use Closure;
  5. use Lcobucci\JWT\Encoding\ChainedFormatter;
  6. use Lcobucci\JWT\Encoding\JoseEncoder;
  7. use Lcobucci\JWT\Signer\Key;
  8. use Lcobucci\JWT\Signer\Key\InMemory;
  9. use Lcobucci\JWT\Signer\None;
  10. use Lcobucci\JWT\Validation\Constraint;
  11. /**
  12. * Configuration container for the JWT Builder and Parser
  13. *
  14. * Serves like a small DI container to simplify the creation and usage
  15. * of the objects.
  16. */
  17. final class Configuration
  18. {
  19. private Parser $parser;
  20. private Signer $signer;
  21. private Key $signingKey;
  22. private Key $verificationKey;
  23. private Validator $validator;
  24. /** @var Closure(ClaimsFormatter $claimFormatter): Builder */
  25. private Closure $builderFactory;
  26. /** @var Constraint[] */
  27. private array $validationConstraints = [];
  28. private function __construct(
  29. Signer $signer,
  30. Key $signingKey,
  31. Key $verificationKey,
  32. ?Encoder $encoder = null,
  33. ?Decoder $decoder = null
  34. ) {
  35. $this->signer = $signer;
  36. $this->signingKey = $signingKey;
  37. $this->verificationKey = $verificationKey;
  38. $this->parser = new Token\Parser($decoder ?? new JoseEncoder());
  39. $this->validator = new Validation\Validator();
  40. $this->builderFactory = static function (ClaimsFormatter $claimFormatter) use ($encoder): Builder {
  41. return new Token\Builder($encoder ?? new JoseEncoder(), $claimFormatter);
  42. };
  43. }
  44. public static function forAsymmetricSigner(
  45. Signer $signer,
  46. Key $signingKey,
  47. Key $verificationKey,
  48. ?Encoder $encoder = null,
  49. ?Decoder $decoder = null
  50. ): self {
  51. return new self(
  52. $signer,
  53. $signingKey,
  54. $verificationKey,
  55. $encoder,
  56. $decoder
  57. );
  58. }
  59. public static function forSymmetricSigner(
  60. Signer $signer,
  61. Key $key,
  62. ?Encoder $encoder = null,
  63. ?Decoder $decoder = null
  64. ): self {
  65. return new self(
  66. $signer,
  67. $key,
  68. $key,
  69. $encoder,
  70. $decoder
  71. );
  72. }
  73. public static function forUnsecuredSigner(
  74. ?Encoder $encoder = null,
  75. ?Decoder $decoder = null
  76. ): self {
  77. $key = InMemory::empty();
  78. return new self(
  79. new None(),
  80. $key,
  81. $key,
  82. $encoder,
  83. $decoder
  84. );
  85. }
  86. /** @param callable(ClaimsFormatter): Builder $builderFactory */
  87. public function setBuilderFactory(callable $builderFactory): void
  88. {
  89. $this->builderFactory = Closure::fromCallable($builderFactory);
  90. }
  91. public function builder(?ClaimsFormatter $claimFormatter = null): Builder
  92. {
  93. return ($this->builderFactory)($claimFormatter ?? ChainedFormatter::default());
  94. }
  95. public function parser(): Parser
  96. {
  97. return $this->parser;
  98. }
  99. public function setParser(Parser $parser): void
  100. {
  101. $this->parser = $parser;
  102. }
  103. public function signer(): Signer
  104. {
  105. return $this->signer;
  106. }
  107. public function signingKey(): Key
  108. {
  109. return $this->signingKey;
  110. }
  111. public function verificationKey(): Key
  112. {
  113. return $this->verificationKey;
  114. }
  115. public function validator(): Validator
  116. {
  117. return $this->validator;
  118. }
  119. public function setValidator(Validator $validator): void
  120. {
  121. $this->validator = $validator;
  122. }
  123. /** @return Constraint[] */
  124. public function validationConstraints(): array
  125. {
  126. return $this->validationConstraints;
  127. }
  128. public function setValidationConstraints(Constraint ...$validationConstraints): void
  129. {
  130. $this->validationConstraints = $validationConstraints;
  131. }
  132. }