Laravel Passport chanage type of exp, iat and nbf to int or float

Viewed 311

After updating to version 10.1.0, I now get an issue in another service which verifies the jwt token. I noticed that token values are changed from

"iat": 1600409130,
"nbf": 1600409130,
"exp": 1631945129

to

"iat": "1607005988.812500",
"nbf": "1607005988.812513",
"exp": "1638541988.293214",

Does anybody know how to remove the decimals here?

1 Answers

Change ClaimsFormatter at Passport ^10

step 1. Make AccessToken class

 <?php

namespace App\Passport;


use DateTimeImmutable;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Key\LocalFileReference;
use League\OAuth2\Server\CryptKey;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Laravel\Passport\Bridge\AccessToken as BaseToken;

class AccessToken extends BaseToken {

    /**
     * @var CryptKey
     */
    private $privateKey;

    /**
     * @var Configuration
     */
    private $jwtConfiguration;

    /**
     * Set the private key used to encrypt this access token.
     */
    public function setPrivateKey( CryptKey $privateKey ) {
        $this->privateKey = $privateKey;
    }

    /**
     * Generate a string representation from the access token
     */
    public function __toString()
    {
        return $this->convertToJWT()->toString();
    }


    /**
     * Initialise the JWT Configuration.
     */
    public function initJwtConfiguration()
    {
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
            new Sha256(),
            LocalFileReference::file($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase() ?? ''),
            InMemory::plainText('')
        );
    }


    public function convertToJWT() {
        $this->initJwtConfiguration();

        return $this->jwtConfiguration->builder(new MicrosecondBasedDateConversion())
            ->permittedFor($this->getClient()->getIdentifier())
            ->identifiedBy($this->getIdentifier())
            ->issuedAt(new DateTimeImmutable())
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
            ->expiresAt($this->getExpiryDateTime())
            ->relatedTo((string) $this->getUserIdentifier())
            ->withClaim('scopes', $this->getScopes())
            ->withClaim('test',[])
            ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
    }
}

step 2. Make AccessTokenRepository class

<?php

namespace App\Repositories;

use App\Passport\AccessToken;
use Laravel\Passport\Bridge\AccessTokenRepository as BaseRepository;
use League\OAuth2\Server\Entities\ClientEntityInterface;

class AccessTokenRepository extends BaseRepository {

    public function getNewToken( ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null ) {
        return new AccessToken( $userIdentifier, $scopes, $clientEntity );
    }
}

step 3. add providers in config/app.php

'providers' => [
       ...
       
    /*
    * Application Service Providers...
    */
      
    App\Providers\PassportServiceProvider::class,
 ],

step 4. Make provider class

<?php

namespace App\Providers;

use App\Repositories\AccessTokenRepository;
use Laravel\Passport\Bridge\ClientRepository;
use Laravel\Passport\Bridge\ScopeRepository;
use League\OAuth2\Server\AuthorizationServer;

class PassportServiceProvider extends \Laravel\Passport\PassportServiceProvider {
  
    public function makeAuthorizationServer() {
    
        return new AuthorizationServer(
            $this->app->make( ClientRepository::class ),
            $this->app->make( AccessTokenRepository::class ),
            $this->app->make( ScopeRepository::class ),
            $this->makeCryptKey( 'private' ),
            app( 'encrypter' )->getKey()
        );
    }

}

step 5. Make ClaimsFormatter class

<?php

namespace App\Passport;

use DateTimeImmutable;
use Lcobucci\JWT\ClaimsFormatter;
use Lcobucci\JWT\Token\RegisteredClaims;

class MicrosecondBasedDateConversion implements ClaimsFormatter
{
    /** @inheritdoc */
    public function formatClaims(array $claims): array
    {
        foreach (RegisteredClaims::DATE_CLAIMS as $claim) {
            if (! array_key_exists($claim, $claims)) {
                continue;
            }

            $claims[$claim] = $this->convertDate($claims[$claim]);
        }

        return $claims;
    }

    /**
     * @param DateTimeImmutable $date
     * @return int
     */
    private function convertDate(DateTimeImmutable $date): int
    {
        return (int)$date->format('U');
    }
}
Related