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

Allow configuring leeway for the validation of jwt token dates #1304

Merged
merged 1 commit into from
Nov 17, 2022
Merged
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
16 changes: 12 additions & 4 deletions src/AuthorizationValidators/BearerTokenValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\Constraint\LooseValidAt;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\Constraint\ValidAt;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use League\OAuth2\Server\CryptKey;
Expand Down Expand Up @@ -43,12 +43,19 @@ class BearerTokenValidator implements AuthorizationValidatorInterface
*/
private $jwtConfiguration;

/**
* @var \DateInterval|null
*/
private $jwtValidAtDateLeeway;

/**
* @param AccessTokenRepositoryInterface $accessTokenRepository
* @param \DateInterval|null $jwtValidAtDateLeeway
*/
public function __construct(AccessTokenRepositoryInterface $accessTokenRepository)
public function __construct(AccessTokenRepositoryInterface $accessTokenRepository, \DateInterval $jwtValidAtDateLeeway = null)
{
$this->accessTokenRepository = $accessTokenRepository;
$this->jwtValidAtDateLeeway = $jwtValidAtDateLeeway;
}

/**
Expand All @@ -73,10 +80,11 @@ private function initJwtConfiguration()
InMemory::plainText('empty', 'empty')
);

$clock = new SystemClock(new DateTimeZone(\date_default_timezone_get()));
$this->jwtConfiguration->setValidationConstraints(
\class_exists(LooseValidAt::class)
? new LooseValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get())))
: new ValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get()))),
? new LooseValidAt($clock, $this->jwtValidAtDateLeeway)
: new ValidAt($clock, $this->jwtValidAtDateLeeway),
new SignedWith(
new Sha256(),
InMemory::plainText($this->publicKey->getKeyContents(), $this->publicKey->getPassPhrase() ?? '')
Expand Down
63 changes: 63 additions & 0 deletions tests/AuthorizationValidators/BearerTokenValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,67 @@ public function testBearerTokenValidatorRejectsExpiredToken()

$bearerTokenValidator->validateAuthorization($request);
}

public function testBearerTokenValidatorAcceptsExpiredTokenWithinLeeway()
{
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();

// We fake generating this token 10 seconds into the future, an extreme example of possible time drift between servers
$future = (new DateTimeImmutable())->add(new DateInterval('PT10S'));

$bearerTokenValidator = new BearerTokenValidator($accessTokenRepositoryMock, new \DateInterval('PT10S'));
$bearerTokenValidator->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));

$bearerTokenValidatorReflection = new ReflectionClass(BearerTokenValidator::class);
$jwtConfiguration = $bearerTokenValidatorReflection->getProperty('jwtConfiguration');
$jwtConfiguration->setAccessible(true);

$jwtTokenFromFutureWithinLeeway = $jwtConfiguration->getValue($bearerTokenValidator)->builder()
->permittedFor('client-id')
->identifiedBy('token-id')
->issuedAt($future)
->canOnlyBeUsedAfter($future)
->expiresAt((new DateTimeImmutable())->add(new DateInterval('PT1H')))
->relatedTo('user-id')
->withClaim('scopes', 'scope1 scope2 scope3 scope4')
->getToken(new Sha256(), InMemory::file(__DIR__ . '/../Stubs/private.key'));

$request = (new ServerRequest())->withHeader('authorization', \sprintf('Bearer %s', $jwtTokenFromFutureWithinLeeway->toString()));

$validRequest = $bearerTokenValidator->validateAuthorization($request);

$this->assertArrayHasKey('authorization', $validRequest->getHeaders());
}

public function testBearerTokenValidatorRejectsExpiredTokenBeyondLeeway()
{
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();

// We fake generating this token 10 seconds into the future, an extreme example of possible time drift between servers
Sephster marked this conversation as resolved.
Show resolved Hide resolved
$future = (new DateTimeImmutable())->add(new DateInterval('PT20S'));

$bearerTokenValidator = new BearerTokenValidator($accessTokenRepositoryMock, new \DateInterval('PT10S'));
$bearerTokenValidator->setPublicKey(new CryptKey('file://' . __DIR__ . '/../Stubs/public.key'));

$bearerTokenValidatorReflection = new ReflectionClass(BearerTokenValidator::class);
$jwtConfiguration = $bearerTokenValidatorReflection->getProperty('jwtConfiguration');
$jwtConfiguration->setAccessible(true);

$jwtTokenFromFutureBeyondLeeway = $jwtConfiguration->getValue($bearerTokenValidator)->builder()
->permittedFor('client-id')
->identifiedBy('token-id')
->issuedAt($future)
->canOnlyBeUsedAfter($future)
->expiresAt((new DateTimeImmutable())->add(new DateInterval('PT1H')))
->relatedTo('user-id')
->withClaim('scopes', 'scope1 scope2 scope3 scope4')
->getToken(new Sha256(), InMemory::file(__DIR__ . '/../Stubs/private.key'));

$request = (new ServerRequest())->withHeader('authorization', \sprintf('Bearer %s', $jwtTokenFromFutureBeyondLeeway->toString()));

$this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class);
$this->expectExceptionCode(9);

$bearerTokenValidator->validateAuthorization($request);
}
}