Skip to content

Commit 0408a95

Browse files
committed
Merge branch 'master' of github.com:mevdschee/php-crud-api
2 parents d88323e + 43f4000 commit 0408a95

File tree

12 files changed

+514
-63
lines changed

12 files changed

+514
-63
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ You can tune the middleware behavior using middleware specific configuration par
616616
- "jwtAuth.header": Name of the header containing the JWT token ("X-Authorization")
617617
- "jwtAuth.leeway": The acceptable number of seconds of clock skew ("5")
618618
- "jwtAuth.ttl": The number of seconds the token is valid ("30")
619-
- "jwtAuth.secret": The shared secret used to sign the JWT token with ("")
619+
- "jwtAuth.secrets": The shared secret(s) used to sign the JWT token with ("")
620620
- "jwtAuth.algorithms": The algorithms that are allowed, empty means 'all' ("")
621621
- "jwtAuth.audiences": The audiences that are allowed, empty means 'all' ("")
622622
- "jwtAuth.issuers": The issuers that are allowed, empty means 'all' ("")
@@ -731,7 +731,7 @@ Once logged in, you have to create an application (its type does not matter). Co
731731
and `Client ID` and keep them for a later use. Then, create an API: give it a name and fill the
732732
`identifier` field with your API endpoint's URL.
733733

734-
Then you have to configure the `jwtAuth.secret` configuration in your `api.php` file.
734+
Then you have to configure the `jwtAuth.secrets` configuration in your `api.php` file.
735735
Don't fill it with the `secret` you will find in your Auth0 application settings but with **a
736736
public certificate**. To find it, go to the settings of your application, then in "Extra settings".
737737
You will now find a "Certificates" tab where you will find your Public Key in the Signing
@@ -755,11 +755,11 @@ You can also change the `url` variable, used to test the API with authentication
755755
First you need to create a Firebase project on the [Firebase console](https://console.firebase.google.com/).
756756
Add a web application to this project and grab the code snippet for later use.
757757

758-
Then you have to configure the `jwtAuth.secret` configuration in your `api.php` file.
758+
Then you have to configure the `jwtAuth.secrets` configuration in your `api.php` file.
759759
Grab the public key via this [URL](https://www.googleapis.com/robot/v1/metadata/x509/[email protected]).
760760
There may be several certificates, just grab the one corresponding to your `kid` (if you don't
761761
know what it is, just test them all until you will be logged in).
762-
Now, just fill `jwtAuth.secret` with your public key.F
762+
Now, just fill `jwtAuth.secrets` with your public key.
763763

764764
To test your integration, you can copy the [firebase/vanilla.html](examples/clients/firebase/vanilla.html)
765765
file and the [firebase/vanilla-success.html](examples/clients/firebase/vanilla-success.html) file,

api.php

Lines changed: 254 additions & 29 deletions
Large diffs are not rendered by default.

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function __construct(Config $config)
121121
new CacheController($router, $responder, $cache);
122122
break;
123123
case 'openapi':
124-
$openApi = new OpenApiService($reflection, $config->getOpenApiBase());
124+
$openApi = new OpenApiService($reflection, $config->getOpenApiBase(), $config->getControllers(), $config->getCustomOpenApiBuilders());
125125
new OpenApiController($router, $responder, $openApi);
126126
break;
127127
case 'geojson':
@@ -131,6 +131,12 @@ public function __construct(Config $config)
131131
break;
132132
}
133133
}
134+
foreach ($config->getCustomControllers() as $className) {
135+
if (class_exists($className)) {
136+
$records = new RecordService($db, $reflection);
137+
new $className($router, $responder, $records);
138+
}
139+
}
134140
$this->router = $router;
135141
$this->responder = $responder;
136142
$this->debug = $config->getDebug();

src/Tqdev/PhpCrudApi/Config.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Config
1313
'database' => null,
1414
'middlewares' => 'cors',
1515
'controllers' => 'records,geojson,openapi',
16+
'customControllers' => '',
17+
'customOpenApiBuilders' => '',
1618
'cacheType' => 'TempFile',
1719
'cachePath' => '',
1820
'cacheTime' => 10,
@@ -32,18 +34,24 @@ private function getDefaultDriver(array $values): string
3234
private function getDefaultPort(string $driver): int
3335
{
3436
switch ($driver) {
35-
case 'mysql':return 3306;
36-
case 'pgsql':return 5432;
37-
case 'sqlsrv':return 1433;
37+
case 'mysql':
38+
return 3306;
39+
case 'pgsql':
40+
return 5432;
41+
case 'sqlsrv':
42+
return 1433;
3843
}
3944
}
4045

4146
private function getDefaultAddress(string $driver): string
4247
{
4348
switch ($driver) {
44-
case 'mysql':return 'localhost';
45-
case 'pgsql':return 'localhost';
46-
case 'sqlsrv':return 'localhost';
49+
case 'mysql':
50+
return 'localhost';
51+
case 'pgsql':
52+
return 'localhost';
53+
case 'sqlsrv':
54+
return 'localhost';
4755
}
4856
}
4957

@@ -131,7 +139,17 @@ public function getMiddlewares(): array
131139

132140
public function getControllers(): array
133141
{
134-
return array_map('trim', explode(',', $this->values['controllers']));
142+
return array_filter(array_map('trim', explode(',', $this->values['controllers'])));
143+
}
144+
145+
public function getCustomControllers(): array
146+
{
147+
return array_filter(array_map('trim', explode(',', $this->values['customControllers'])));
148+
}
149+
150+
public function getCustomOpenApiBuilders(): array
151+
{
152+
return array_filter(array_map('trim', explode(',', $this->values['customOpenApiBuilders'])));
135153
}
136154

