| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Limoncello\Application\Packages\Cors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2015-2019 [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Limoncello\Contracts\Settings\SettingsInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Neomerx\Cors\Strategies\Settings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @package Limoncello\Application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class CorsSettings implements SettingsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     const KEY_ALLOWED_ORIGINS = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     const KEY_ALLOWED_METHODS = self::KEY_ALLOWED_ORIGINS + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     const KEY_ALLOWED_HEADERS = self::KEY_ALLOWED_METHODS + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     const KEY_EXPOSED_HEADERS = self::KEY_ALLOWED_HEADERS + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     const KEY_IS_USING_CREDENTIALS = self::KEY_EXPOSED_HEADERS + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     const KEY_FLIGHT_CACHE_MAX_AGE = self::KEY_IS_USING_CREDENTIALS + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     const KEY_IS_FORCE_ADD_METHODS = self::KEY_FLIGHT_CACHE_MAX_AGE + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     const KEY_IS_FORCE_ADD_HEADERS = self::KEY_IS_FORCE_ADD_METHODS + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** @see Settings */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     const KEY_IS_CHECK_HOST = self::KEY_IS_FORCE_ADD_HEADERS + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** Settings key */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     const KEY_LOG_IS_ENABLED = self::KEY_IS_CHECK_HOST + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** Settings key */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     const KEY_LAST = self::KEY_LOG_IS_ENABLED; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     private $appConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 2 |  |     final public function get(array $appConfig): array | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 2 |  |         $this->appConfig = $appConfig; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 2 |  |         $serverOriginScheme = $appConfig[A::KEY_APP_ORIGIN_SCHEMA]; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $serverOriginHost   = $appConfig[A::KEY_APP_ORIGIN_HOST]; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 2 |  |         $serverOriginPort   = $appConfig[A::KEY_APP_ORIGIN_PORT] ? (int)$appConfig[A::KEY_APP_ORIGIN_PORT] : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $corsSettings = (new Settings())->init($serverOriginScheme, $serverOriginHost, $serverOriginPort); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         // convert settings into Cors Settings and then into cache data | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 2 |  |         $packageSettings = $this->getSettings(); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |         $corsSettings->setAllowedOrigins($packageSettings[static::KEY_ALLOWED_ORIGINS]); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 2 |  |         $corsSettings->setAllowedMethods($packageSettings[static::KEY_ALLOWED_METHODS]); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 2 |  |         $corsSettings->setAllowedHeaders($packageSettings[static::KEY_ALLOWED_HEADERS]); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         $corsSettings->setExposedHeaders($packageSettings[static::KEY_EXPOSED_HEADERS]); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 2 |  |         $corsSettings->setPreFlightCacheMaxAge($packageSettings[static::KEY_FLIGHT_CACHE_MAX_AGE]); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $packageSettings[static::KEY_IS_USING_CREDENTIALS] === true ? | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 2 |  |             $corsSettings->setCredentialsSupported() : $corsSettings->setCredentialsNotSupported(); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |         $packageSettings[static::KEY_IS_FORCE_ADD_METHODS] === true ? | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             $corsSettings->enableAddAllowedMethodsToPreFlightResponse() : | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 2 |  |             $corsSettings->disableAddAllowedMethodsToPreFlightResponse(); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 1 |  |         $packageSettings[static::KEY_IS_FORCE_ADD_HEADERS] === true ? | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             $corsSettings->enableAddAllowedHeadersToPreFlightResponse() : | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 2 |  |             $corsSettings->disableAddAllowedHeadersToPreFlightResponse(); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $packageSettings[static::KEY_IS_CHECK_HOST] === true ? | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 2 |  |             $corsSettings->enableCheckHost() : $corsSettings->disableCheckHost(); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         return [$corsSettings->getData(), (bool)$packageSettings[static::KEY_LOG_IS_ENABLED]]; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |     protected function getSettings(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |         $appConfig = $this->getAppConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $serverOrigin = $appConfig[A::KEY_APP_ORIGIN_URI] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $isLogEnabled = (bool)($appConfig[A::KEY_IS_LOG_ENABLED] ?? false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |             static::KEY_ALLOWED_ORIGINS      => empty($serverOrigin) === true ? [] : [$serverOrigin], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |             static::KEY_ALLOWED_METHODS      => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |             static::KEY_ALLOWED_HEADERS      => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |             static::KEY_EXPOSED_HEADERS      => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |             static::KEY_IS_USING_CREDENTIALS => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |             static::KEY_FLIGHT_CACHE_MAX_AGE => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |             static::KEY_IS_FORCE_ADD_METHODS => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |             static::KEY_IS_FORCE_ADD_HEADERS => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             static::KEY_IS_CHECK_HOST        => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             static::KEY_LOG_IS_ENABLED       => $isLogEnabled, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 2 |  |     protected function getAppConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $this->appConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |