From 36df529c43d1610222a5534d8f5e260510e9c1bf Mon Sep 17 00:00:00 2001 From: Shingo Kumagai Date: Sun, 17 Nov 2024 18:34:19 +0900 Subject: [PATCH] =?UTF-8?q?Token=E3=82=AF=E3=83=A9=E3=82=B9=E3=82=92?= =?UTF-8?q?=E5=88=A9=E7=94=A8=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Auth/Auth.php | 21 +++++++-------- src/Auth/AuthInterface.php | 4 +-- src/Provider/ManagementClientProvider.php | 9 ++----- tests/Auth/AuthTest.php | 33 +++++++++++++++++++++++ tests/Auth0ModuleTest.php | 13 +++------ 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/Auth/Auth.php b/src/Auth/Auth.php index d0efbc3..deac17c 100644 --- a/src/Auth/Auth.php +++ b/src/Auth/Auth.php @@ -5,8 +5,9 @@ namespace Ray\Auth0Module\Auth; use Auth0\SDK\Configuration\SdkConfiguration; +use Auth0\SDK\Contract\TokenInterface; use Auth0\SDK\Exception\InvalidTokenException; -use Auth0\SDK\Token\Parser; +use Auth0\SDK\Token; use Ray\Auth0Module\Annotation\Auth0Config; use Ray\Auth0Module\Exception\InvalidToken; @@ -19,22 +20,18 @@ class Auth implements AuthInterface #[Auth0Config('config')] public function __construct(array $config) { - $this->configuration = new SdkConfiguration([ - 'domain' => $config['domain'], - 'clientId' => $config['clientId'], - 'clientSecret' => $config['clientSecret'] ?? null, - 'cookieSecret' => $config['cookieSecret'] ?? null, - ]); + $this->configuration = new SdkConfiguration($config); } - public function verifyToken(string $token): Parser + public function verifyToken(string $token) : TokenInterface { try { - $parser = new parser($this->configuration, $token); - $parser->parse(); - $parser->verify(jwksUri: 'https://' . $this->configuration->getDomain() . '/.well-known/jwks.json'); + $token = new Token($this->configuration, $token); + $token + ->verify() + ->validate(); - return $parser; + return $token; } catch (InvalidTokenException $e) { throw new InvalidToken($e->getMessage()); } diff --git a/src/Auth/AuthInterface.php b/src/Auth/AuthInterface.php index 29126c1..811c2f9 100644 --- a/src/Auth/AuthInterface.php +++ b/src/Auth/AuthInterface.php @@ -4,9 +4,9 @@ namespace Ray\Auth0Module\Auth; -use Auth0\SDK\Token\Parser; +use Auth0\SDK\Contract\TokenInterface; interface AuthInterface { - public function verifyToken(string $token): Parser; + public function verifyToken(string $token) : TokenInterface; } diff --git a/src/Provider/ManagementClientProvider.php b/src/Provider/ManagementClientProvider.php index e695553..184420b 100644 --- a/src/Provider/ManagementClientProvider.php +++ b/src/Provider/ManagementClientProvider.php @@ -24,15 +24,10 @@ class ManagementClientProvider implements ProviderInterface #[Auth0Config('config')] public function __construct(private $config) { - $this->configuration = new SdkConfiguration([ - 'domain' => $config['domain'], - 'clientId' => $config['clientId'], - 'clientSecret' => $config['clientSecret'] ?? null, - 'cookieSecret' => $config['cookieSecret'] ?? null, - ]); + $this->configuration = new SdkConfiguration($config); } - public function get(): Management + public function get() : Management { return new Management($this->configuration); } diff --git a/tests/Auth/AuthTest.php b/tests/Auth/AuthTest.php index e69de29..e8d9ed7 100644 --- a/tests/Auth/AuthTest.php +++ b/tests/Auth/AuthTest.php @@ -0,0 +1,33 @@ + 'test.auth0.com', + 'clientId' => 'test-client-id', + 'clientSecret' => 'test-client-secret', + 'cookieSecret' => 'test-cookie-secret', + 'audience' => ['test-audience'] + ]; + + public function testConstructorWithValidConfig(): void + { + $auth = new Auth($this->validConfig); + $this->assertInstanceOf(Auth::class, $auth); + } + + public function testVerifyTokenWithInvalidToken(): void + { + $auth = new Auth($this->validConfig); + $invalidTokenString = 'invalid.token'; + + $this->expectException(InvalidToken::class); + $auth->verifyToken($invalidTokenString); + } +} diff --git a/tests/Auth0ModuleTest.php b/tests/Auth0ModuleTest.php index f1eb8f8..21db62d 100644 --- a/tests/Auth0ModuleTest.php +++ b/tests/Auth0ModuleTest.php @@ -1,27 +1,20 @@ module = new class extends AbstractModule {