| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright (c) 2014-2018 Spomky-Labs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This software may be modified and distributed under the terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * of the MIT license.  See the LICENSE file for details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Jose\Component\Signature; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Jose\Component\Core\JWT; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | class JWS implements JWT | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @var bool | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     private $isPayloadDetached = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var string|null | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $encodedPayload = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var Signature[] | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     private $signatures = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @var string|null | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     private $payload = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * JWS constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param string|null $payload | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param string|null $encodedPayload | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @param bool        $isPayloadDetached | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private function __construct(?string $payload, ?string $encodedPayload = null, bool $isPayloadDetached = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->payload = $payload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->encodedPayload = $encodedPayload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->isPayloadDetached = $isPayloadDetached; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * Creates a JWS object. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param string|null $payload | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @param string|null $encodedPayload | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param bool        $isPayloadDetached | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @return JWS | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public static function create(?string $payload, ?string $encodedPayload = null, bool $isPayloadDetached = false): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return new self($payload, $encodedPayload, $isPayloadDetached); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function getPayload(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $this->payload; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * Returns true if the payload is detached. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function isPayloadDetached(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->isPayloadDetached; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * Returns the Base64Url encoded payload. | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * If the payload is detached, this method returns null. | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @return string|null | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function getEncodedPayload(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if (true === $this->isPayloadDetached()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $this->encodedPayload; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * Returns the signatures associated with the JWS. | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return Signature[] | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function getSignatures(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return $this->signatures; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * Returns the signature at the given index. | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * @param int $id | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @return Signature | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function getSignature(int $id): Signature | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if (isset($this->signatures[$id])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             return $this->signatures[$id]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         throw new \InvalidArgumentException('The signature does not exist.'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * This method adds a signature to the JWS object. | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * Its returns a new JWS object. | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * @internal | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * @param string      $signature | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * @param array       $protectedHeader | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * @param string|null $encodedProtectedHeader | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * @param array       $header | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * @return JWS | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function addSignature(string $signature, array $protectedHeader, ?string $encodedProtectedHeader, array $header = []): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $jws = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $jws->signatures[] = Signature::create($signature, $protectedHeader, $encodedProtectedHeader, $header); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return $jws; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |      * Returns the number of signature associated with the JWS. | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * @return int | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public function countSignatures(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return count($this->signatures); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * This method splits the JWS into a list of JWSs. | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      * It is only useful when the JWS contains more than one signature (JSON General Serialization). | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |      * @return JWS[] | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     public function split(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         foreach ($this->signatures as $signature) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             $jws = self::create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 $this->payload, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 $this->encodedPayload, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 $this->isPayloadDetached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $jws = $jws->addSignature( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                  $signature->getSignature(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                  $signature->getProtectedHeader(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                  $signature->getEncodedProtectedHeader(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                  $signature->getHeader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |              ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             $result[] = $jws; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 185 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |  |