diff --git a/Slim/Http/Headers.php b/Slim/Http/Headers.php index 6b6375b59..4aa7a5e4d 100644 --- a/Slim/Http/Headers.php +++ b/Slim/Http/Headers.php @@ -51,6 +51,7 @@ class Headers extends Collection implements HeadersInterface public static function createFromEnvironment(Environment $environment) { $data = []; + $environment = self::determineAuthorization($environment); foreach ($environment as $key => $value) { $key = strtoupper($key); if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) { @@ -63,6 +64,30 @@ public static function createFromEnvironment(Environment $environment) return new static($data); } + /** + * If HTTP_AUTHORIZATION does not exist tries to get it from + * getallheaders() when available. + * + * @param Environment $environment The Slim application Environment + * + * @return Environment + */ + + public static function determineAuthorization(Environment $environment) + { + $authorization = $environment->get('HTTP_AUTHORIZATION'); + + if (null === $authorization && is_callable('getallheaders')) { + $headers = getallheaders(); + $headers = array_change_key_case($headers, CASE_LOWER); + if (isset($headers['authorization'])) { + $environment->set('HTTP_AUTHORIZATION', $headers['authorization']); + } + } + + return $environment; + } + /** * Return array of HTTP header names and values. * This method returns the _original_ header name diff --git a/tests/Http/HeadersTest.php b/tests/Http/HeadersTest.php index 345e7620f..9b03197ae 100644 --- a/tests/Http/HeadersTest.php +++ b/tests/Http/HeadersTest.php @@ -201,4 +201,14 @@ public function testNormalizeKey() $this->assertEquals('foo-bar', $h->normalizeKey('http_foo_bar')); $this->assertEquals('foo-bar', $h->normalizeKey('http-foo-bar')); } + + public function testDetermineAuthorization() + { + $e = Environment::mock([]); + $en = Headers::determineAuthorization($e); + $h = Headers::createFromEnvironment($e); + + $this->assertEquals('electrolytes', $en->get('HTTP_AUTHORIZATION')); + $this->assertEquals(['electrolytes'], $h->get('Authorization')); + } } diff --git a/tests/Http/RequestTest.php b/tests/Http/RequestTest.php index 826e2d43f..995e4f05f 100644 --- a/tests/Http/RequestTest.php +++ b/tests/Http/RequestTest.php @@ -604,8 +604,8 @@ public function testWithUploadedFiles() public function testGetServerParams() { - $mockEnv = Environment::mock(); - $request = $this->requestFactory(); + $mockEnv = Environment::mock(["HTTP_AUTHORIZATION" => "test"]); + $request = $this->requestFactory(["HTTP_AUTHORIZATION" => "test"]); $serverParams = $request->getServerParams(); foreach ($serverParams as $key => $value) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f12f6eb6a..865e7e45c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -12,5 +12,7 @@ /** @var \Composer\Autoload\ClassLoader $autoloader */ $autoloader = require dirname(__DIR__) . '/vendor/autoload.php'; +require dirname(__FILE__) . '/getallheaders.php'; + // Register test classes $autoloader->addPsr4('Slim\Tests\\', __DIR__); diff --git a/tests/getallheaders.php b/tests/getallheaders.php new file mode 100644 index 000000000..b1b188e4c --- /dev/null +++ b/tests/getallheaders.php @@ -0,0 +1,7 @@ + 'electrolytes']; + } +}