Skip to content

Commit 97f6d6e

Browse files
committed
Merge branch 'feature/auth-refactoring' into 7.x
2 parents e1437cf + ac27a15 commit 97f6d6e

File tree

77 files changed

+1322
-146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1322
-146
lines changed

.travis.yml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
language: php
22

3+
dist: xenial
4+
35
php:
46
- 7.2
57
- 7.3
6-
8+
- '7.4snapshot'
9+
710
sudo: false
811

12+
services:
13+
- postgresql
14+
- mysql
15+
16+
cache:
17+
directories:
18+
- vendor
19+
- $HOME/.composer/cache
20+
921
env:
1022
matrix:
11-
- DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
23+
- DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test?init[]=SET sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'
1224
- DB=pgsql db_dsn='postgres://[email protected]/cakephp_test'
1325
- DB=sqlite db_dsn='sqlite:///:memory:'
1426

@@ -31,8 +43,8 @@ matrix:
3143
before_script:
3244
- composer self-update
3345
- composer install --prefer-dist --no-interaction
34-
- if [ $DB = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
35-
- if [ $DB = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi
46+
- if [[ $DB == 'mysql' ]]; then mysql -u root -e 'CREATE DATABASE cakephp_test;'; fi
47+
- if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi
3648
- if [[ $PHPSTAN = 1 ]]; then composer stan-setup; fi
3749

3850
script:

composer.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@
6060
"check": [
6161
"@cs-check",
6262
"@test",
63-
"@stan"
63+
"@analyse"
6464
],
65-
"cs-check": "phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
65+
"analyse": [
66+
"@stan",
67+
"@psalm"
68+
],
69+
"cs-check": "phpcs -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
6670
"cs-fix": "phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/",
6771
"test": "phpunit --stderr",
68-
"stan": "phpstan analyse src/ && psalm --show-info=false",
69-
"psalm": "psalm --show-info=false",
70-
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json",
72+
"stan": "phpstan analyse src/",
73+
"psalm": "php vendor/psalm/phar/psalm.phar --show-info=false src/ ",
74+
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan-shim:^0.11.18 psalm/phar:^3.5 && mv composer.backup composer.json",
7175
"rector": "rector process src/",
7276
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json",
7377
"coverage-test": "phpunit --stderr --coverage-clover=clover.xml"

config/api.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
<?php
22
/**
3-
* Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
3+
* Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
44
*
55
* Licensed under The MIT License
66
* Redistributions of files must retain the above copyright notice.
77
*
8-
* @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
8+
* @copyright Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
99
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
1010
*/
11+
use Authentication\AuthenticationService;
12+
use Authentication\Middleware\AuthenticationMiddleware;
13+
use Authorization\Middleware\AuthorizationMiddleware;
14+
use Authorization\Middleware\RequestAuthorizationMiddleware;
15+
use CakeDC\Api\Middleware\ParseApiRequestMiddleware;
16+
use CakeDC\Api\Middleware\ProcessApiRequestMiddleware;
17+
use CakeDC\Api\ApiInitializer;
1118

1219
return [
1320
'Api' => [
@@ -31,6 +38,30 @@
3138
'default' => 'auth'
3239
],
3340
],
41+
42+
'Middleware' => [
43+
'authentication' => [
44+
'class' => AuthenticationMiddleware::class,
45+
'request' => ApiInitializer::class,
46+
'method' => 'getAuthenticationService',
47+
],
48+
'apiParser' => [
49+
'class' => ParseApiRequestMiddleware::class,
50+
],
51+
'apiAuthorize' => [
52+
'class' => AuthorizationMiddleware::class,
53+
'request' => ApiInitializer::class,
54+
'params' => [
55+
'unauthorizedHandler' => 'CakeDC/Api.ApiException',
56+
],
57+
],
58+
'apiAuthorizeRequest' => [
59+
'class' => RequestAuthorizationMiddleware::class,
60+
],
61+
'apiProcessor' => [
62+
'class' => ProcessApiRequestMiddleware::class,
63+
],
64+
],
3465

3566
'Service' => [
3667
'default' => [

config/api_permissions.php.default

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'CakeDC/Auth.api_permissions' => [
5+
[
6+
'role' => '*',
7+
'service' => '*',
8+
'action' => '*',
9+
'method' => '*',
10+
'bypassAuth' => true,
11+
],
12+
]
13+
];

config/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
/**
3-
* Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
3+
* Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
44
*
55
* Licensed under The MIT License
66
* Redistributions of files must retain the above copyright notice.
77
*
8-
* @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
8+
* @copyright Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
99
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
1010
*/
1111

config/routes.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<?php
22
/**
3-
* Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
3+
* Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
44
*
55
* Licensed under The MIT License
66
* Redistributions of files must retain the above copyright notice.
77
*
8-
* @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
8+
* @copyright Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
99
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
1010
*/
1111

1212
use Cake\Core\Configure;
1313
use Cake\Routing\Router;
14+
use Cake\Routing\RouteBuilder;
1415

1516
Router::plugin('CakeDC/Api', ['path' => '/api'], function ($routes) {
1617
$useVersioning = Configure::read('Api.useVersioning');
1718
$versionPrefix = Configure::read('Api.versionPrefix');
19+
$middlewares = Configure::read('Api.Middleware');
20+
$middlewareNames = array_keys($middlewares);
21+
22+
$routes->applyMiddleware(...$middlewareNames);
23+
1824
if (empty($versionPrefix)) {
1925
$versionPrefix = 'v';
2026
}
@@ -29,11 +35,6 @@
2935
'controller' => 'Api',
3036
'action' => 'listing'
3137
], ['version' => $versionPrefix . '\d+', 'pass' => []]);
32-
$routes->connect('/:version/:service/*', [
33-
'plugin' => 'CakeDC/Api',
34-
'controller' => 'Api',
35-
'action' => 'process'
36-
], ['version' => $versionPrefix . '\d+', 'pass' => []]);
3738
}
3839
$routes->connect('/describe/*', [
3940
'plugin' => 'CakeDC/Api',
@@ -45,9 +46,5 @@
4546
'controller' => 'Api',
4647
'action' => 'listing'
4748
]);
48-
$routes->connect('/:service/*', [
49-
'plugin' => 'CakeDC/Api',
50-
'controller' => 'Api',
51-
'action' => 'process'
52-
]);
49+
$routes->connect('/**', ['plugin' => 'CakeDC/Api', 'controller' => 'Api', 'action' => 'notFound']);
5350
});

