Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr committed May 5, 2024
1 parent bbea924 commit 66af300
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 99 deletions.
4 changes: 2 additions & 2 deletions DependencyInjection/Security/Factory/JWTUserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
final class JWTUserFactory implements UserProviderFactoryInterface
{
public function create(ContainerBuilder $container, $id, $config): void
public function create(ContainerBuilder $container, string $id, array $config): void
{
$container->setDefinition($id, new ChildDefinition('lexik_jwt_authentication.security.jwt_user_provider'))
->replaceArgument(0, $config['class']);
Expand All @@ -37,7 +37,7 @@ public function addConfiguration(NodeDefinition $node): void
->cannotBeEmpty()
->defaultValue(JWTUser::class)
->validate()
->ifTrue(fn ($class) => !(new \ReflectionClass($class))->implementsInterface(JWTUserInterface::class))
->ifTrue(fn ($class) => !is_subclass_of($class, JWTUserInterface::class))
->thenInvalid('The %s class must implement ' . JWTUserInterface::class . ' for using the "lexik_jwt" user provider.')
->end()
->end()
Expand Down
2 changes: 0 additions & 2 deletions Event/AuthenticationFailureEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
class AuthenticationFailureEvent extends Event
{
protected AuthenticationException $exception;

protected ?Response $response;

protected ?Request $request;

public function __construct(?AuthenticationException $exception, ?Response $response, ?Request $request = null)
Expand Down
11 changes: 4 additions & 7 deletions Event/BeforeJWEComputationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
*/
class BeforeJWEComputationEvent
{
/**
* @var array<string, mixed>
*/
private $header;

/**
* @param array<string, mixed> $header
*/
public function __construct(array $header)
{
$this->header = $header;
}

/**
* @param mixed $value
*/
public function setHeader(string $key, $value): self
public function setHeader(string $key, mixed $value): self
{
$this->header[$key] = $value;

Expand Down
1 change: 0 additions & 1 deletion Event/JWTDecodedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
class JWTDecodedEvent extends Event
{
protected array $payload;

protected bool $isValid;

public function __construct(array $payload)
Expand Down
4 changes: 2 additions & 2 deletions EventListener/RejectBlockedTokenListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public function __construct(BlockedTokenManagerInterface $blockedTokenManager)
}

/**
* @throws InvalidTokenException if the JWT is blocked
* @throws InvalidTokenException If the JWT is blocked
*/
public function __invoke(JWTAuthenticatedEvent $event): void
{
try {
if ($this->blockedTokenManager->has($event->getPayload())) {
throw new InvalidTokenException('JWT blocked');
}
} catch (MissingClaimException $e) {
} catch (MissingClaimException) {
// Do nothing if the required claims do not exist on the payload (older JWTs won't have the "jti" claim the manager requires)
}
}
Expand Down
2 changes: 0 additions & 2 deletions Helper/JWTSplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
class JWTSplitter
{
private string $header;

private string $payload;

private string $signature;

/**
Expand Down
1 change: 0 additions & 1 deletion OpenApi/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
class OpenApiFactory implements OpenApiFactoryInterface
{
private OpenApiFactoryInterface $decorated;

private string $checkPath;
private string $usernamePath;
private string $passwordPath;
Expand Down
11 changes: 10 additions & 1 deletion Response/JWTAuthenticationFailureResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\Response;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -11,7 +12,7 @@
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class JWTAuthenticationFailureResponse extends JWTCompatAuthenticationFailureResponse
final class JWTAuthenticationFailureResponse extends JsonResponse
{
private string $message;

Expand All @@ -22,6 +23,14 @@ public function __construct(string $message = 'Bad credentials', int $statusCode
parent::__construct(null, $statusCode, ['WWW-Authenticate' => 'Bearer']);
}

/**
* Sets the response data with the statusCode & message included.
*/
public function setData(mixed $data = []): static
{
return parent::setData((array)$data + ["code" => $this->statusCode, "message" => $this->getMessage()]);
}

/**
* Sets the failure message.
*/
Expand Down
58 changes: 0 additions & 58 deletions Response/JWTCompatAuthenticationFailureResponse.php

This file was deleted.

10 changes: 2 additions & 8 deletions Services/JWSProvider/JWSProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ interface JWSProviderInterface
{
/**
* Creates a new JWS signature from a given payload.
*
* @return CreatedJWS
*/
public function create(array $payload, array $header = []);
public function create(array $payload, array $header = []): CreatedJWS;

/**
* Loads an existing JWS signature from a given JWT token.
*
* @param string $token
*
* @return LoadedJWS
*/
public function load($token);
public function load(string $token): LoadedJWS;
}
4 changes: 2 additions & 2 deletions Services/JWSProvider/LcobucciJWSProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(KeyLoaderInterface $keyLoader, string $signatureAlgo
/**
* {@inheritdoc}
*/
public function create(array $payload, array $header = [])
public function create(array $payload, array $header = []): CreatedJWS
{
$jws = new JWTBuilder(new JoseEncoder(), ChainedFormatter::default());

Expand Down Expand Up @@ -109,7 +109,7 @@ public function create(array $payload, array $header = [])
/**
* {@inheritdoc}
*/
public function load($token)
public function load(string $token): LoadedJWS
{
$jws = (new JWTParser(new JoseEncoder()))->parse($token);

Expand Down
4 changes: 2 additions & 2 deletions Services/JWTManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private function generateJwtStringAndDispatchEvents(UserInterface $user, array $
*
* @throws JWTDecodeFailureException
*/
public function decode(TokenInterface $token)
public function decode(TokenInterface $token): array|bool
{
if (!($payload = $this->jwtEncoder->decode($token->getCredentials()))) {
return false;
Expand Down Expand Up @@ -135,7 +135,7 @@ public function parse(string $token): array
* Add user identity to payload, username by default.
* Override this if you need to identify it by another property.
*/
protected function addUserIdentityToPayload(UserInterface $user, array &$payload)
protected function addUserIdentityToPayload(UserInterface $user, array &$payload): void
{
$accessor = PropertyAccess::createPropertyAccessor();
$payload[$this->userIdClaim] = $accessor->getValue($user, $accessor->isReadable($user, $this->userIdClaim) ? $this->userIdClaim : 'user_identifier');
Expand Down
2 changes: 1 addition & 1 deletion Services/JWTTokenManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function createFromPayload(UserInterface $user, array $payload = []): str
* @return array|false The JWT token payload or false if an error occurs
* @throws JWTDecodeFailureException
*/
public function decode(TokenInterface $token);
public function decode(TokenInterface $token): array|bool;

/**
* Parses a raw JWT token and returns its payload
Expand Down
6 changes: 1 addition & 5 deletions Services/KeyLoader/KeyLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
interface KeyLoaderInterface
{
public const TYPE_PUBLIC = 'public';

public const TYPE_PRIVATE = 'private';

/**
Expand All @@ -22,10 +21,7 @@ interface KeyLoaderInterface
*/
public function loadKey($type);

/**
* @return string|null
*/
public function getPassphrase();
public function getPassphrase(): ?string;

public function getSigningKey(): ?string;

Expand Down
5 changes: 0 additions & 5 deletions Services/WebToken/AccessTokenLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
final class AccessTokenLoader
{
private $jwsLoader;

private $jwsHeaderCheckerManager;

private $claimCheckerManager;

private $jweLoader;

private $signatureKeyset;

private $encryptionKeyset;

/**
Expand Down

0 comments on commit 66af300

Please sign in to comment.