1 | <?php |
||
28 | class JWSBuilder |
||
29 | { |
||
30 | /** |
||
31 | * @var null|string |
||
32 | */ |
||
33 | protected $payload; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $isPayloadDetached; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $signatures = []; |
||
44 | |||
45 | /** |
||
46 | * @var null|bool |
||
47 | */ |
||
48 | protected $isPayloadEncoded; |
||
49 | |||
50 | /** |
||
51 | * @var AlgorithmManager |
||
52 | */ |
||
53 | private $signatureAlgorithmManager; |
||
54 | |||
55 | public function __construct(AlgorithmManager $signatureAlgorithmManager) |
||
56 | { |
||
57 | $this->signatureAlgorithmManager = $signatureAlgorithmManager; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns the algorithm manager associated to the builder. |
||
62 | */ |
||
63 | public function getSignatureAlgorithmManager(): AlgorithmManager |
||
64 | { |
||
65 | return $this->signatureAlgorithmManager; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Reset the current data. |
||
70 | * |
||
71 | * @return JWSBuilder |
||
72 | */ |
||
73 | public function create(): self |
||
74 | { |
||
75 | $this->payload = null; |
||
76 | $this->isPayloadDetached = false; |
||
77 | $this->signatures = []; |
||
78 | $this->isPayloadEncoded = null; |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Set the payload. |
||
85 | * This method will return a new JWSBuilder object. |
||
86 | * |
||
87 | * @throws InvalidArgumentException if the payload is not UTF-8 encoded |
||
88 | * |
||
89 | * @return JWSBuilder |
||
90 | */ |
||
91 | public function withPayload(string $payload, bool $isPayloadDetached = false): self |
||
92 | { |
||
93 | if (false === mb_detect_encoding($payload, 'UTF-8', true)) { |
||
94 | throw new InvalidArgumentException('The payload must be encoded in UTF-8'); |
||
95 | } |
||
96 | $clone = clone $this; |
||
97 | $clone->payload = $payload; |
||
98 | $clone->isPayloadDetached = $isPayloadDetached; |
||
99 | |||
100 | return $clone; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Adds the information needed to compute the signature. |
||
105 | * This method will return a new JWSBuilder object. |
||
106 | * |
||
107 | * @throws InvalidArgumentException if the payload encoding is inconsistent |
||
108 | * |
||
109 | * @return JWSBuilder |
||
110 | */ |
||
111 | public function addSignature(JWK $signatureKey, array $protectedHeader, array $header = []): self |
||
134 | |||
135 | /** |
||
136 | * Computes all signatures and return the expected JWS object. |
||
137 | * |
||
138 | * @throws RuntimeException if the payload is not set |
||
139 | * @throws RuntimeException if no signature is defined |
||
140 | */ |
||
141 | public function build(): JWS |
||
173 | |||
174 | private function checkIfPayloadIsEncoded(array $protectedHeader): bool |
||
178 | |||
179 | /** |
||
180 | * @throws LogicException if the header parameter "crit" is missing, invalid or does not contain "b64" when "b64" is set |
||
181 | */ |
||
182 | private function checkB64AndCriticalHeader(array $protectedHeader): void |
||
197 | |||
198 | /** |
||
199 | * @throws InvalidArgumentException if the header parameter "alg" is missing or the algorithm is not allowed/not supported |
||
200 | * |
||
201 | * @return MacAlgorithm|SignatureAlgorithm |
||
202 | */ |
||
203 | private function findSignatureAlgorithm(JWK $key, array $protectedHeader, array $header): Algorithm |
||
220 | |||
221 | /** |
||
222 | * @throws InvalidArgumentException if the header contains duplicated entries |
||
223 | */ |
||
224 | private function checkDuplicatedHeaderParameters(array $header1, array $header2): void |
||
231 | } |
||
232 |