| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Emarref\Jwt\Verification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Emarref\Jwt\Encoding; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Emarref\Jwt\Encryption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Emarref\Jwt\Exception\InvalidSignatureException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Emarref\Jwt\HeaderParameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Emarref\Jwt\Signature; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Emarref\Jwt\Token; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class EncryptionVerifier implements VerifierInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * @var Encryption\EncryptionInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private $encryption; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @var Encoding\EncoderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     private $encoder; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @var Signature\Jws | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     protected $signer; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param Encryption\EncryptionInterface $encryption | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param Encoding\EncoderInterface    $encoder | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct(Encryption\EncryptionInterface $encryption, Encoding\EncoderInterface $encoder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->encryption = $encryption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->encoder    = $encoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->signer     = new Signature\Jws($this->encryption, $this->encoder); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param Token $token | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @throws InvalidSignatureException | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function verify(Token $token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         /** @var HeaderParameter\Algorithm $algorithmParameter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $algorithmParameter = $token->getHeader()->findParameterByName(HeaderParameter\Algorithm::NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (null === $algorithmParameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             throw new \RuntimeException('Algorithm parameter not found in token header.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if ($algorithmParameter->getValue() !== $this->encryption->getAlgorithmName()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             throw new \RuntimeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 'Cannot use "%s" algorithm to decrypt token encrypted with algorithm "%s".', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $this->encryption->getAlgorithmName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $algorithmParameter->getValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (!$this->encryption->verify($this->signer->getUnsignedValue($token), $token->getSignature())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             throw new InvalidSignatureException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  |