friendship ended with social-app. php is my new best friend
1<?php
2declare(strict_types=1);
3
4namespace Lcobucci\JWT;
5
6use Closure;
7use Lcobucci\JWT\Encoding\ChainedFormatter;
8use Lcobucci\JWT\Encoding\JoseEncoder;
9use Lcobucci\JWT\Signer\Key;
10use Lcobucci\JWT\Validation\Constraint;
11
12/**
13 * Configuration container for the JWT Builder and Parser
14 *
15 * Serves like a small DI container to simplify the creation and usage
16 * of the objects.
17 */
18final class Configuration
19{
20 private Parser $parser;
21 private Validator $validator;
22
23 /** @var Closure(ClaimsFormatter $claimFormatter): Builder */
24 private Closure $builderFactory;
25
26 /** @var Constraint[] */
27 private array $validationConstraints;
28
29 /** @param Closure(ClaimsFormatter $claimFormatter): Builder|null $builderFactory */
30 private function __construct(
31 private readonly Signer $signer,
32 private readonly Key $signingKey,
33 private readonly Key $verificationKey,
34 private readonly Encoder $encoder,
35 private readonly Decoder $decoder,
36 ?Parser $parser,
37 ?Validator $validator,
38 ?Closure $builderFactory,
39 Constraint ...$validationConstraints,
40 ) {
41 $this->parser = $parser ?? new Token\Parser($decoder);
42 $this->validator = $validator ?? new Validation\Validator();
43
44 $this->builderFactory = $builderFactory
45 ?? static function (ClaimsFormatter $claimFormatter) use ($encoder): Builder {
46 return Token\Builder::new($encoder, $claimFormatter);
47 };
48
49 $this->validationConstraints = $validationConstraints;
50 }
51
52 public static function forAsymmetricSigner(
53 Signer $signer,
54 Key $signingKey,
55 Key $verificationKey,
56 Encoder $encoder = new JoseEncoder(),
57 Decoder $decoder = new JoseEncoder(),
58 ): self {
59 return new self(
60 $signer,
61 $signingKey,
62 $verificationKey,
63 $encoder,
64 $decoder,
65 null,
66 null,
67 null,
68 );
69 }
70
71 public static function forSymmetricSigner(
72 Signer $signer,
73 Key $key,
74 Encoder $encoder = new JoseEncoder(),
75 Decoder $decoder = new JoseEncoder(),
76 ): self {
77 return new self(
78 $signer,
79 $key,
80 $key,
81 $encoder,
82 $decoder,
83 null,
84 null,
85 null,
86 );
87 }
88
89 /**
90 * @deprecated Deprecated since v5.5, please use {@see self::withBuilderFactory()} instead
91 *
92 * @param callable(ClaimsFormatter): Builder $builderFactory
93 */
94 public function setBuilderFactory(callable $builderFactory): void
95 {
96 $this->builderFactory = $builderFactory(...);
97 }
98
99 /** @param callable(ClaimsFormatter): Builder $builderFactory */
100 public function withBuilderFactory(callable $builderFactory): self
101 {
102 return new self(
103 $this->signer,
104 $this->signingKey,
105 $this->verificationKey,
106 $this->encoder,
107 $this->decoder,
108 $this->parser,
109 $this->validator,
110 $builderFactory(...),
111 ...$this->validationConstraints,
112 );
113 }
114
115 public function builder(?ClaimsFormatter $claimFormatter = null): Builder
116 {
117 return ($this->builderFactory)($claimFormatter ?? ChainedFormatter::default());
118 }
119
120 public function parser(): Parser
121 {
122 return $this->parser;
123 }
124
125 /** @deprecated Deprecated since v5.5, please use {@see self::withParser()} instead */
126 public function setParser(Parser $parser): void
127 {
128 $this->parser = $parser;
129 }
130
131 public function withParser(Parser $parser): self
132 {
133 return new self(
134 $this->signer,
135 $this->signingKey,
136 $this->verificationKey,
137 $this->encoder,
138 $this->decoder,
139 $parser,
140 $this->validator,
141 $this->builderFactory,
142 ...$this->validationConstraints,
143 );
144 }
145
146 public function signer(): Signer
147 {
148 return $this->signer;
149 }
150
151 public function signingKey(): Key
152 {
153 return $this->signingKey;
154 }
155
156 public function verificationKey(): Key
157 {
158 return $this->verificationKey;
159 }
160
161 public function validator(): Validator
162 {
163 return $this->validator;
164 }
165
166 /** @deprecated Deprecated since v5.5, please use {@see self::withValidator()} instead */
167 public function setValidator(Validator $validator): void
168 {
169 $this->validator = $validator;
170 }
171
172 public function withValidator(Validator $validator): self
173 {
174 return new self(
175 $this->signer,
176 $this->signingKey,
177 $this->verificationKey,
178 $this->encoder,
179 $this->decoder,
180 $this->parser,
181 $validator,
182 $this->builderFactory,
183 ...$this->validationConstraints,
184 );
185 }
186
187 /** @return Constraint[] */
188 public function validationConstraints(): array
189 {
190 return $this->validationConstraints;
191 }
192
193 /** @deprecated Deprecated since v5.5, please use {@see self::withValidationConstraints()} instead */
194 public function setValidationConstraints(Constraint ...$validationConstraints): void
195 {
196 $this->validationConstraints = $validationConstraints;
197 }
198
199 public function withValidationConstraints(Constraint ...$validationConstraints): self
200 {
201 return new self(
202 $this->signer,
203 $this->signingKey,
204 $this->verificationKey,
205 $this->encoder,
206 $this->decoder,
207 $this->parser,
208 $this->validator,
209 $this->builderFactory,
210 ...$validationConstraints,
211 );
212 }
213}