Skip to content

Commit

Permalink
Cleanup & finishing touches ✨
Browse files Browse the repository at this point in the history
removed the PredicateMiddleware for no added value
  • Loading branch information
dakujem committed Nov 6, 2020
1 parent e4c637f commit c41a7fb
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 300 deletions.
2 changes: 1 addition & 1 deletion src/Factory/AuthFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function inspectTokens(
$response = fn() => $this->responseFactory->createResponse(401); // HTTP status 401 (Unauthorized)
$withError = fn($error = null) => $error !== null ? Man::writeJsonError($response(), $error) : $response();
$token = $request->getAttribute($tokenAttribute ?? Man::TOKEN_ATTRIBUTE_NAME);
if ($token !== null) {
if (is_object($token)) { // asserts that the token is an object
return $inspector(
$token,
fn() => $next->handle($request),
Expand Down
6 changes: 3 additions & 3 deletions src/Factory/AuthWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Dakujem\Middleware\Factory;

use Dakujem\Middleware\PredicateMiddleware;
use Dakujem\Middleware\GenericMiddleware;
use Dakujem\Middleware\TokenManipulators as Man;
use Dakujem\Middleware\TokenMiddleware;
use Psr\Http\Message\ResponseFactoryInterface as ResponseFactory;
Expand Down Expand Up @@ -67,7 +67,7 @@ public static function decodeTokens(
* @param ResponseFactory $responseFactory
* @param string|null $tokenAttribute
* @param string|null $errorAttribute
* @return PredicateMiddleware
* @return GenericMiddleware
*/
public static function assertTokens(
ResponseFactory $responseFactory,
Expand All @@ -84,7 +84,7 @@ public static function assertTokens(
* @param callable $inspector fn(Token,callable,callable):Response
* @param string|null $tokenAttribute
* @param string|null $errorAttribute
* @return PredicateMiddleware
* @return GenericMiddleware
*/
public static function inspectTokens(
ResponseFactory $responseFactory,
Expand Down
49 changes: 0 additions & 49 deletions src/PredicateMiddleware.php

This file was deleted.

42 changes: 0 additions & 42 deletions src/TokenManipulators.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,48 +127,6 @@ public static function attributeTokenProvider(string $attributeName = self::TOKE
};
}

/**
* Create a basic error responder that returns a response with 400 (Bad Request) status.
*
* @param ResponseFactory $responseFactory
* @param int|null $httpStatus HTTP response status, default is 400
* @return callable
*/
public static function basicErrorResponder(
ResponseFactory $responseFactory,
?int $httpStatus = null
): callable {
return function (/* Request $request */) use ($responseFactory, $httpStatus): Response {
return $responseFactory->createResponse($httpStatus ?? 400);
};
}

/**
* Create a function that reads a message from a Request attribute of choice
* and writes it as JSON to the Response body, setting correct content-type header.
*
* It can be used to transfer an error message from the Request to the Response for end user convenience.
* Warning: Opinionated and inflexible. You will probably want to use your own implementation.
*
* @param string $errorAttributeName
* @return callable fn(Request,Response):Response
*/
public static function errorMessagePassJson(string $errorAttributeName = self::ERROR_ATTRIBUTE_NAME): callable
{
return function (Request $request, Response $response) use ($errorAttributeName): Response {
// When this error handler is called and no error message is found, it assumes that no token was found.
$msg = $request->getAttribute($errorAttributeName) ?? 'No token found.';
$stream = $response->getBody();
/** @noinspection PhpComposerExtensionStubsInspection */
$stream !== null && $stream->write(json_encode([
'error' => [
'message' => $msg,
],
]));
return $response->withHeader('Content-type', 'application/json');
};
}

/**
* Write error message or error data as JSON to the Response.
* Also sets the Content-type header for JSON.
Expand Down
26 changes: 26 additions & 0 deletions tests/ComplexInteractionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,32 @@ class _ComplexInteractionTest extends TestCase
});
}

public function testRejectWithData()
{
$errorData = [
'message' => 'Sorry, the server turned into a teapot!',
'reason' => 'Well, the reason is unknown...',
];
$mw = fn() => [
AuthWizard::inspectTokens(
new ResponseFactory(),
function (object $token, callable $next, callable $withError) use ($errorData): Response {
return $withError($errorData)->withStatus(418); // reject the token
}
),
AuthWizard::decodeTokens($this->key),
];

// the token is indeed valid, but will be rejected by the inspector
$request = $this->req()->withHeader('Authorization', 'Bearer ' . $this->validToken());
self::check($mw(), $request, function (Request $request) {
throw new LogicException('The kernel should never be reached.');
}, function (Response $response) use ($errorData) {
Assert::same(418, $response->getStatusCode());
Assert::same(json_encode(['error' => $errorData]), $response->getBody()->getContents());
});
}

public function testAcceptByInspector()
{
$mw = fn() => [
Expand Down
71 changes: 0 additions & 71 deletions tests/ErrorMessagePassTest.phpt

This file was deleted.

43 changes: 0 additions & 43 deletions tests/ErrorResponderTest.phpt

This file was deleted.

91 changes: 0 additions & 91 deletions tests/PredicateMwTest.phpt

This file was deleted.

0 comments on commit c41a7fb

Please sign in to comment.