Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent bad design by using named constructors #467

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Bundle/JoseFramework/Resources/config/checkers.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
->public();

$container->set(ExpirationTimeChecker::class)
->factory([null, 'create'])
->arg('$clock', service('jose.internal_clock'))
->tag('jose.checker.claim', [
'alias' => 'exp',
Expand All @@ -44,6 +45,7 @@
]);

$container->set(IssuedAtChecker::class)
->factory([null, 'create'])
->arg('$clock', service('jose.internal_clock'))
->tag('jose.checker.claim', [
'alias' => 'iat',
Expand All @@ -53,6 +55,7 @@
]);

$container->set(NotBeforeChecker::class)
->factory([null, 'create'])
->arg('$clock', service('jose.internal_clock'))
->tag('jose.checker.claim', [
'alias' => 'nbf',
Expand Down
12 changes: 12 additions & 0 deletions src/Component/Checker/ExpirationTimeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ final class ExpirationTimeChecker implements ClaimChecker, HeaderChecker

private readonly ClockInterface $clock;

/**
* @private
* @deprecated since 3.3.0, to be private in 4.0.0. Please use named constructor `create` instead.
*/
public function __construct(
private readonly int $allowedTimeDrift = 0,
private readonly bool $protectedHeaderOnly = false,
Expand All @@ -33,6 +37,14 @@ public function __construct(
$this->clock = $clock;
}

public static function create(
ClockInterface $clock,
int $allowedTimeDrift = 0,
bool $protectedHeaderOnly = false,
): self {
return new self($allowedTimeDrift, $protectedHeaderOnly, $clock);
}

/**
* {@inheritdoc}
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Component/Checker/IssuedAtChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ final class IssuedAtChecker implements ClaimChecker, HeaderChecker

private readonly ClockInterface $clock;

/**
* @private
* @deprecated since 3.3.0, to be private in 4.0.0. Please use named constructor `create` instead.
*/
public function __construct(
private readonly int $allowedTimeDrift = 0,
private readonly bool $protectedHeaderOnly = false,
Expand All @@ -33,6 +37,14 @@ public function __construct(
$this->clock = $clock;
}

public static function create(
ClockInterface $clock,
int $allowedTimeDrift = 0,
bool $protectedHeaderOnly = false,
): self {
return new self($allowedTimeDrift, $protectedHeaderOnly, $clock);
}

/**
* {@inheritdoc}
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Component/Checker/NotBeforeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ final class NotBeforeChecker implements ClaimChecker, HeaderChecker

private readonly ClockInterface $clock;

/**
* @private
* @deprecated since 3.3.0, to be private in 4.0.0. Please use named constructor `create` instead.
*/
public function __construct(
private readonly int $allowedTimeDrift = 0,
private readonly bool $protectedHeaderOnly = false,
Expand All @@ -33,6 +37,14 @@ public function __construct(
$this->clock = $clock;
}

public static function create(
ClockInterface $clock,
int $allowedTimeDrift = 0,
bool $protectedHeaderOnly = false,
): self {
return new self($allowedTimeDrift, $protectedHeaderOnly, $clock);
}

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/Component/Checker/ClaimCheckerManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ private function getClaimCheckerManagerFactory(ClockInterface $clock = new MockC
{
if ($this->claimCheckerManagerFactory === null) {
$this->claimCheckerManagerFactory = new ClaimCheckerManagerFactory();
$this->claimCheckerManagerFactory->add('exp', new ExpirationTimeChecker(clock: $clock));
$this->claimCheckerManagerFactory->add('iat', new IssuedAtChecker(clock: $clock));
$this->claimCheckerManagerFactory->add('nbf', new NotBeforeChecker(clock: $clock));
$this->claimCheckerManagerFactory->add('exp', ExpirationTimeChecker::create(clock: $clock));
$this->claimCheckerManagerFactory->add('iat', IssuedAtChecker::create(clock: $clock));
$this->claimCheckerManagerFactory->add('nbf', NotBeforeChecker::create(clock: $clock));
$this->claimCheckerManagerFactory->add('aud', new AudienceChecker('My Service'));
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Component/Checker/ExpirationTimeClaimCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function theExpirationTimeClaimMustBeAnInteger(): void
$this->expectExceptionMessage('"exp" must be an integer.');

$clock = new MockClock();
$checker = new ExpirationTimeChecker(clock: $clock);
$checker = ExpirationTimeChecker::create(clock: $clock);
$checker->checkClaim('foo');
}

Expand All @@ -33,15 +33,15 @@ public function theExpirationTimeIsInThePast(): void
$this->expectExceptionMessage('The token expired.');

$clock = new MockClock();
$checker = new ExpirationTimeChecker(clock: $clock);
$checker = ExpirationTimeChecker::create(clock: $clock);
$checker->checkClaim($clock->now()->getTimestamp() - 1);
}

#[Test]
public function theExpirationTimeIsInTheFutur(): void
{
$clock = new MockClock();
$checker = new ExpirationTimeChecker(clock: $clock);
$checker = ExpirationTimeChecker::create(clock: $clock);
$checker->checkClaim($clock->now()->getTimestamp() + 3600);
static::assertSame('exp', $checker->supportedClaim());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Component/Checker/IssuedAtClaimCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function anIssuedAtClaimMustBeAnInteger(): void
$this->expectExceptionMessage('"iat" must be an integer.');

$clock = new MockClock();
$checker = new IssuedAtChecker(clock: $clock);
$checker = IssuedAtChecker::create(clock: $clock);
$checker->checkClaim('foo');
}

Expand All @@ -33,15 +33,15 @@ public function theIssuedAtClaimIsInTheFutur(): void
$this->expectExceptionMessage('The JWT is issued in the future.');

$clock = new MockClock();
$checker = new IssuedAtChecker(clock: $clock);
$checker = IssuedAtChecker::create(clock: $clock);
$checker->checkClaim($clock->now()->getTimestamp() + 3600);
}

#[Test]
public function theIssuedAtClaimIsInThePast(): void
{
$clock = new MockClock();
$checker = new IssuedAtChecker(clock: $clock);
$checker = IssuedAtChecker::create(clock: $clock);
$checker->checkClaim($clock->now()->getTimestamp() - 3600);
static::assertSame('iat', $checker->supportedClaim());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Component/Checker/NotBeforeClaimCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function theNotBeforeClaimMustBeAnInteger(): void
$this->expectExceptionMessage('"nbf" must be an integer.');

$clock = new MockClock();
$checker = new NotBeforeChecker(clock: $clock);
$checker = NotBeforeChecker::create(clock: $clock);
$checker->checkClaim('foo');
}

Expand All @@ -33,15 +33,15 @@ public function theNotBeforeClaimIsInTheFutur(): void
$this->expectExceptionMessage('The JWT can not be used yet.');

$clock = new MockClock();
$checker = new NotBeforeChecker(clock: $clock);
$checker = NotBeforeChecker::create(clock: $clock);
$checker->checkClaim($clock->now()->getTimestamp() + 3600);
}

#[Test]
public function theNotBeforeClaimIsInThePast(): void
{
$clock = new MockClock();
$checker = new NotBeforeChecker(clock: $clock);
$checker = NotBeforeChecker::create(clock: $clock);
$checker->checkClaim($clock->now()->getTimestamp() - 3600);
static::assertSame('nbf', $checker->supportedClaim());
}
Expand Down
Loading