Laravel Passport将exp、iat和nbf的通道类型转换为int或float



更新到10.1.0版本后,我现在在另一个验证jwt令牌的服务中遇到问题。我注意到令牌值从更改

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

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

有人知道如何去掉这里的小数吗?

在Passport^10 更改索赔格式

步骤1。使AccessToken类成为

<?php
namespace AppPassport;

use DateTimeImmutable;
use LcobucciJWTConfiguration;
use LcobucciJWTSignerKeyInMemory;
use LcobucciJWTSignerKeyLocalFileReference;
use LeagueOAuth2ServerCryptKey;
use LcobucciJWTSignerRsaSha256;
use LaravelPassportBridgeAccessToken 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());
}
}

步骤2。使AccessTokenRepository类成为

<?php
namespace AppRepositories;
use AppPassportAccessToken;
use LaravelPassportBridgeAccessTokenRepository as BaseRepository;
use LeagueOAuth2ServerEntitiesClientEntityInterface;
class AccessTokenRepository extends BaseRepository {
public function getNewToken( ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null ) {
return new AccessToken( $userIdentifier, $scopes, $clientEntity );
}
}

步骤3。在config/app.php 中添加提供程序

'providers' => [
...

/*
* Application Service Providers...
*/

AppProvidersPassportServiceProvider::class,
],

步骤4。使提供商类别

<?php
namespace AppProviders;
use AppRepositoriesAccessTokenRepository;
use LaravelPassportBridgeClientRepository;
use LaravelPassportBridgeScopeRepository;
use LeagueOAuth2ServerAuthorizationServer;
class PassportServiceProvider extends LaravelPassportPassportServiceProvider {

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()
);
}
}

步骤5。Make ClaimsFormatter类

<?php
namespace AppPassport;
use DateTimeImmutable;
use LcobucciJWTClaimsFormatter;
use LcobucciJWTTokenRegisteredClaims;
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');
}
}

我可以确认,这对我有效。此外,要回答@pedrorocha

您可以在app/Passport/AccessToken.php第50行编辑代码,并从更改

InMemory::plainText('')

InMemory::plainText('empty', 'empty')

在本期github中感谢silverl希望这能有所帮助,谢谢。

最新更新