| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the AppleApnPush package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Vitaliy Zhuk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Apple\ApnPush\Protocol\Http\Authenticator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Apple\ApnPush\Jwt\JwtInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Apple\ApnPush\Protocol\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Jose\Factory\JWKFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Jose\Factory\JWSFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Authenticate request via Json Web Token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class JwtAuthenticator implements AuthenticatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private const ALGORITHM = 'ES256'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var JwtInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $jwt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $jws; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var \DateInterval | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $jwsLifetime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var \DateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private $jwsValidTo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param JwtInterface  $jwt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param \DateInterval $jwsLifetime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function __construct(JwtInterface $jwt, \DateInterval $jwsLifetime = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->jwt = $jwt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if ($jwsLifetime && $jwsLifetime->invert) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             throw new \InvalidArgumentException('JWS lifetime must not be inverted'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->jwsLifetime = $jwsLifetime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function authenticate(Request $request): Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $now = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (!$this->jws || !$this->jwsLifetime || $this->jwsValidTo < $now) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->jws = $this->createJwsContent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->jwsValidTo = $this->jwsLifetime ? ($now)->add($this->jwsLifetime) : $this->jwsValidTo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $request = $request->withHeader('authorization', sprintf('bearer %s', $this->jws)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Create the content of JWS by Json Web Token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |     private function createJwsContent(): string | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |         $jwk = JWKFactory::createFromKeyFile($this->jwt->getPath(), '', [ | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |             'kid' => $this->jwt->getKey(), | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |             'alg' => self::ALGORITHM, | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             'use' => 'sig', | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         ]); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         $payload = [ | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             'iss' => $this->jwt->getTeamId(), | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |             'iat' => time(), | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         $header = [ | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |             'alg' => self::ALGORITHM, | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |             'kid' => $jwk->get('kid'), | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         return JWSFactory::createJWSToCompactJSON( | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             $payload, | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |             $jwk, | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             $header | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |