diff --git a/.gitignore b/.gitignore index f6b4976..bd4660f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ composer.phar composer.lock /vendor/ -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.phpunit.cache/ \ No newline at end of file diff --git a/composer.json b/composer.json index 57ddeba..2dbaefa 100644 --- a/composer.json +++ b/composer.json @@ -13,23 +13,19 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": ">=7.3", + "php": ">=8.1", "ext-gmp": "*", "ext-json": "*", - "web-token/jwt-checker": "^2.0", - "web-token/jwt-core": "^2.0", - "web-token/jwt-signature": "^2.0", - "web-token/jwt-signature-algorithm-rsa": "^2.0", + "web-token/jwt-library": "^3.0", "illuminate/contracts": ">=7.1" }, "require-dev": { "illuminate/cache": ">=7.1", "illuminate/config": ">=7.1", "illuminate/container": ">=7.1", - "phpunit/phpunit": "^9.0", - "doctrine/coding-standard": "^6.0 || ^8.0", - "phpstan/phpstan": "^1.0", - "web-token/jwt-easy": "~2.0" + "phpunit/phpunit": "^10.0", + "doctrine/coding-standard": "^12.0", + "phpstan/phpstan": "^1.0" }, "autoload": { "psr-4": { @@ -47,5 +43,10 @@ "CustomerGauge\\Cognito\\LaravelCognitoServiceProvider" ] } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cedb388..fc07472 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,13 @@ - - - - ./src - - + ./tests + + + ./src + + diff --git a/src/CognitoUserProvider.php b/src/CognitoUserProvider.php index cb6a8db..636e09a 100644 --- a/src/CognitoUserProvider.php +++ b/src/CognitoUserProvider.php @@ -1,32 +1,29 @@ -parser = $parser; - $this->factory = $factory; } + /** @inheritdoc */ public function retrieveByCredentials(array $credentials) { $token = $credentials['cognito_token']; try { $payload = $this->parser->parse($token); - - } catch (Exception $e) { + } catch (Throwable) { // If we cannot parse the token, that probably means it's an invalid Token. Since // the Authenticate Middleware implements a Chain Of Responsibility Pattern, // we have to return null so that other Guards can try to authenticate. @@ -36,23 +33,48 @@ public function retrieveByCredentials(array $credentials) return $this->factory->make($payload); } - /** @phpstan ignore */ + /** + * @inheritdoc + * @phpstan ignore + */ public function validateCredentials(Authenticatable $user, array $credentials) { + throw new BadMethodCallException('Not implemented'); } - /** @phpstan ignore */ + /** + * @inheritdoc + * @phpstan ignore + */ public function retrieveById($identifier) { + throw new BadMethodCallException('Not implemented'); } - /** @phpstan ignore */ + /** + * @inheritdoc + * @phpstan ignore + */ public function retrieveByToken($identifier, $token) { + throw new BadMethodCallException('Not implemented'); } - /** @phpstan ignore */ + /** + * @inheritdoc + * @phpstan ignore + */ public function updateRememberToken(Authenticatable $user, $token) { + throw new BadMethodCallException('Not implemented'); + } + + /** + * @inheritdoc + * @phpstan ignore + */ + public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false) + { + throw new BadMethodCallException('Not implemented'); } } diff --git a/src/Contracts/UserFactory.php b/src/Contracts/UserFactory.php index 4785ee0..3c9188d 100644 --- a/src/Contracts/UserFactory.php +++ b/src/Contracts/UserFactory.php @@ -1,4 +1,6 @@ -userPoolId = $userPoolId; - $this->region = $region; } public function toString(): string diff --git a/src/KeyResolver.php b/src/KeyResolver.php index 9c7eceb..43ac0e8 100644 --- a/src/KeyResolver.php +++ b/src/KeyResolver.php @@ -1,27 +1,32 @@ -issuer = $issuer; - $this->cache = $cache; } public function jwkset(): string { $url = $this->issuer->toString() . '/.well-known/jwks.json'; - return $this->cache->remember('jwks', 7200, function() use ($url) { - return file_get_contents($url); + return $this->cache->remember('jwks', 7200, static function () use ($url) { + $content = file_get_contents($url); + + if ($content === false) { + throw new InvalidArgumentException('Invalid JWKS file'); + } + + return $content; }); } diff --git a/src/LaravelCognitoServiceProvider.php b/src/LaravelCognitoServiceProvider.php index e04cb33..c7a15c3 100644 --- a/src/LaravelCognitoServiceProvider.php +++ b/src/LaravelCognitoServiceProvider.php @@ -1,4 +1,6 @@ -registerIssuer(); @@ -30,7 +32,7 @@ private function registerIssuer(): void private function registerCognitoUserProvider(): void { - Auth::provider(CognitoUserProvider::class, function (Container $app) { + Auth::provider(CognitoUserProvider::class, static function (Container $app) { return $app->make(CognitoUserProvider::class); }); } diff --git a/src/Testing/TokenGenerator.php b/src/Testing/TokenGenerator.php index 502d89b..f71d1d1 100644 --- a/src/Testing/TokenGenerator.php +++ b/src/Testing/TokenGenerator.php @@ -1,51 +1,66 @@ -jwk = $jwk; } public static function fromFile(string $path): self { $key = file_get_contents($path); + if ($key === false) { + throw new InvalidArgumentException('Invalid file'); + } + return new self(JWKSet::createFromJson($key)); } + /** @param mixed[] $attributes */ public function sign(array $attributes): string { $time = time(); - $builder = Build::jws() - ->exp($time + 3600) - ->iat($time) - ->nbf($time) - ->jti($this->jti, true) - ->alg($this->algorithm) - ->iss($this->issuer) - ->sub($this->subject); - - foreach ($attributes as $key => $value) { - $builder->claim($key, $value, true); - } + $algorithmManager = new AlgorithmManager([new RS256()]); + $jwsBuilder = new JWSBuilder($algorithmManager); + $payload = JsonConverter::encode([ + 'iat' => $time, + 'nbf' => $time, + 'exp' => $time + 3600, + 'iss' => $this->issuer, + 'jti' => $this->jti, + 'sub' => $this->subject, + ] + $attributes); + + $jws = $jwsBuilder->create() + ->withPayload($payload) + ->addSignature($this->jwk->get(0), ['alg' => $this->algorithm]) + ->build(); - return $builder->sign($this->jwk->get(0)); + return (new CompactSerializer())->serialize($jws); } -} \ No newline at end of file +} diff --git a/src/TokenParser.php b/src/TokenParser.php index 64537bb..a1b4101 100644 --- a/src/TokenParser.php +++ b/src/TokenParser.php @@ -1,4 +1,6 @@ -keyResolver = $keyResolver; } - public function parse(string $token) + public function parse(string $token): mixed { $jws = $this->loadAndVerifyWithKeySet($token); @@ -31,7 +32,7 @@ public function parse(string $token) $claimCheckerManager = new ClaimCheckerManager([ new IssuerChecker([$this->keyResolver->issuer()->toString()]), - new ExpirationTimeChecker, + new ExpirationTimeChecker(), ]); $claimCheckerManager->check($payload); diff --git a/tests/Fixtures/MyUser.php b/tests/Fixtures/MyUser.php index 3fb7dc8..926c427 100644 --- a/tests/Fixtures/MyUser.php +++ b/tests/Fixtures/MyUser.php @@ -29,4 +29,8 @@ public function setRememberToken($value) public function getRememberTokenName() { } + + public function getAuthPasswordName() + { + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 701837d..493f866 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,8 +9,6 @@ use Illuminate\Cache\Repository; use Illuminate\Container\Container; use Illuminate\Contracts\Cache\Repository as RepositoryContract; -use Jose\Component\Core\JWKSet; -use Jose\Easy\Build; use PHPUnit\Framework\TestCase as BaseTestCase; use Tests\CustomerGauge\Cognito\Fixtures\MyUserFactory;