Skip to content

Commit

Permalink
fixes for phpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Jan 26, 2024
1 parent 6b5fd1f commit 26103da
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 116 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
}
},
"scripts": {
"cs-check": "phpcs --colors -p src/ tests/",
"cs-fix": "phpcbf --colors -p src/ tests/"
"test": "phpunit",
"cs-check": "phpcs --colors -p ./src ./tests",
"cs-fix": "phpcbf --colors -p ./src ./tests"
}
}
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="CakePHP Sentry">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />
</ruleset>
68 changes: 42 additions & 26 deletions src/ApiTokenAuthenticatorPlugin.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
<?php
declare(strict_types=1);

namespace ApiTokenAuthenticator;

use Cake\Core\Configure;
use Cake\Routing\Router;
use Cake\Core\BasePlugin;
use Cake\Http\MiddlewareQueue;
use Cake\Console\CommandCollection;
use ApiTokenAuthenticator\Authentication\Authenticator\ProvisoryTokenAuthenticator;
use Authentication\AuthenticationService;
use Cake\Core\PluginApplicationInterface;
use Psr\Http\Message\ServerRequestInterface;
use Authentication\Identifier\AbstractIdentifier;
use Authentication\AuthenticationServiceInterface;
use Authentication\Middleware\AuthenticationMiddleware;
use Authentication\AuthenticationServiceProviderInterface;
use ApiTokenAuthenticator\Authentication\Authenticator\ProvisoryTokenAuthenticator;
use Authentication\Identifier\AbstractIdentifier;
use Authentication\Middleware\AuthenticationMiddleware;
use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use Cake\Core\Configure;
use Cake\Core\PluginApplicationInterface;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Router;
use Psr\Http\Message\ServerRequestInterface;

