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

Fix Carbon not accepting string values #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mokhosh
Copy link

@mokhosh mokhosh commented Nov 11, 2024

Description

Carbon methods are strongly typed. So, you can't provide seconds as a string.
This is what you get if you try to override JWT_TTL in your .env:

Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given

This PR fixes the issue by casting the config after reading it from .env as is common in Laravel's config files.

Fixes #271

Checklist:

  • I've added tests for my changes or tests are not applicable
  • I've changed documentations or changes are not required
  • I've added my changes to CHANGELOG.md

Copy link
Contributor

@leoralph leoralph left a comment

Choose a reason for hiding this comment

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

LGTM, I just see a problem where people who already published their config files would still have this problem, maybe in a new major version we could use config()->integer() which would be a breaking change for who uses laravel versions <11.

@mokhosh
Copy link
Author

mokhosh commented Nov 12, 2024

LGTM, I just see a problem where people who already published their config files would still have this problem, maybe in a new major version we could use config()->integer() which would be a breaking change for who uses laravel versions <11.

I guess that's the case for anyone who publishes any vendor file, whether that's config or views etc.

They would need to sync with latest changes if they wanna keep getting the benefits.

@Messhias
Copy link
Collaborator

I'll wait if anyone else has something to say and approve. If not I'll merge it by the weekend.

Copy link
Contributor

@mfn mfn left a comment

Choose a reason for hiding this comment

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

I'm not saying the proposed changed here a wrong, but setTtl(), which receives the JWT_TTL config value already performs a cast

public function setTTL($ttl)
{
$this->ttl = $ttl ? (int) $ttl : $ttl;
return $this;

I rather see inconsistencies with other casts, e.g. we've

  • \PHPOpenSourceSaver\JWTAuth\Blacklist::setRefreshTTL which has a cast

    jwt-auth/src/Blacklist.php

    Lines 224 to 226 in 83653d5

    public function setRefreshTTL($ttl)
    {
    $this->refreshTTL = (int) $ttl;
  • but then \PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator::setRefreshTTL which has no cast
    public function setRefreshTTL($ttl)
    {
    $this->refreshTTL = $ttl;

Again, I'm not against this change but it feels like the fixes are better provided in other places, which would also make working with the library as API directly safer.

How exactly did you produce this, can you provide a full stacktrace?

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 this pull request may close these issues.

Changing JWT_TTL in .env doesn't work
4 participants