Skip to content

Commit

Permalink
Apply updated code linting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims committed Jun 26, 2023
1 parent 8359936 commit befe076
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 228 deletions.
10 changes: 0 additions & 10 deletions .github/dependabot.yml

This file was deleted.

20 changes: 0 additions & 20 deletions .github/stale.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor
.env
composer.lock
.DS_Store
app/.php-cs-fixer.cache
26 changes: 0 additions & 26 deletions app/phpunit.xml.dist

This file was deleted.

132 changes: 62 additions & 70 deletions app/src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Auth0\Quickstart;

use Auth0\Quickstart\Contract\QuickstartExample;
use Auth0\SDK\Auth0;
use Auth0\SDK\Configuration\SdkConfiguration;
use Auth0\SDK\Exception\InvalidTokenException;
Expand All @@ -16,16 +15,6 @@ final class Application
*/
private SdkConfiguration $configuration;

/**
* An instance of the Auth0 SDK.
*/
private Auth0 $sdk;

/**
* An instance of our application's template rendering helper class, for sending responses.
*/
private ApplicationTemplates $templates;

/**
* An instance of our application's error handling class, for gracefully reporting exceptions.
*/
Expand All @@ -37,24 +26,22 @@ final class Application
private ApplicationRouter $router;

/**
* An instance of a QuickstartExample class, specified from the AUTH0_USE_EXAMPLE env.
* An instance of the Auth0 SDK.
*/
private ?QuickstartExample $example = null;
private Auth0 $sdk;

/**
* An array of hooks with callback functions for examples to override default behavior.
*
* @var array<string, callable>
* An instance of our application's template rendering helper class, for sending responses.
*/
private array $exampleHooks = [];
private ApplicationTemplates $templates;

