Skip to content

Commit

Permalink
Merge pull request #248 from kellerjmrtn/bump-lcobucci-jwt-version
Browse files Browse the repository at this point in the history
Bump lcobucci jwt version
  • Loading branch information
Messhias authored May 14, 2024
2 parents fb882f5 + 49c13a9 commit d88bb28
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ You can find and compare releases at the GitHub release page.

## [Unreleased]

### Added
- Support for lcobucci/jwt^5.0 (and dropped support for ^4.0)

## [2.3.0] 2024-05-09

### Added
- Support for Carbon 3 (and drop Carbon 1, but it was unused anyway)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"illuminate/contracts": "^10|^11",
"illuminate/http": "^10|^11",
"illuminate/support": "^10|^11",
"lcobucci/jwt": "^4.0",
"lcobucci/jwt": "^5.0",
"namshi/jose": "^7.0",
"nesbot/carbon": "^2.0|^3.0"
},
Expand Down
47 changes: 12 additions & 35 deletions src/Providers/JWT/Lcobucci.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function encode(array $payload)

try {
foreach ($payload as $key => $value) {
$this->addClaim($key, $value);
$this->builder = $this->addClaim($key, $value);
}

return $this->builder->getToken($this->config->signer(), $this->config->signingKey())->toString();
Expand Down Expand Up @@ -179,41 +179,22 @@ public function decode($token)
*
* @param string $key
*/
protected function addClaim($key, $value)
protected function addClaim($key, $value): Builder
{
if (!isset($this->builder)) {
$this->builder = $this->config->builder();
}

switch ($key) {
case RegisteredClaims::ID:
$this->builder->identifiedBy($value);
break;
case RegisteredClaims::EXPIRATION_TIME:
$this->builder->expiresAt(\DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::NOT_BEFORE:
$this->builder->canOnlyBeUsedAfter(\DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::ISSUED_AT:
$this->builder->issuedAt(\DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::ISSUER:
$this->builder->issuedBy($value);
break;
case RegisteredClaims::AUDIENCE:
if (is_array($value)) {
$this->builder->permittedFor(...$value);
} else {
$this->builder->permittedFor($value);
}
break;
case RegisteredClaims::SUBJECT:
$this->builder->relatedTo($value);
break;
default:
$this->builder->withClaim($key, $value);
}
return match ($key) {
RegisteredClaims::ID => $this->builder->identifiedBy($value),
RegisteredClaims::EXPIRATION_TIME => $this->builder->expiresAt(\DateTimeImmutable::createFromFormat('U', $value)),
RegisteredClaims::NOT_BEFORE => $this->builder->canOnlyBeUsedAfter(\DateTimeImmutable::createFromFormat('U', $value)),
RegisteredClaims::ISSUED_AT => $this->builder->issuedAt(\DateTimeImmutable::createFromFormat('U', $value)),
RegisteredClaims::ISSUER => $this->builder->issuedBy($value),
RegisteredClaims::AUDIENCE => is_array($value) ? $this->builder->permittedFor(...$value) : $this->builder->permittedFor($value),
RegisteredClaims::SUBJECT => $this->builder->relatedTo($value),
default => $this->builder->withClaim($key, $value),
};
}

/**
Expand All @@ -231,10 +212,6 @@ protected function getSigner()

$signer = $this->signers[$this->algo];

if (is_subclass_of($signer, Ecdsa::class)) {
return $signer::create();
}

return new $signer();
}

Expand Down

0 comments on commit d88bb28

Please sign in to comment.