137155
public function getCacheType(): string

src/Tqdev/PhpCrudApi/Database/TypeConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function __construct(string $driver)
6363
'datetime' => 'timestamp',
6464
'year' => 'integer',
6565
'enum' => 'varchar',
66+
'set' => 'varchar',
6667
'json' => 'clob',
6768
],
6869
'pgsql' => [
@@ -102,7 +103,7 @@ public function __construct(string $driver)
102103
],
103104
// source: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-basic-data-types?view=sql-server-2017
104105
'sqlsrv' => [
105-
'varbinary(0)' => 'blob',
106+
'varbinary()' => 'blob',
106107
'bit' => 'boolean',
107108
'datetime' => 'timestamp',
108109
'datetime2' => 'timestamp',

src/Tqdev/PhpCrudApi/Middleware/Base/Middleware.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ protected function getArrayProperty(string $key, string $default): array
2424
return array_filter(array_map('trim', explode(',', $this->getProperty($key, $default))));
2525
}
2626

27+
protected function getMapProperty(string $key, string $default): array
28+
{
29+
$pairs = $this->getArrayProperty($key, $default);
30+
$result = array();
31+
foreach ($pairs as $pair) {
32+
if (strpos($pair, ':')) {
33+
list($k, $v) = explode(':', $pair, 2);
34+
$result[trim($k)] = trim($v);
35+
} else {
36+
$result[] = trim($pair);
37+
}
38+
}
39+
return $result;
40+
}
41+
2742
protected function getProperty(string $key, $default)
2843
{
2944
return isset($this->properties[$key]) ? $this->properties[$key] : $default;

src/Tqdev/PhpCrudApi/Middleware/JwtAuthMiddleware.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
use Psr\Http\Message\ResponseInterface;
66
use Psr\Http\Message\ServerRequestInterface;
77
use Psr\Http\Server\RequestHandlerInterface;
8-
use Tqdev\PhpCrudApi\Controller\Responder;
98
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
109
use Tqdev\PhpCrudApi\Record\ErrorCode;
1110
use Tqdev\PhpCrudApi\RequestUtils;
1211

1312
class JwtAuthMiddleware extends Middleware
1413
{
15-
private function getVerifiedClaims(string $token, int $time, int $leeway, int $ttl, string $secret, array $requirements): array
14+
private function getVerifiedClaims(string $token, int $time, int $leeway, int $ttl, array $secrets, array $requirements): array
1615
{
1716
$algorithms = array(
1817
'HS256' => 'sha256',
@@ -27,9 +26,14 @@ private function getVerifiedClaims(string $token, int $time, int $leeway, int $t
2726
return array();
2827
}
2928
$header = json_decode(base64_decode(strtr($token[0], '-_', '+/')), true);
30-
if (!$secret) {
29+
$kid = 0;
30+
if (isset($header['kid'])) {
31+
$kid = $header['kid'];
32+
}
33+
if (!isset($secrets[$kid])) {
3134
return array();
3235
}
36+
$secret = $secrets[$kid];
3337
if ($header['typ'] != 'JWT') {
3438
return array();
3539
}
@@ -93,16 +97,16 @@ private function getClaims(string $token): array
9397
$time = (int) $this->getProperty('time', time());
9498
$leeway = (int) $this->getProperty('leeway', '5');
9599
$ttl = (int) $this->getProperty('ttl', '30');
96-
$secret = $this->getProperty('secret', '');
100+
$secrets = $this->getMapProperty('secrets', '');
101+
if (!$secrets) {
102+
$secrets = [$this->getProperty('secret', '')];
103+
}
97104
$requirements = array(
98105
'alg' => $this->getArrayProperty('algorithms', ''),
99106
'aud' => $this->getArrayProperty('audiences', ''),
100107
'iss' => $this->getArrayProperty('issuers', ''),
101108
);
102-
if (!$secret) {
103-
return array();
104-
}
105-
return $this->getVerifiedClaims($token, $time, $leeway, $ttl, $secret, $requirements);
109+
return $this->getVerifiedClaims($token, $time, $leeway, $ttl, $secrets, $requirements);
106110
}
107111

108112
private function getAuthorizationToken(ServerRequestInterface $request): string

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ class OpenApiBuilder
1010
private $openapi;
1111
private $records;
1212
private $columns;
13+
private $builders;
1314

14-
public function __construct(ReflectionService $reflection, $base)
15+
public function __construct(ReflectionService $reflection, array $base, array $controllers, array $builders)
1516
{
1617
$this->openapi = new OpenApiDefinition($base);
17-
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
18-
$this->columns = new OpenApiColumnsBuilder($this->openapi, $reflection);
18+
$this->records = in_array('records', $controllers) ? new OpenApiRecordsBuilder($this->openapi, $reflection) : null;
19+
$this->columns = in_array('columns', $controllers) ? new OpenApiColumnsBuilder($this->openapi) : null;
20+
$this->builders = array();
21+
foreach ($builders as $className) {
22+
$this->builders[] = new $className($this->openapi, $reflection);
23+
}
1924
}
2025

2126
private function getServerUrl(): string
@@ -34,7 +39,15 @@ public function build(): OpenApiDefinition
3439
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
3540
$this->openapi->set("servers|0|url", $this->getServerUrl());
3641
}
37-
$this->records->build();
42+
if ($this->records) {
43+
$this->records->build();
44+
}
45+
if ($this->columns) {
46+
$this->columns->build();
47+
}
48+
foreach ($this->builders as $builder) {
49+
$builder->build();
50+
}
3851
return $this->openapi;
3952
}
4053
}

0 commit comments

Comments
 (0)