/**
* Setup our Quickstart application.
*
* @param array<string,mixed> $env Auth0 configuration imported from .env file.
*/
public function __construct(
array $env
array $env,
) {
// Configure the SDK using our .env configuration.
$this->setupAuth0($env);
Expand All @@ -63,41 +50,6 @@ public function __construct(
$this->templates = new ApplicationTemplates($this);
$this->errorHandler = new ApplicationErrorHandler($this);
$this->router = new ApplicationRouter($this);
$this->example = null;
}

/**
* Configure the Auth0 SDK using the .env configuration.
*
* @param array<string,mixed> $env Auth0 configuration imported from .env file.
*/
public function setupAuth0(
array $env
): void {
// Build our SdkConfiguration.
$this->configuration = new SdkConfiguration([
'domain' => $env['AUTH0_DOMAIN'] ?? null,
'clientId' => $env['AUTH0_CLIENT_ID'] ?? null,
'clientSecret' => $env['AUTH0_CLIENT_SECRET'] ?? null,
'audience' => ($env['AUTH0_AUDIENCE'] ?? null) !== null ? [trim($env['AUTH0_AUDIENCE'])] : null,
'organization' => ($env['AUTH0_ORGANIZATION'] ?? null) !== null ? [trim($env['AUTH0_ORGANIZATION'])] : null,
'strategy' => SdkConfiguration::STRATEGY_API,
]);

// Setup the Auth0 SDK.
$this->sdk = new Auth0($this->configuration);
}

/**
* "Run" our application, responding to end-user requests.
*/
public function run(): void
{
// Intercept exceptions to gracefully report them.
$this->errorHandler->hook();

// Handle incoming requests through the router.
$this->router->run();
}

/**
Expand Down Expand Up @@ -141,40 +93,80 @@ public function &getRouter(): ApplicationRouter
}

/**
* Called from the ApplicationRouter when end user loads '/'.
* Called from the ApplicationRouter when end user loads '/api'.
*
* @param ApplicationRouter $router
*/
public function onIndexRoute(
ApplicationRouter $router
public function onApiRoute(
ApplicationRouter $router,
): void {
$session = $this->getToken();

// Send response to browser.
$this->templates->render('spa', [
'config' => $this->getConfiguration(),
$this->templates->render('logged-' . ($session instanceof \Auth0\SDK\Contract\TokenInterface ? 'in' : 'out'), [
'session' => $session,
'router' => $router,
]);
}

/**
* Called from the ApplicationRouter when end user loads '/api'.
* Called from the ApplicationRouter when end user loads an unknown route.
*
* @param ApplicationRouter $router
*/
public function onApiRoute(
ApplicationRouter $router
public function onError404(
ApplicationRouter $router,
): void {
$session = $this->getToken();
$router->setHttpStatus(404);
}

/**
* Called from the ApplicationRouter when end user loads '/'.
*
* @param ApplicationRouter $router
*/
public function onIndexRoute(
ApplicationRouter $router,
): void {
// Send response to browser.
$this->templates->render('logged-' . ($session === null ? 'out' : 'in'), [
'session' => $session,
$this->templates->render('spa', [
'config' => $this->getConfiguration(),
'router' => $router,
]);
}

/**
* Called from the ApplicationRouter when end user loads an unknown route.
* "Run" our application, responding to end-user requests.
*/
public function onError404(
ApplicationRouter $router
public function run(): void
{
// Intercept exceptions to gracefully report them.
$this->errorHandler->hook();

// Handle incoming requests through the router.
$this->router->run();
}

/**
* Configure the Auth0 SDK using the .env configuration.
*
* @param array<string,mixed> $env Auth0 configuration imported from .env file.
*/
public function setupAuth0(
array $env,
): void {
$router->setHttpStatus(404);
// Build our SdkConfiguration.
$this->configuration = new SdkConfiguration([
'domain' => $env['AUTH0_DOMAIN'] ?? null,
'clientId' => $env['AUTH0_CLIENT_ID'] ?? null,
'clientSecret' => $env['AUTH0_CLIENT_SECRET'] ?? null,
'audience' => ($env['AUTH0_AUDIENCE'] ?? null) !== null ? [trim($env['AUTH0_AUDIENCE'])] : null,
'organization' => ($env['AUTH0_ORGANIZATION'] ?? null) !== null ? [trim($env['AUTH0_ORGANIZATION'])] : null,
'strategy' => SdkConfiguration::STRATEGY_API,
]);

// Setup the Auth0 SDK.
$this->sdk = new Auth0($this->configuration);
}

/**
Expand All @@ -188,15 +180,15 @@ private function getToken(): ?\Auth0\SDK\Contract\TokenInterface
$token = $_GET['token'] ?? $_SERVER['HTTP_AUTHORIZATION'] ?? $_SERVER['Authorization'] ?? null;

// If no token was present, abort processing.
if ($token === null) {
if (null === $token) {
return null;
}

// Trim whitespace from token string.
$token = trim($token);

// Remove the 'Bearer ' prefix, if present, in the event we're using an Authorization header that's using it.
if (substr($token, 0, 7) === 'Bearer ') {
if (str_starts_with($token, 'Bearer ')) {
$token = substr($token, 7);
}

Expand Down
12 changes: 8 additions & 4 deletions app/src/ApplicationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Auth0\SDK\Exception\Auth0Exception;
use Throwable;

use function array_key_exists;

final class ApplicationErrorHandler
{
/**
Expand All @@ -20,17 +22,19 @@ final class ApplicationErrorHandler
* @param Application $app An instance of our Quickstart Application.
*/
public function __construct(
Application &$app
Application &$app,
) {
$this->app = & $app;
$this->app = &$app;
}

/**
* Setup onException() as the exception handler with PHP for this request.
*/
public function hook(): void
{
set_exception_handler([$this, 'onException']);
set_exception_handler(function (Throwable $throwable): void {
$this->onException($throwable);
});
}

/**
Expand All @@ -39,7 +43,7 @@ public function hook(): void
* @param Throwable $throwable The throwable to report.
*/
public function onException(
\Throwable $throwable
Throwable $throwable,
): void {
$exception = $throwable;

Expand Down
Loading

0 comments on commit befe076

Please sign in to comment.