Skip to content

Commit

Permalink
Tokenクラスを利用するように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
kuma-guy committed Nov 17, 2024
1 parent 5f037cc commit 36df529
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
21 changes: 9 additions & 12 deletions src/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Auth/AuthInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 2 additions & 7 deletions src/Provider/ManagementClientProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Auth/AuthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

namespace Ray\Auth0Module\Auth;

use PHPUnit\Framework\TestCase;
use Ray\Auth0Module\Exception\InvalidToken;

class AuthTest extends TestCase
{
private array $validConfig = [
'domain' => '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);
}
}
13 changes: 3 additions & 10 deletions tests/Auth0ModuleTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
<?php

declare(strict_types=1);

namespace Ray\Auth0Module;

use Auth0\SDK\API\Authentication;
use Auth0\SDK\API\Management;
use josegonzalez\Dotenv\Loader;
use PHPUnit\Framework\TestCase;
use Ray\AuraWebModule\AuraWebModule;
use Ray\Auth0Module\Annotation\Auth0Config;
use Ray\Auth0Module\Annotation\Extractors;
use Ray\Auth0Module\Auth\Auth;
use Ray\Auth0Module\Auth\AuthInterface;
use Ray\Auth0Module\Extractor\AuthorizationHeaderTokenExtractor;
use Ray\Auth0Module\Extractor\TokenExtractorResolver;
use Ray\Auth0Module\Provider\AuthenticationClientProvider;
use Ray\Auth0Module\Provider\FakeManagementClientProvider;
use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\Di\Scope;

class Auth0ModuleTest extends TestCase
{
private AbstractModule $module;

public function testModule() : void
{
$this->module = new class extends AbstractModule {
Expand Down

0 comments on commit 36df529

Please sign in to comment.