Skip to content

Commit

Permalink
♻️ upgrade lcobucci/jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tini committed Jul 20, 2021
1 parent 2c9a3f5 commit e1509b7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/JwtRequestAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use DateTimeImmutable;
use Exception;
use Illuminate\Http\Request;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\Validator;
use MyParcelCom\AuthModule\Interfaces\RequestAuthenticatorInterface;
use MyParcelCom\JsonApi\Exceptions\InvalidAccessTokenException;
use MyParcelCom\JsonApi\Exceptions\MissingTokenException;
Expand All @@ -31,12 +30,17 @@ class JwtRequestAuthenticator implements RequestAuthenticatorInterface
public function authenticate(Request $request): Token
{
try {
$parsedToken = (new Parser())->parse(
$config = Configuration::forSymmetricSigner(
new Sha256(),
InMemory::plainText($this->getPublicKey())
);

$parsedToken = $config->parser()->parse(
$this->getTokenString($request)
);

$constraint = new SignedWith(new Sha256(), new Key($this->getPublicKey()));
$valid = (new Validator())->validate($parsedToken, $constraint);
$constraint = new SignedWith($config->signer(), $config->signingKey());
$valid = $config->validator()->validate($parsedToken, $constraint);

if (!$valid) {
throw new InvalidAccessTokenException('Token could not be verified');
Expand Down
2 changes: 2 additions & 0 deletions tests/JwtRequestAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function testAuthenticateWithInvalidSignature()
{
$privateKeyResource = openssl_pkey_new(['private_key_bits' => 1024]);
openssl_pkey_export($privateKeyResource, $this->privateKey);
$this->generateKeys();

$authorizationHeader = 'Bearer ' . $this->createTokenString([], null, 'some-user-id', []);
$request = Mockery::mock(Request::class, ['header' => $authorizationHeader, 'has' => false]);

Expand Down
20 changes: 14 additions & 6 deletions tests/Traits/AccessTokenTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use DateTimeImmutable;
use Illuminate\Http\Request;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token;

Expand All @@ -20,6 +19,9 @@ trait AccessTokenTrait
/** @var string */
protected $publicKey;

/** @var Configuration */
protected $config;

/**
* Generate RSA keys.
*
Expand All @@ -34,6 +36,12 @@ protected function generateKeys(bool $overrideConfig = false)
if ($overrideConfig) {
config(['auth.public_key' => $this->publicKey]);
}

$this->config = Configuration::forAsymmetricSigner(
new Sha256(),
InMemory::plainText($this->privateKey),
InMemory::plainText($this->publicKey)
);
}

/**
Expand All @@ -51,7 +59,7 @@ protected function createTokenString(
string $userId = '',
array $claims = []
): string {
$builder = new Builder();
$builder = $this->config->builder();
$builder
->withClaim('user_id', $userId)
->withClaim('scope', implode(' ', $scopes));
Expand All @@ -64,7 +72,7 @@ protected function createTokenString(
$builder->expiresAt((new DateTimeImmutable())->setTimestamp($expiration));
}

return (string) $builder->getToken(new Sha256(), new Key($this->privateKey));
return $builder->getToken($this->config->signer(), $this->config->signingKey())->toString();
}

/**
Expand All @@ -84,7 +92,7 @@ protected function createParsedToken(
): Token {
$tokenString = $this->createTokenString($scopes, $expiration, $userId, $claims);

return (new Parser())->parse($tokenString);
return $this->config->parser()->parse($tokenString);
}

/**
Expand Down

0 comments on commit e1509b7

Please sign in to comment.