Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed Sep 27, 2023
1 parent ae405bc commit ae0e2a7
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/DecodeToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
*
* @param non-empty-string $token
*/
public function __invoke(string $token): JwtDecodedToken
public function __invoke(string $token): Plain
{
try {
$tokenDecoded = $this->parser->parse($token);
Expand All @@ -42,6 +42,6 @@ public function __invoke(string $token): JwtDecodedToken
throw $exception;
}

return new JwtDecodedToken($tokenDecoded, $token);
return $tokenDecoded;
}
}
4 changes: 2 additions & 2 deletions src/JwtAuthentication/NullAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Tuupola\Middleware\JwtAuthentication;

use Lcobucci\JWT\Token\Plain;
use Psr\Http\Message\ResponseInterface;
use Tuupola\Middleware\JwtAuthentificationAfter;
use Tuupola\Middleware\JwtDecodedToken;

class NullAfter implements JwtAuthentificationAfter
{
public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecodedToken): ResponseInterface
public function __invoke(ResponseInterface $response, Plain $token): ResponseInterface
{
return $response;
}
Expand Down
4 changes: 2 additions & 2 deletions src/JwtAuthentication/NullBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Tuupola\Middleware\JwtAuthentication;

use Lcobucci\JWT\Token\Plain;
use Psr\Http\Message\ServerRequestInterface;
use Tuupola\Middleware\JwtAuthentificationBefore;
use Tuupola\Middleware\JwtDecodedToken;

class NullBefore implements JwtAuthentificationBefore
{
public function __invoke(ServerRequestInterface $request, JwtDecodedToken $jwtDecodedToken): ServerRequestInterface
public function __invoke(ServerRequestInterface $request, Plain $token): ServerRequestInterface
{
return $request;
}
Expand Down
3 changes: 2 additions & 1 deletion src/JwtAuthentificationAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Tuupola\Middleware;

use Lcobucci\JWT\Token\Plain;
use Psr\Http\Message\ResponseInterface;

interface JwtAuthentificationAfter
{
public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecodedToken): ResponseInterface;
public function __invoke(ResponseInterface $response, Plain $token): ResponseInterface;
}
3 changes: 2 additions & 1 deletion src/JwtAuthentificationBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Tuupola\Middleware;

use Lcobucci\JWT\Token\Plain;
use Psr\Http\Message\ServerRequestInterface;

interface JwtAuthentificationBefore
{
public function __invoke(ServerRequestInterface $request, JwtDecodedToken $jwtDecodedToken): ServerRequestInterface;
public function __invoke(ServerRequestInterface $request, Plain $token): ServerRequestInterface;
}
14 changes: 0 additions & 14 deletions src/JwtDecodedToken.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Assets/TestAfterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@

namespace Tuupola\Tests\Middleware\Assets;

use Lcobucci\JWT\Token\Plain;
use Psr\Http\Message\ResponseInterface;
use Tuupola\Middleware\JwtAuthentificationAfter;
use Tuupola\Middleware\JwtDecodedToken;

class TestAfterHandler implements JwtAuthentificationAfter
{
public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecodedToken): ResponseInterface
public function __invoke(ResponseInterface $response, Plain $token): ResponseInterface
{
$response->getBody()->write(self::class);

Expand Down
4 changes: 2 additions & 2 deletions tests/Assets/TestBeforeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@

namespace Tuupola\Tests\Middleware\Assets;

use Lcobucci\JWT\Token\Plain;
use Psr\Http\Message\ServerRequestInterface;
use Tuupola\Middleware\JwtAuthentificationBefore;
use Tuupola\Middleware\JwtDecodedToken;

class TestBeforeHandler implements JwtAuthentificationBefore
{
public function __invoke(ServerRequestInterface $request, JwtDecodedToken $jwtDecodedToken): ServerRequestInterface
public function __invoke(ServerRequestInterface $request, Plain $token): ServerRequestInterface
{
return $request->withAttribute('test', 'invoke');
}
Expand Down
34 changes: 16 additions & 18 deletions tests/Functional/JwtAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Plain;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -54,7 +55,6 @@
use Tuupola\Middleware\JwtAuthentificationAfter;
use Tuupola\Middleware\JwtAuthentificationBefore;
use Tuupola\Middleware\JwtAuthentificationError;
use Tuupola\Middleware\JwtDecodedToken;
use Tuupola\Tests\Middleware\Assets\TestAfterHandler;
use Tuupola\Tests\Middleware\Assets\TestBeforeHandler;
use Tuupola\Tests\Middleware\Assets\TestErrorHandler;
Expand Down Expand Up @@ -278,7 +278,7 @@ public function testShouldAlterResponseWithAnonymousAfter(): void

$option = JwtAuthenticationOption::create(InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw='))
->withAfter(new class implements JwtAuthentificationAfter {
public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecodedToken): ResponseInterface
public function __invoke(ResponseInterface $response, Plain $token): ResponseInterface
{
return $response->withHeader('X-Brawndo', 'plants crave');
}
Expand Down Expand Up @@ -604,10 +604,10 @@ public function testShouldAttachToken(): void
$default = static function (ServerRequestInterface $request): ResponseInterface {
$decodedToken = $request->getAttribute('token');

assert($decodedToken instanceof JwtDecodedToken);
assert($decodedToken instanceof Plain);

$response = (new ResponseFactory())->createResponse();
$response->getBody()->write((string) json_encode($decodedToken->payload->claims()->get('iss')));
$response->getBody()->write((string) json_encode($decodedToken->claims()->get('iss')));
$response->getBody()->rewind();

return $response;
Expand Down Expand Up @@ -638,10 +638,10 @@ public function testShouldAttachCustomToken(): void
$default = static function (ServerRequestInterface $request): ResponseInterface {
$decodedToken = $request->getAttribute('nekot');

assert($decodedToken instanceof JwtDecodedToken);
assert($decodedToken instanceof Plain);

$response = (new ResponseFactory())->createResponse();
$response->getBody()->write((string) json_encode($decodedToken->payload->claims()->get('iss')));
$response->getBody()->write((string) json_encode($decodedToken->claims()->get('iss')));

return $response;
};
Expand Down Expand Up @@ -678,9 +678,9 @@ public function testShouldCallAfterWithProperArguments(): void

$option = JwtAuthenticationOption::create(InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw='))
->withAfter(new class implements JwtAuthentificationAfter {
public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecodedToken): ResponseInterface
public function __invoke(ResponseInterface $response, Plain $token): ResponseInterface
{
return $response->withHeader('decoded', (string) json_encode($jwtDecodedToken->payload->claims()->get('iss')))->withHeader('token', $jwtDecodedToken->token);
return $response->withHeader('decoded', (string) json_encode($token->claims()->get('iss')));
}
});

Expand All @@ -694,7 +694,6 @@ public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecode
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('Success', $response->getBody());
self::assertJsonStringEqualsJsonString((string) json_encode(self::$acmeTokenArray['iss']), $response->getHeaderLine('decoded'));
self::assertEquals(self::$acmeToken, $response->getHeaderLine('token'));
}

public function testShouldCallBeforeWithProperArguments(): void
Expand All @@ -705,17 +704,16 @@ public function testShouldCallBeforeWithProperArguments(): void

$default = static function (ServerRequestInterface $request): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
$response->getBody()->write('Success' . (string) json_encode($request->getAttribute('decoded')) . (string) json_encode($request->getAttribute('token')));
$response->getBody()->write('Success' . (string) json_encode($request->getAttribute('decoded')));

return $response;
};

$option = JwtAuthenticationOption::create(InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw='))
->withBefore(new class implements JwtAuthentificationBefore {
public function __invoke(ServerRequestInterface $request, JwtDecodedToken $jwtDecodedToken): ServerRequestInterface
public function __invoke(ServerRequestInterface $request, Plain $token): ServerRequestInterface
{
return $request->withAttribute('decoded', (string) json_encode($jwtDecodedToken->payload->claims()->get('iss')))
->withAttribute('token', $jwtDecodedToken->token);
return $request->withAttribute('decoded', (string) json_encode($token->claims()->get('iss')));
}
});

Expand All @@ -730,7 +728,7 @@ public function __invoke(ServerRequestInterface $request, JwtDecodedToken $jwtDe

$body->rewind();
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('Success' . (string) json_encode(json_encode(self::$acmeTokenArray['iss'])) . (string) json_encode(self::$acmeToken), $body->getContents());
self::assertEquals('Success' . (string) json_encode(json_encode(self::$acmeTokenArray['iss'])), $body->getContents());
}

public function testShouldCallAnonymousErrorFunction(): void
Expand Down Expand Up @@ -868,7 +866,7 @@ public function testShouldReturn401FromAfter(): void

$option = JwtAuthenticationOption::create(InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw='))
->withAfter(new class implements JwtAuthentificationAfter {
public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecodedToken): ResponseInterface
public function __invoke(ResponseInterface $response, Plain $token): ResponseInterface
{
return $response
->withBody((new StreamFactory())->createStream())
Expand Down Expand Up @@ -905,7 +903,7 @@ public function testShouldModifyRequestUsingAnonymousBefore(): void

$option = JwtAuthenticationOption::create(InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw='))
->withBefore(new class implements JwtAuthentificationBefore {
public function __invoke(ServerRequestInterface $request, JwtDecodedToken $jwtDecodedToken): ServerRequestInterface
public function __invoke(ServerRequestInterface $request, Plain $token): ServerRequestInterface
{
return $request->withAttribute('test', 'test');
}
Expand Down Expand Up @@ -1047,15 +1045,15 @@ public function testShouldBindToMiddleware(): void

$option = JwtAuthenticationOption::create(InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw='))
->withAfter(new class implements JwtAuthentificationAfter {
public function __invoke(ResponseInterface $response, JwtDecodedToken $jwtDecodedToken): ResponseInterface
public function __invoke(ResponseInterface $response, Plain $token): ResponseInterface
{
$response->getBody()->write('im after');

return $response;
}
})
->withBefore(new class implements JwtAuthentificationBefore {
public function __invoke(ServerRequestInterface $request, JwtDecodedToken $jwtDecodedToke): ServerRequestInterface
public function __invoke(ServerRequestInterface $request, Plain $token): ServerRequestInterface
{
return $request->withAttribute('before', 'im before');
}
Expand Down

0 comments on commit ae0e2a7

Please sign in to comment.