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

Using lcobucci/jwt v4.0 breaks iat claim #824

Closed
tsdevelopment opened this issue Jan 10, 2021 · 4 comments · Fixed by #826
Closed

Using lcobucci/jwt v4.0 breaks iat claim #824

tsdevelopment opened this issue Jan 10, 2021 · 4 comments · Fixed by #826

Comments

@tsdevelopment
Copy link

I think this issued could be almost the same as #803

public function onJWTCreated(JWTCreatedEvent $event)
{
    $payload['iat'] = time() - 15;
    $event->setData($payload);
}

Gives
Builder#withClaim() is meant to be used for non-registered claims, check the documentation on how to set claim "iat"

@chalasr
Copy link
Collaborator

chalasr commented Jan 10, 2021

Thank you for the report, I'm going to release a fix for this in the next couple of days.

@Ocramius
Copy link

Ocramius commented Jan 19, 2021

Similar issue over here: the problem happens in

public function create(array $payload, array $header = [])
{
if (class_exists(JWTBuilder::class)) {
$jws = new JWTBuilder(new JoseEncoder(), new MicrosecondBasedDateConversion());
} else {
$jws = new Builder();
}
foreach ($header as $k => $v) {
$jws->{$this->legacyJWTApi ? 'setHeader' : 'withHeader'}($k, $v);
}
$now = time();
if ($this->legacyJWTApi) {
$jws->setIssuedAt($now);
} else {
$jws->issuedAt($this->useDateObjects ? new \DateTimeImmutable("@{$now}") : $now);
}
if (null !== $this->ttl || isset($payload['exp'])) {
$exp = isset($payload['exp']) ? $payload['exp'] : $now + $this->ttl;
unset($payload['exp']);
if ($this->legacyJWTApi) {
$jws->setExpiration($exp);
} else {
$jws->expiresAt($exp instanceof \DateTimeImmutable ? $exp : ($this->useDateObjects ? new \DateTimeImmutable("@$exp") : $exp));
}
}
if (isset($payload['sub'])) {
$jws->{$this->legacyJWTApi ? 'setSubject' : 'relatedTo'}($payload['sub']);
unset($payload['sub']);
}
if (interface_exists(RegisteredClaims::class)) {
$this->addStandardClaims($jws, $payload);
}
foreach ($payload as $name => $value) {
if ($this->legacyJWTApi) {
$jws->set($name, $value);
} else {
$jws->{method_exists($jws,'with') ? 'with' : 'withClaim'}($name, $value);

Specifically, this line leads to the crash:

$jws->{method_exists($jws,'with') ? 'with' : 'withClaim'}($name, $value);

If 'iat' is passed as one of the values of the token, this crashes.

A relatively simplistic way to verify this is by decoding a token, extracting its data (which will contain "iat") and then re-encoding it: that leads to a crash.

@chalasr
Copy link
Collaborator

chalasr commented Jan 20, 2021

Thanks for the helpful details. Fix released in v2.10.6

@Ocramius
Copy link

Thanks @chalasr \o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants