Skip to content

Commit

Permalink
Add specific exception
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed Nov 5, 2023
1 parent e5837b2 commit a2242b6
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"phpstan/phpstan": "^1.10.40",
"phpunit/phpunit": "^10.4.2",
"psalm/plugin-phpunit": "^0.18.4",
"thecodingmachine/safe": "^2.5",
"vimeo/psalm": "^5.15"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/DecodeToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Tuupola\Middleware;

use DateTimeImmutable;
use Exception;
use Lcobucci\JWT\Parser as ParserInterface;
use Lcobucci\JWT\Token\Plain;
use Psr\Log\LoggerInterface;
use Throwable;

/**

Check failure on line 13 in src/DecodeToken.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Found multi-line doc comment with single line content, use one-line doc comment instead.
* @internal
*/
final class DecodeToken
{
public function __construct(
Expand All @@ -23,23 +25,25 @@ public function __construct(
* Decode the token.
*
* @param non-empty-string $token
*
* @throw UnableToDecodeToken
*/
public function __invoke(string $token): Plain
{
try {
$tokenDecoded = $this->parser->parse($token);

if ($tokenDecoded->isExpired(new DateTimeImmutable())) {
throw new Exception('Token expired');
throw TokenExpired::create();
}

if (! ($tokenDecoded instanceof Plain)) {
throw new Exception('Token not signed');
throw TokenNotSigned::create();
}
} catch (Throwable $exception) {
$this->logger->warning($exception->getMessage(), [$token]);
$this->logger->warning($exception->getMessage(), ['token' => $token]);

throw $exception;
throw UnableToDecodeToken::create($exception);
}

return $tokenDecoded;
Expand Down
3 changes: 3 additions & 0 deletions src/FetchToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use function is_string;
use function preg_match;

/**

Check failure on line 14 in src/FetchToken.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Found multi-line doc comment with single line content, use one-line doc comment instead.
* @internal
*/
final class FetchToken
{
public function __construct(
Expand Down
3 changes: 1 addition & 2 deletions src/JwtAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;
use Throwable;

use function in_array;
use function sprintf;
Expand Down Expand Up @@ -52,7 +51,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

try {
$jwtDecodedToken = $this->decodeToken->__invoke($token);
} catch (Throwable) {
} catch (UnableToDecodeToken) {
return $handler->handle($request);
}

Expand Down
2 changes: 1 addition & 1 deletion src/JwtAuthentificationAcl.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->shouldAuthenticate($request) === false) {
if (! $this->shouldAuthenticate($request)) {
return $handler->handle($request);
}

Expand Down
15 changes: 15 additions & 0 deletions src/TokenExpired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tuupola\Middleware;

use RuntimeException;

class TokenExpired extends RuntimeException
{
public static function create(): self
{
return new self('Token expired');
}
}
15 changes: 15 additions & 0 deletions src/TokenNotSigned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tuupola\Middleware;

use RuntimeException;

class TokenNotSigned extends RuntimeException
{
public static function create(): self
{
return new self('Token not signed');
}
}
16 changes: 16 additions & 0 deletions src/UnableToDecodeToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Tuupola\Middleware;

use RuntimeException;
use Throwable;

class UnableToDecodeToken extends RuntimeException
{
public static function create(Throwable $previous): self
{
return new self('Unable to decode token.', previous: $previous);
}
}
6 changes: 3 additions & 3 deletions tests/Functional/JwtAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
use Tuupola\Tests\Middleware\Assets\TestBeforeHandlerHandler;

use function assert;
use function fopen;
use function json_encode;
use function Safe\fopen;

/** @psalm-suppress UnusedClass */
class JwtAuthenticationTest extends TestCase
Expand Down Expand Up @@ -273,7 +273,7 @@ public function testWronParser(): void
};

$logger = self::createMock(LoggerInterface::class);
$logger->expects(self::once())->method('warning')->with('Token not signed', [self::$acmeToken]);
$logger->expects(self::once())->method('warning')->with('Token not signed', ['token' => self::$acmeToken]);

$token = self::createMock(Token::class);
$token->expects(self::once())->method('isExpired')->willReturn(false);
Expand Down Expand Up @@ -382,7 +382,7 @@ public function testShouldReturn400WithExpiredToken(): void
};

$logger = self::createMock(LoggerInterface::class);
$logger->expects(self::once())->method('warning')->with('Token expired', [self::$expired]);
$logger->expects(self::once())->method('warning')->with('Token expired', ['token' => self::$expired]);
$logger->expects(self::once())->method('debug')->with('Using token from request header', []);

$option = JwtAuthenticationOption::create(InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw='));
Expand Down

0 comments on commit a2242b6

Please sign in to comment.