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

Handle claims conversion #171

Merged
merged 6 commits into from
Feb 23, 2017
Merged

Handle claims conversion #171

merged 6 commits into from
Feb 23, 2017

Conversation

lcobucci
Copy link
Owner

There're some weird stuff to improve @Ocramius (and I couldn't resist adding the last commit 😂).

Fixes #144
Fixes #43

@lcobucci lcobucci added this to the 4.0.0 milestone Feb 17, 2017
@lcobucci lcobucci self-assigned this Feb 17, 2017
@lcobucci lcobucci requested a review from Ocramius February 17, 2017 17:10

private function formatClaims(array $claims): array
{
if (isset($claims[RegisteredClaims::AUDIENCE]) && count($claims[RegisteredClaims::AUDIENCE]) === 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't sneak in this optimisation here :-P

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you assume that it always is a packed array, simply use an isset() on index 0 and another one on index 1

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's definitely better then count() in that case

return $claims;
}

private function convertDate(DateTimeImmutable $date)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type declaration needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it's int|string. Can't it be float?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that json_encode() does some trickery around floats, making them integers when no part is after the comma

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

@lcobucci lcobucci Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was with the old behaviour in my mind: https://3v4l.org/1Q7hh

Then when we convert it back to a DateTime we would have DateTimeImmutable::createFromFormat('U.u', '1487372450.0231') instead of DateTimeImmutable::createFromFormat('U.u', '1487372450.023126'), what could cause some weird errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you mean. If you can, you should cover the edge case you just mentioned in the test suite

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But can be more explicit, ofc

$seconds = $date->format('U');
$microseconds = $date->format('u');

return $microseconds === '000000' ? (int) $seconds : $seconds . '.' . $microseconds;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big assumption on the number of decimal digits. Instead, use 0 === (int) $microseconds

Copy link
Owner Author

@lcobucci lcobucci Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateTimeInterface#format('u') always returns a 6 chars string. It can be compared to 0 though, there's no much difference

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but nothing prevents the engine from making this 7 chars, or similar, in future versions. This code is very fragile

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed

return (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data));
$claims = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data));

if (isset($claims[RegisteredClaims::AUDIENCE])) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that casting null to (array) yields an empty array. I don't know if that's valid for $claims, but worth considering

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to set if it doesn't exist in the original token

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

}

foreach (RegisteredClaims::DATE_CLAIMS as $claim) {
if (!isset($claims[$claim])) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

areay_intersect_key()?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to use it I would also have to do an array_flip(RegisteredClaims::DATE_CLAIMS) (or an array_keys($claims) and use array_intersect() instead) not sure if will help us...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or define the constants in the keys ;-)

$value .= '.0';
}

return DateTimeImmutable::createFromFormat('U.u', $value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use two different expressions for when a microtime is set or not: this way you avoid overwriting $value

@@ -27,6 +27,8 @@
self::SUBJECT
];

const DATE_CLAIMS = [self::ISSUED_AT, self::NOT_BEFORE, self::EXPIRATION_TIME];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One per line, please

@lcobucci lcobucci force-pushed the handle-claims-conversion branch from 31a7d7f to f7f7c64 Compare February 19, 2017 09:26
@lcobucci lcobucci force-pushed the handle-claims-conversion branch from f7f7c64 to c74ef19 Compare February 19, 2017 09:29
@lcobucci lcobucci dismissed Ocramius’s stale review February 19, 2017 09:30

Processed 😉

@lcobucci
Copy link
Owner Author

@Ocramius could you please review it again?

@lcobucci lcobucci force-pushed the handle-claims-conversion branch from c74ef19 to ee3669e Compare February 19, 2017 10:15
@Ocramius Ocramius merged commit 6dbb242 into master Feb 23, 2017
@lcobucci lcobucci deleted the handle-claims-conversion branch February 23, 2017 22:26
@lcobucci lcobucci mentioned this pull request Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants