Skip to content

Commit ce5ad65

Browse files
authored
Merge pull request #32 from robertpustulka/fix-handler
Fix unauthorized handler with authorization check.
2 parents cafb8a5 + 4f712b8 commit ce5ad65

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/Middleware/AuthorizationMiddleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
111111

112112
try {
113113
$response = $next($request, $response);
114+
if ($this->getConfig('requireAuthorizationCheck') && !$service->authorizationChecked()) {
115+
throw new AuthorizationRequiredException(['url' => $request->getRequestTarget()]);
116+
}
114117
} catch (Exception $exception) {
115118
$handler = $this->getHandler();
116119
$response = $handler->handle($exception, $request, $response, (array)$this->getConfig('unauthorizedHandler'));
117120
}
118-
if ($this->getConfig('requireAuthorizationCheck') && !$service->authorizationChecked()) {
119-
throw new AuthorizationRequiredException(['url' => $request->getRequestTarget()]);
120-
}
121121

122122
return $response;
123123
}

tests/TestCase/Middleware/AuthorizationMiddlewareTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ public function testInvokeAuthorizationRequiredError()
6363

6464
$request = (new ServerRequest())->withAttribute('identity', ['id' => 1]);
6565
$response = new Response();
66-
$next = function ($request) {
66+
$next = function ($request, $response) {
6767
// Don't call any auth methods.
68-
return $request;
68+
return $response;
6969
};
7070

7171
$middleware = new AuthorizationMiddleware($service, ['requireAuthorizationCheck' => true]);
7272
$result = $middleware($request, $response, $next);
7373

74-
$this->assertInstanceOf(RequestInterface::class, $result);
75-
$this->assertSame($service, $result->getAttribute('authorization'));
74+
$this->assertInstanceOf(ResponseInterface::class, $result);
75+
$this->assertSame($service, $request->getAttribute('authorization'));
7676
}
7777

7878
public function testInvokeApp()
@@ -343,4 +343,22 @@ public function testUnauthorizedHandlerSuppress()
343343
$result = $middleware($request, $response, $next);
344344
$this->assertSame($response, $result);
345345
}
346+
347+
public function testUnauthorizedHandlerRequireAuthz()
348+
{
349+
$service = $this->createMock(AuthorizationServiceInterface::class);
350+
$request = new ServerRequest();
351+
$response = new Response();
352+
$next = function () {
353+
throw new Exception();
354+
};
355+
356+
$middleware = new AuthorizationMiddleware($service, [
357+
'requireAuthorizationCheck' => true,
358+
'unauthorizedHandler' => 'Suppress',
359+
]);
360+
361+
$result = $middleware($request, $response, $next);
362+
$this->assertSame($response, $result);
363+
}
346364
}

0 commit comments

Comments
 (0)