class ApiTokenAuthenticatorPlugin extends BasePlugin implements AuthenticationServiceProviderInterface
{
// TODO disable CSRF middleware - now it is manually in Application.php

/**
* @inheritDoc
*/
public function middleware(MiddlewareQueue $middleware): MiddlewareQueue
{
// Add middleware here.
Expand All @@ -32,6 +37,9 @@ public function middleware(MiddlewareQueue $middleware): MiddlewareQueue
return $middleware;
}

/**
* @inheritDoc
*/
public function console(CommandCollection $commands): CommandCollection
{
// Add console commands here.
Expand All @@ -40,13 +48,19 @@ public function console(CommandCollection $commands): CommandCollection
return $commands;
}

/**
* @inheritDoc
*/
public function bootstrap(PluginApplicationInterface $app): void
{
// Add constants, load configuration defaults.
// By default will load `config/bootstrap.php` in the plugin.
parent::bootstrap($app);
}

/**
* @inheritDoc
*/
public function routes($routes): void
{
// Add routes.
Expand All @@ -57,7 +71,7 @@ public function routes($routes): void
/**
* Returns a service provider instance.
*
* @param \Psr\Http\Message\ServerRequestInterface $request Request
* @param \Psr\Http\Message\ServerRequestInterface $request Request
* @return \Authentication\AuthenticationServiceInterface
*/
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
Expand All @@ -68,28 +82,30 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe

$fields = [
AbstractIdentifier::CREDENTIAL_USERNAME => $options['fields']['username'],
AbstractIdentifier::CREDENTIAL_PASSWORD => $options['fields']['password']
AbstractIdentifier::CREDENTIAL_PASSWORD => $options['fields']['password'],
];

$service->loadAuthenticator(
ProvisoryTokenAuthenticator::class, [
'header' => $options['header'],
ProvisoryTokenAuthenticator::class,
[
'header' => $options['header'],
]
);
$service->loadIdentifier('Authentication.Token');

$service->loadAuthenticator(
'Authentication.Form', [
'fields' => $fields,
'loginUrl' => Router::url(
[
'prefix' => false,
'plugin' => null,
'controller' => $options['login']['controller'],
'action' => $options['login']['action'],
'_ext' => $options['login']['_ext']
]
),
'Authentication.Form',
[
'fields' => $fields,
'loginUrl' => Router::url(
[
'prefix' => false,
'plugin' => null,
'controller' => $options['login']['controller'],
'action' => $options['login']['action'],
'_ext' => $options['login']['_ext'],
]
),
]
);

Expand All @@ -101,7 +117,7 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
'Authentication.Password',
[
'fields' => $fields,
'passwordHasher' => $options['passwordHasher']
'passwordHasher' => $options['passwordHasher'],
]
);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Authentication/Authenticator/ProvisoryTokenAuthenticator.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<?php
declare(strict_types=1);

namespace ApiTokenAuthenticator\Authentication\Authenticator;

use Cake\I18n\DateTime;
use Cake\Core\Configure;
use Authentication\Authenticator\Result;
use Psr\Http\Message\ServerRequestInterface;
use Authentication\Authenticator\ResultInterface;
use Authentication\Authenticator\TokenAuthenticator;
use Cake\Core\Configure;
use Cake\I18n\DateTime;
use Psr\Http\Message\ServerRequestInterface;

class ProvisoryTokenAuthenticator extends TokenAuthenticator
{
/**
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @return \Authentication\Authenticator\ResultInterface
*/
public function authenticate(ServerRequestInterface $request): ResultInterface
{
$result = parent::authenticate($request);
Expand Down
9 changes: 8 additions & 1 deletion src/Controller/AppController.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
<?php
declare(strict_types=1);

namespace ApiTokenAuthenticator\Controller;

use Cake\View\JsonView;
use App\Controller\AppController as BaseController;
use Cake\View\JsonView;

class AppController extends BaseController
{
/**
* @inheritDoc
*/
public function initialize(): void
{
parent::initialize();
// TODO do we need this here or just in the application?
$this->loadComponent('Authentication.Authentication');
}

/**
* @inheritDoc
*/
public function viewClasses(): array
{
// TODO do we need this here or just in the application?
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class UsersFixture extends TestFixture
[
'id' => 1,
'email' => 'rrd',
'password' => '$2y$10$ZVRmoo2amhyBLFqz/Qoo8OsqbGDXs95AdQM/gmLSJ2BghRSWdmRDS', // 123
'password' => '$2y$10$ZVRmoo2amhyBLFqz/Qoo8OsqbGDXs95AdQM/gmLSJ2BghRSWdmRDS', // 123
'token' => 'token-1',
'created' => '2023-10-21 12:12:12',
'modified' => '2023-10-21 12:12:12',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php
declare(strict_types=1);

namespace ApiTokenAuthenticator\Test\Authentication\Authenticator;

use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use Cake\Http\ServerRequestFactory;
use ApiTokenAuthenticator\Authentication\Authenticator\ProvisoryTokenAuthenticator;
use Authentication\Authenticator\Result;
use Authentication\Identifier\IdentifierCollection;
use ApiTokenAuthenticator\Authentication\Authenticator\ProvisoryTokenAuthenticator;
use Cake\Core\Configure;
use Cake\Http\ServerRequestFactory;
use Cake\TestSuite\TestCase;

class ProvisoryTokenAuthenticatorTest extends TestCase
{
Expand Down
10 changes: 4 additions & 6 deletions tests/TestCase/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

declare(strict_types=1);

namespace TestApp\Test\TestCase\Controller;
Expand All @@ -20,7 +19,7 @@ public function testLogin(): void
$this->post('/users/login.json', ['email' => 'rrd', 'password' => '123']);
$this->assertResponseOk();
$this->assertHeader('Content-Type', 'application/json');
$user = $this->viewVariable('user');
$user = $this->viewVariable('user');
$this->assertEquals('token-1', $user->token);
}

Expand All @@ -31,22 +30,21 @@ public function testIndex(): void

$this->configRequest(
[
'headers' => ['Token' => 'FalseToken']
'headers' => ['Token' => 'FalseToken'],
]
);
$this->get('/users.json');
$this->assertResponseCode(401);


$this->configRequest(
[
'headers' => ['Token' => 'token-1']
'headers' => ['Token' => 'token-1'],
]
);
$this->get('/users.json');
$this->assertResponseOk();
$this->assertHeader('Content-Type', 'application/json');
$users = $this->viewVariable('users');
$users = $this->viewVariable('users');
$this->assertEquals('rrd', $users->toArray()[0]->email);
}
}
62 changes: 1 addition & 61 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,7 @@
<?php

declare(strict_types=1);

use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Routing\Router;
use Cake\Utility\Security;

$findRoot = function ($root) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
} while ($root !== $lastRoot);
throw new Exception('Cannot find the root of the application, unable to run tests');
};
$root = $findRoot(__FILE__);
unset($findRoot);
chdir($root);

//require_once 'vendor/cakephp/cakephp/src/functions.php';
//require_once 'vendor/autoload.php';

define('ROOT', $root . DS . 'tests' . DS . 'test_app' . DS);
// define('APP', ROOT . 'App' . DS);
// define('TMP', sys_get_temp_dir() . DS);
define('CONFIG', ROOT . DS . 'config' . DS);

Configure::write('debug', true);
Configure::write(
'App', [
'namespace' => 'TestApp',
'encoding' => 'UTF-8',
'paths' => [
'plugins' => [ROOT . 'Plugin' . DS],
'templates' => [ROOT . 'templates' . DS],
],
]
);

ConnectionManager::setConfig(
'test', [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlite',
'database' => ':memory:',
'encoding' => 'utf8',
'timezone' => 'UTC',
'quoteIdentifiers' => false,
]
);

Router::reload();
//Security::setSalt('rrd');

Plugin::getCollection()->add(new \Authentication\Plugin());

$_SERVER['PHP_SELF'] = '/';

Configure::load('ApiTokenAuthenticator.apiTokenAuthenticator');

use Cake\TestSuite\Fixture\SchemaLoader;
// Load a schema dump file.

(new SchemaLoader())->loadSqlFiles('tests/schema.sql', 'test');
2 changes: 1 addition & 1 deletion tests/test_app/config/plugins.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
return [
'ApiTokenAuthenticator' => []
'ApiTokenAuthenticator' => [],
];
15 changes: 7 additions & 8 deletions tests/test_app/src/Application.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<?php

declare(strict_types=1);

namespace TestApp;

use Cake\Http\BaseApplication;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\RouteBuilder;
use Cake\Core\ContainerInterface;
use Cake\Routing\Route\DashedRoute;
use Cake\Http\BaseApplication;
use Cake\Http\Middleware\BodyParserMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;

class Application extends BaseApplication
{

public function routes(RouteBuilder $routes): void
{
$routes->setRouteClass(DashedRoute::class);
$routes->scope(
'/', function (RouteBuilder $builder): void {
'/',
function (RouteBuilder $builder): void {
$builder->setExtensions(['json']);
$builder->resources('Users');
$builder->fallbacks();
Expand All @@ -30,7 +29,7 @@ public function routes(RouteBuilder $routes): void
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
return $middlewareQueue->add(new RoutingMiddleware($this))
->add(new BodyParserMiddleware());;
->add(new BodyParserMiddleware());
}

public function services(ContainerInterface $container): void
Expand Down
1 change: 0 additions & 1 deletion tests/test_app/src/Controller/AppController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

declare(strict_types=1);

namespace TestApp\Controller;
Expand Down
Loading

0 comments on commit 26103da

Please sign in to comment.