-
Hi guys, I'm working on a piece of code that should accept a JWT generated by another library. I've set up the SignedWith constraint and it's working fine, but i'm struggling with the expiration. Here's an example of the token I get: And here's my code: try {
$signer = new Sha512();
$key = InMemory::plainText('secret');
$config = Configuration::forSymmetricSigner(
$signer,
$key
);
$legacyToken = $request->request->get('legacyToken');
if (!$legacyToken) {
throw new BadRequestHttpException($this->translator->trans('missing_legacy_token_in_request', [], 'auth'));
}
$decodedToken = $config->parser()->parse($legacyToken);
assert($decodedToken instanceof UnencryptedToken);
$clock = new SystemClock(new \DateTimeZone('UTC'));
$config->setValidationConstraints(new StrictValidAt($clock));
$config->setValidationConstraints(new SignedWith($signer, $key));
$constraints = $config->validationConstraints();
$config->validator()->assert($decodedToken, ...$constraints);
} catch (RequiredConstraintsViolated $e) {
return $this->json(['error' => $this->translator->trans('invalid_legacy_token_in_request', [], 'auth')], Response::HTTP_UNAUTHORIZED);
} problem is: I can put any datetime in the token, this code will always consider it as "valid". What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
As the method name suggests, The following edit is enough: $config->setValidationConstraints(
new StrictValidAt($clock),
new SignedWith($signer, $key)
); The upcoming version |
Beta Was this translation helpful? Give feedback.
-
Indeed, I did not thought about that. To be honest, I found the documentation very "light" on this topic (= no mention of setValidationConstraint() and how to use it. I had to guess by looking in the library code). It would probably be a good idea to update it ;) Thanks a lot! |
Beta Was this translation helpful? Give feedback.
As the method name suggests,
$config->setValidationConstraints
doesn't append constraints, it sets them, so the last call overwrites the previous ones.The following edit is enough:
The upcoming version
v4.2
will contain a simplified API that should ease DX like this.