gpt4 book ai didi

Laravel Passport 将 exp、iat 和 nbf 的类型更改为 int 或 float

转载 作者:行者123 更新时间:2023-12-04 08:28:39 26 4
gpt4 key购买 nike

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

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

"iat": "1607005988.812500",
"nbf": "1607005988.812513",
"exp": "1638541988.293214",
有人知道如何在这里删除小数吗?

最佳答案

在 Passport ^10 处更改 ClaimsFormatter
步骤 1. 制作 AccessToken 类

 <?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());
}
}
步骤 2. 创建 AccessTokenRepository 类
<?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 );
}
}
步骤 3. 在 config/app.php 中添加提供者
'providers' => [
...

/*
* Application Service Providers...
*/

App\Providers\PassportServiceProvider::class,
],
步骤 4. 制作提供者类
<?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()
);
}

}
步骤 5. 创建 ClaimsFormatter 类
<?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');
}
}

关于Laravel Passport 将 exp、iat 和 nbf 的类型更改为 int 或 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65129092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com