src/ApiInitializer.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
11+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12+
*/
13+
14+
namespace CakeDC\Api;
15+
16+
use Authentication\AuthenticationService;
17+
use Authorization\AuthorizationService;
18+
use Authorization\AuthorizationServiceInterface;
19+
use Authorization\AuthorizationServiceProviderInterface;
20+
use Authorization\Policy\MapResolver;
21+
use Authorization\Policy\OrmResolver;
22+
use Authorization\Policy\ResolverCollection;
23+
use Cake\Core\Configure;
24+
use Cake\Http\ServerRequest;
25+
use CakeDC\Api\Rbac\ApiRbac;
26+
use CakeDC\Auth\Rbac\Rbac;
27+
use CakeDC\Auth\Policy\CollectionPolicy;
28+
use CakeDC\Auth\Policy\RbacPolicy;
29+
use CakeDC\Auth\Policy\SuperuserPolicy;
30+
use Psr\Http\Message\ResponseInterface;
31+
use Psr\Http\Message\ServerRequestInterface;
32+
33+
class ApiInitializer implements AuthorizationServiceProviderInterface
34+
{
35+
36+
public function getAuthenticationService(): AuthenticationService
37+
{
38+
$service = new AuthenticationService();
39+
$service->loadIdentifier('Authentication.JwtSubject', []);
40+
41+
$service->loadIdentifier('Authentication.Password', []);
42+
$service->loadAuthenticator('Authentication.Session', [
43+
'sessionKey' => 'Auth',
44+
]);
45+
46+
$service->loadIdentifier('Authentication.Token', [
47+
'dataField' => 'token',
48+
'tokenField' => 'api_token',
49+
]);
50+
$service->loadAuthenticator('Authentication.Token', [
51+
'queryParam' => 'token',
52+
]);
53+
54+
return $service;
55+
}
56+
57+
public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface
58+
{
59+
$map = new MapResolver();
60+
$rbac = new ApiRbac();
61+
$map->map(
62+
ServerRequest::class,
63+
new CollectionPolicy([
64+
//SuperuserPolicy::class,
65+
new RbacPolicy([
66+
'adapter' => $rbac
67+
])
68+
])
69+
);
70+
71+
$orm = new OrmResolver();
72+
73+
$resolver = new ResolverCollection([
74+
$map,
75+
$orm
76+
]);
77+
78+
return new AuthorizationService($resolver);
79+
}
80+
81+
}

src/Controller/ApiController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,9 @@ protected function _process(array $options = []): ?Response
123123

124124
return $this->getResponse();
125125
}
126+
127+
public function notFound()
128+
{
129+
return $this->getResponse()->withStatus(404);
130+
}
126131
}

src/Controller/AppController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
namespace CakeDC\Api\Controller;
1515

16-
//use App\Controller\AppController as BaseController;
16+
use App\Controller\AppController as BaseController;
1717

18-
19-
use Cake\Controller\Controller as BaseController;
18+
// use Cake\Controller\Controller as BaseController;
2019

2120
/**
2221
* Class AppController

src/Middleware/ApiMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/**
2828
* Applies routing rules to the request and creates the controller
2929
* instance if possible.
30+
* @deprecated use ParseApiRequestMiddleware and ProcessApiRequestMiddleware instead
3031
*/
3132
class ApiMiddleware implements MiddlewareInterface
3233
{

0 commit comments

Comments
 (0)