Skip to content

Commit 61581ce

Browse files
committed
add AuthorizationEnvironmentMiddleware
1 parent 257b2d0 commit 61581ce

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace OAuthServer\Middleware;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
/**
9+
* For php-fpm|php-cgi
10+
*
11+
* Set Authorization header from HTTP_AUTHORIZATION|REDIRECT_HTTP_AUTHORIZATION environment
12+
*/
13+
class AuthorizationEnvironmentMiddleware
14+
{
15+
/**
16+
* @var array the Environment variable name that set for Authorization
17+
*/
18+
protected $environment = [
19+
'HTTP_AUTHORIZATION',
20+
'REDIRECT_HTTP_AUTHORIZATION',
21+
];
22+
23+
/**
24+
* AuthorizationEnvironmentMiddleware constructor.
25+
*
26+
* @param array $environment the Environment variable name that set for Authorization
27+
*/
28+
public function __construct(array $environment = ['HTTP_AUTHORIZATION', 'REDIRECT_HTTP_AUTHORIZATION'])
29+
{
30+
$this->environment = $environment;
31+
}
32+
33+
/**
34+
* Serve assets if the path matches one.
35+
*
36+
* @param ServerRequestInterface $request The request.
37+
* @param ResponseInterface $response The response.
38+
* @param callable $next Callback to invoke the next middleware.
39+
* @return ResponseInterface A response
40+
*/
41+
public function __invoke($request, $response, $next)
42+
{
43+
if ($request->hasHeader('Authorization')) {
44+
// If Authorization header is set, nothing to do.
45+
return $next($request, $response);
46+
}
47+
48+
foreach ($this->environment as $env) {
49+
// Set Authorization header, if the environment variables is set.
50+
if (isset($_SERVER[$env])) {
51+
return $next($request->withHeader('Authorization', $_SERVER[$env]), $response);
52+
}
53+
}
54+
55+
return $next($request, $response);
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace OAuthServer\Test\TestCase\Middleware;
4+
5+
use OAuthServer\Middleware\AuthorizationEnvironmentMiddleware;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use Zend\Diactoros\Response;
10+
use Zend\Diactoros\ServerRequest;
11+
12+
class AuthorizationEnvironmentMiddlewareTest extends TestCase
13+
{
14+
protected function setUp()
15+
{
16+
parent::setUp();
17+
unset($_SERVER['HTTP_AUTHORIZATION'], $_SERVER['REDIRECT_HTTP_AUTHORIZATION']);
18+
}
19+
20+
protected function tearDown()
21+
{
22+
unset($_SERVER['HTTP_AUTHORIZATION'], $_SERVER['REDIRECT_HTTP_AUTHORIZATION']);
23+
parent::tearDown();
24+
}
25+
26+
/**
27+
* @dataProvider dataSetHeaderFromEnvironment
28+
* @param string $env environment name
29+
* @return void
30+
*/
31+
public function testSetHeaderFromEnvironment($env)
32+
{
33+
$_SERVER[$env] = 'from env';
34+
35+
$request = new ServerRequest();
36+
$response = new Response();
37+
$next = function (ServerRequestInterface $request, ResponseInterface $response) {
38+
$this->assertSame('from env', $request->getHeaderLine('authorization'));
39+
};
40+
41+
$middleware = new AuthorizationEnvironmentMiddleware();
42+
43+
$middleware($request, $response, $next);
44+
}
45+
46+
/**
47+
* @return array
48+
*/
49+
public function dataSetHeaderFromEnvironment()
50+
{
51+
return [
52+
['HTTP_AUTHORIZATION'],
53+
['REDIRECT_HTTP_AUTHORIZATION'],
54+
];
55+
}
56+
57+
public function testSetHeaderFromFirstEnvironment()
58+
{
59+
$_SERVER['HTTP_AUTHORIZATION'] = 'from authorization';
60+
$_SERVER['REDIRECT_HTTP_AUTHORIZATION'] = 'from redirect authorization';
61+
62+
$request = new ServerRequest();
63+
$response = new Response();
64+
$next = function (ServerRequestInterface $request, ResponseInterface $response) {
65+
$this->assertSame('from authorization', $request->getHeaderLine('authorization'));
66+
};
67+
68+
$middleware = new AuthorizationEnvironmentMiddleware();
69+
70+
$middleware($request, $response, $next);
71+
}
72+
73+
public function testNotSetHeaderWhenExists()
74+
{
75+
$_SERVER['HTTP_AUTHORIZATION'] = 'from authorization';
76+
$_SERVER['REDIRECT_HTTP_AUTHORIZATION'] = 'from redirect authorization';
77+
78+
$request = (new ServerRequest())->withHeader('Authorization', 'from header');
79+
$response = new Response();
80+
$next = function (ServerRequestInterface $request, ResponseInterface $response) {
81+
$this->assertSame('from header', $request->getHeaderLine('authorization'));
82+
};
83+
84+
$middleware = new AuthorizationEnvironmentMiddleware();
85+
86+
$middleware($request, $response, $next);
87+
}
88+
}

0 commit comments

Comments
 (0)