Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade lcobucci/jwt to 5.x #2214

Open
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
build
coverage.xml
.phpunit.result.cache
.idea
36 changes: 3 additions & 33 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
{
"name": "tymon/jwt-auth",
"description": "JSON Web Token Authentication for Laravel and Lumen",
"keywords": [
"auth",
"authentication",
"json web token",
"jwt",
"laravel"
],
"homepage": "https://github.com/tymondesigns/jwt-auth",
"support": {
"issues": "https://github.com/tymondesigns/jwt-auth/issues",
"source": "https://github.com/tymondesigns/jwt-auth"
},
"name": "nexus/common",
"description": "JSON Web Token Authentication for Laravel",
"license": "MIT",
"authors": [
{
"name": "Sean Tymon",
"email": "[email protected]",
"homepage": "https://tymon.xyz",
"role": "Developer"
}
],
"require": {
"php": "^8.0",
"illuminate/auth": "^9.0|^10.0",
"illuminate/contracts": "^9.0|^10.0",
"illuminate/http": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0",
"lcobucci/jwt": "^4.0",
"lcobucci/jwt": "^5.0",
"nesbot/carbon": "^2.0"
},
"require-dev": {
Expand All @@ -49,10 +29,6 @@
}
},
"extra": {
"branch-alias": {
"dev-develop": "1.0-dev",
"dev-2.x": "2.0-dev"
},
"laravel": {
"aliases": {
"JWTAuth": "Tymon\\JWTAuth\\Facades\\JWTAuth",
Expand All @@ -63,12 +39,6 @@
]
}
},
"funding": [
{
"type": "patreon",
"url": "https://www.patreon.com/seantymon"
}
],
"config": {
"sort-packages": true
},
Expand Down
41 changes: 12 additions & 29 deletions src/Providers/JWT/Lcobucci.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,18 @@ protected function getBuilderFromClaims(array $payload): Builder
$builder = $this->config->builder();

foreach ($payload as $key => $value) {
switch ($key) {
case RegisteredClaims::ID:
$builder->identifiedBy($value);
break;
case RegisteredClaims::EXPIRATION_TIME:
$builder->expiresAt(DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::NOT_BEFORE:
$builder->canOnlyBeUsedAfter(DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::ISSUED_AT:
$builder->issuedAt(DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::ISSUER:
$builder->issuedBy($value);
break;
case RegisteredClaims::AUDIENCE:
$builder->permittedFor($value);
break;
case RegisteredClaims::SUBJECT:
$builder->relatedTo($value);
break;
default:
$builder->withClaim($key, $value);
}
$builder = match ($key) {
RegisteredClaims::ID => $builder->identifiedBy($value),
RegisteredClaims::EXPIRATION_TIME => $builder->expiresAt(DateTimeImmutable::createFromFormat('U',
$value)),
RegisteredClaims::NOT_BEFORE => $builder->canOnlyBeUsedAfter(DateTimeImmutable::createFromFormat('U',
$value)),
RegisteredClaims::ISSUED_AT => $builder->issuedAt(DateTimeImmutable::createFromFormat('U', $value)),
RegisteredClaims::ISSUER => $builder->issuedBy($value),
RegisteredClaims::AUDIENCE => $builder->permittedFor($value),
RegisteredClaims::SUBJECT => $builder->relatedTo($value),
default => $builder->withClaim($key, $value),
};
}

return $builder;
Expand Down Expand Up @@ -207,10 +194,6 @@ protected function getSigner()

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

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

return new $signer();
}

Expand Down
26 changes: 24 additions & 2 deletions src/Providers/JWT/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Tymon\JWTAuth\Providers\JWT;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Lcobucci\JWT\Signer\Key\InMemory;

abstract class Provider
{
Expand Down Expand Up @@ -137,7 +139,9 @@ public function getKeys()
*/
public function getPublicKey()
{
return Arr::get($this->keys, 'public');
$public = Arr::get($this->keys, 'public');

return $this->getKeyContents($public);
}

/**
Expand All @@ -147,7 +151,7 @@ public function getPublicKey()
*/
public function getPrivateKey()
{
return Arr::get($this->keys, 'private');
return Arr::get($this->keys, 'public');
}

/**
Expand Down Expand Up @@ -181,6 +185,24 @@ protected function getVerificationKey()
return $this->isAsymmetric() ? $this->getPublicKey() : $this->getSecret();
}

/**
* Properly load pem files
*
* @param string $key
*
* @return string
*/
protected function getKeyContents(string $key)
{
if (Str::of($key)->endsWith('.pem')) {
$key = InMemory::file(storage_path($key));
} else {
$key = InMemory::base64Encoded($key);
}

return $key->contents;
}

/**
* Determine if the algorithm is asymmetric, and thus requires a public/private key combo.
*
Expand Down