Skip to content

Commit

Permalink
update CHANGELOG-4.x.md
Browse files Browse the repository at this point in the history
  • Loading branch information
liyuzhao authored and liyuzhao committed Apr 15, 2022
1 parent ba46c04 commit ddedbde
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-4.x.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v4.0.6
### Feat
- 新增jwt可以自定义payload的sub、iss、aud字段

## v4.0.5
### Fixed
- [#51](https://github.com/phper666/jwt-auth/issues/51) 修复单点登录重新获取token,没有使原有用户token失效的问题
Expand Down
19 changes: 17 additions & 2 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Hyperf\Contract\ConfigInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use Lcobucci\Clock\SystemClock;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\DataSet;
use Lcobucci\JWT\Token\Plain;
use Lcobucci\JWT\Token\RegisteredClaimGiven;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\Validation\Constraint\IdentifiedBy;
use Lcobucci\JWT\Validation\Constraint\IssuedBy;
Expand Down Expand Up @@ -153,12 +155,23 @@ public function getToken(string $scene, array $claims): Plain
$clock = SystemClock::fromUTC();
$now = $clock->now();
$expiresAt = $clock->now()->modify('+' . $jwtSceneConfig['ttl'] . ' second');
$builder = $this->lcobucciJwtConfiguration->builder(ChainedFormatter::withUnixTimestampDates());
$builder = $this->lcobucciJwtConfiguration->builder(ChainedFormatter::withUnixTimestampDates())->issuedBy($issuedBy);
foreach ($claims as $k => $v) {
if ($k == RegisteredClaims::SUBJECT) {
$builder = $builder->relatedTo($v);
continue;
}
if ($k == RegisteredClaims::AUDIENCE) {
$builder = $builder->PermittedFor($v);
continue;
}
if ($k == RegisteredClaims::ISSUER) {
$builder = $builder->issuedBy($v);
continue;
}
$builder = $builder->withClaim($k, $v); // 自定义数据
}
$builder = $builder
->issuedBy($issuedBy)
// Configures the id (jti claim) 设置jwt的jti
->identifiedBy($uniqid)
// Configures the time that the token was issue (iat claim) 发布时间
Expand All @@ -167,6 +180,8 @@ public function getToken(string $scene, array $claims): Plain
->canOnlyBeUsedAfter($now)
// Configures the expiration time of the token (exp claim) 到期时间
->expiresAt($expiresAt);


$token = $builder->getToken($this->lcobucciJwtConfiguration->signer(), $this->lcobucciJwtConfiguration->signingKey());
if ($loginType == JWTConstant::SSO) {
$this->addTokenBlack($token, true);
Expand Down

0 comments on commit ddedbde

Please sign in to comment.