| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * OAuth 2.0 Bearer Token Response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author      Alex Bilbie <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright   Copyright (c) Alex Bilbie | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license     http://mit-license.org/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @link        https://github.com/thephpleague/oauth2-server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace League\OAuth2\Server\ResponseTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use League\OAuth2\Server\Entities\DeviceCodeEntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use function json_encode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use function time; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class DeviceCodeResponse extends AbstractResponseType | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected DeviceCodeEntityInterface $deviceCodeEntity; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     private bool $includeVerificationUriComplete = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private bool $includeInterval = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 5 |  |     public function generateHttpResponse(ResponseInterface $response): ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 5 |  |         $expireDateTime = $this->deviceCodeEntity->getExpiryDateTime()->getTimestamp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 5 |  |         $responseParams = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 5 |  |             'device_code' => $this->deviceCodeEntity->getIdentifier(), | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 5 |  |             'user_code' => $this->deviceCodeEntity->getUserCode(), | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 5 |  |             'verification_uri' => $this->deviceCodeEntity->getVerificationUri(), | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 5 |  |             'expires_in'   => $expireDateTime - time(), | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 5 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 5 |  |         if ($this->includeVerificationUriComplete === true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |             $responseParams['verification_uri_complete'] = $this->deviceCodeEntity->getVerificationUriComplete(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 5 |  |         if ($this->includeInterval === true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |             $responseParams['interval'] = $this->deviceCodeEntity->getInterval(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 5 |  |         $responseParams = json_encode($responseParams); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 5 |  |         if ($responseParams === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             throw new LogicException('Error encountered JSON encoding response parameters'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 5 |  |         $response = $response | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 5 |  |             ->withStatus(200) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 5 |  |             ->withHeader('pragma', 'no-cache') | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 5 |  |             ->withHeader('cache-control', 'no-store') | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 5 |  |             ->withHeader('content-type', 'application/json; charset=UTF-8'); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 5 |  |         $response->getBody()->write($responseParams); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 5 |  |         return $response; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 5 |  |     public function setDeviceCodeEntity(DeviceCodeEntityInterface $deviceCodeEntity): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 | 5 |  |         $this->deviceCodeEntity = $deviceCodeEntity; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |     public function includeVerificationUriComplete(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 | 1 |  |         $this->includeVerificationUriComplete = true; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function includeInterval(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |         $this->includeInterval = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * Add custom fields to your Bearer Token response here, then override | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * AuthorizationServer::getResponseType() to pull in your version of | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * this class rather than the default. | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @return array<array-key,mixed> | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     protected function getExtraParams(DeviceCodeEntityInterface $deviceCode): array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |