Skip to content

Commit

Permalink
Merge pull request #7 from yuki777/auth0-php-v8
Browse files Browse the repository at this point in the history
Migrate auth0-php v8
  • Loading branch information
kuma-guy authored Nov 6, 2024
2 parents abc0a83 + bd8eca9 commit 7c6b07f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
32 changes: 17 additions & 15 deletions src/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@

namespace Ray\Auth0Module\Auth;

use Auth0\SDK\Configuration\SdkConfiguration;
use Auth0\SDK\Exception\InvalidTokenException;
use Auth0\SDK\Helpers\JWKFetcher;
use Auth0\SDK\Helpers\Tokens\AsymmetricVerifier;
use Lcobucci\JWT\Token;
use Auth0\SDK\Token\Parser;
use Ray\Auth0Module\Annotation\Auth0Config;
use Ray\Auth0Module\Exception\InvalidToken;

class Auth implements AuthInterface
{
/**
* @var AsymmetricVerifier
*/
private $verifier;
/** @var SdkConfiguration */
private $configuration;

/**
* @Auth0Config("config")
*/
/** @Auth0Config("config") */
#[Auth0Config('config')]
public function __construct(array $config)
{
$jwksFetcher = new JWKFetcher();
$jwks = $jwksFetcher->getKeys('https://' . $config['domain'] . '/.well-known/jwks.json');
$this->verifier = new AsymmetricVerifier($jwks);
$this->configuration = new SdkConfiguration([
'domain' => $config['domain'],
'clientId' => $config['clientId'],
'clientSecret' => $config['clientSecret'] ?? null,
'cookieSecret' => $config['cookieSecret'] ?? null,
]);
}

public function verifyToken(string $token) : Token
public function verifyToken(string $token): Parser
{
try {
return $this->verifier->verifyAndDecode($token);
$parser = new parser($this->configuration, $token);
$parser->parse();
$parser->verify(jwksUri: 'https://' . $this->configuration->getDomain() . '/.well-known/jwks.json');

return $parser;
} 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 Lcobucci\JWT\Token;
use Auth0\SDK\Token\Parser;

interface AuthInterface
{
public function verifyToken(string $token) : Token;
public function verifyToken(string $token): Parser;
}
26 changes: 14 additions & 12 deletions src/Provider/ManagementClientProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,35 @@
namespace Ray\Auth0Module\Provider;

use Auth0\SDK\API\Management;
use Auth0\SDK\Configuration\SdkConfiguration;
use Ray\Auth0Module\Annotation\Auth0Config;
use Ray\Di\ProviderInterface;

class ManagementClientProvider implements ProviderInterface
{
use AuthenticationClientInject;

/** @var array */
private $config;
/** @var SdkConfiguration */
private $configuration;

/**
* @Auth0Config("config")
*
* @param array $config
*
* @Auth0Config("config")
*/
#[Auth0Config('config')]
public function __construct($config)
public function __construct(private $config)
{
$this->config = $config;
$this->configuration = new SdkConfiguration([
'domain' => $config['domain'],
'clientId' => $config['clientId'],
'clientSecret' => $config['clientSecret'] ?? null,
'cookieSecret' => $config['cookieSecret'] ?? null,
]);
}

public function get() : Management
public function get(): Management
{
$response = $this->authClient->client_credentials([
'audience' => 'https://' . $this->config['domain'] . '/api/v2/',
]);

return new Management($response['access_token'], $this->config['domain']);
return new Management($this->configuration);
}
}

0 comments on commit 7c6b07f

Please sign in to comment.