Skip to content

Commit 58e0946

Browse files
ybelenkowing328
authored andcommitted
[Slim] Add Basic authentication middleware (#606)
* [Slim] Add Basic Authentication Middleware User needs to add own implementation to verifyCredentials method in AuthBasic.php. * [Slim] Update README template I'm not sure about `middlewareSrcPath` variable. I'll fix it in following PRs if path is broken. Hope that notice in README catches attention and most of users will read it. * Revert "[Slim] Update README template" This reverts commit 204ee02. * Revert "[Slim] Add Basic Authentication Middleware" This reverts commit 6a8e030. * [Slim] Add "tuupola/slim-basic-auth" package Package "tuupola/slim-basic-auth" 3.1.0 requires PHP 7, that's why I set it's version to ^3.0.0 in Composer. Minimum version will be 3.0.0-rc.1 which supports PHP 5.5. I've tested build with PHP 7, it would be nice to check build with PHP 5.5 someday. * [Slim] Update README template Not sure about forward slash in path to SlimRouter class. I will fix it in upcoming PRs if necessary. * [Slim] Refresh samples
1 parent f793ac2 commit 58e0946

20 files changed

Lines changed: 530 additions & 78 deletions

File tree

modules/openapi-generator/src/main/resources/php-slim-server/README.mustache

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ is an example of building a PHP Slim server.
77

88
This example uses the [Slim Framework](http://www.slimframework.com/). To see how to make this your own, please take a look at the template here:
99

10-
[TEMPLATES](https://github.com/openapitools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/slim/)
10+
[TEMPLATES](https://github.com/openapitools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/php-slim-server/)
11+
12+
{{#authMethods}}
13+
{{^hasMore}}
14+
## Authentication
15+
{{/hasMore}}
16+
{{/authMethods}}
17+
{{#authMethods}}
18+
{{#isBasic}}
19+
> Important! To make Basic Authentication work you need to implement `authenticator` function in [SlimRouter]({{srcBasePath}}/SlimRouter.php) class.
20+
> Documentation [tuupola/slim-basic-auth](https://github.com/tuupola/slim-basic-auth)
21+
{{/isBasic}}
22+
{{/authMethods}}

modules/openapi-generator/src/main/resources/php-slim-server/SlimRouter.mustache

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use {{apiPackage}}\{{classname}};
3939
use Slim\App;
4040
use Psr\Container\ContainerInterface;
4141
use InvalidArgumentException;
42+
use Tuupola\Middleware\HttpBasicAuthentication;
4243

4344
/**
4445
* SlimRouter Class Doc Comment
@@ -66,10 +67,29 @@ class SlimRouter {
6667
public function __construct($container = []) {
6768
$app = new App($container);
6869
70+
$basicAuth = new HttpBasicAuthentication([
71+
"secure" => false,
72+
"authenticator" => function ($arguments) {
73+
$user = $arguments["user"];
74+
$password = $arguments["password"];
75+
return false;
76+
}
77+
]);
78+
6979
{{#apis}}
7080
{{#operations}}
7181
{{#operation}}
72-
$app->{{httpMethod}}('{{{basePathWithoutHost}}}{{{path}}}', {{classname}}::class . ':{{operationId}}');
82+
$app->{{httpMethod}}(
83+
'{{{basePathWithoutHost}}}{{{path}}}', {{classname}}::class . ':{{operationId}}'
84+
{{#hasAuthMethods}}
85+
{{#authMethods}}
86+
{{#isBasic}}
87+
)->add(
88+
$basicAuth
89+
{{/isBasic}}
90+
{{/authMethods}}
91+
{{/hasAuthMethods}}
92+
);
7393
{{/operation}}
7494
{{/operations}}
7595
{{/apis}}

modules/openapi-generator/src/main/resources/php-slim-server/composer.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"minimum-stability": "RC",
33
"require": {
44
"php": ">=5.5",
5-
"slim/slim": "3.*"
5+
"slim/slim": "3.*",
6+
"tuupola/slim-basic-auth": "^3.0.0"
67
},
78
"require-dev": {
89
"phpunit/phpunit": "^4.8"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.1-SNAPSHOT
1+
3.1.2-SNAPSHOT

samples/server/petstore-security-test/php-slim/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ is an example of building a PHP Slim server.
77

88
This example uses the [Slim Framework](http://www.slimframework.com/). To see how to make this your own, please take a look at the template here:
99

10-
[TEMPLATES](https://github.com/openapitools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/slim/)
10+
[TEMPLATES](https://github.com/openapitools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/php-slim-server/)
11+
12+
## Authentication

samples/server/petstore-security-test/php-slim/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"minimum-stability": "RC",
33
"require": {
44
"php": ">=5.5",
5-
"slim/slim": "3.*"
5+
"slim/slim": "3.*",
6+
"tuupola/slim-basic-auth": "^3.0.0"
67
},
78
"require-dev": {
89
"phpunit/phpunit": "^4.8"

samples/server/petstore-security-test/php-slim/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* OpenAPI Petstore &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
4-
* @version 1.0.0 &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
3+
* OpenAPI Petstore *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
4+
* @version 1.0.0 *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
55
*/
66

77
require_once __DIR__ . '/vendor/autoload.php';

samples/server/petstore-security-test/php-slim/lib/AbstractApiController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
*/
1212

1313
/**
14-
* OpenAPI Petstore ' \" =end -- \\r\\n \\n \\r
14+
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
1515
*
16-
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ ' \" =end --
17-
* OpenAPI spec version: 1.0.0 ' \" =end -- \\r\\n \\n \\r
18-
* Contact: something@something.abc ' \" =end -- \\r\\n \\n \\r
16+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
17+
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
18+
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://github.com/openapitools/openapi-generator.git
2020
*/
2121

samples/server/petstore-security-test/php-slim/lib/Api/FakeApi.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
*/
1212

1313
/**
14-
* OpenAPI Petstore ' \" =end -- \\r\\n \\n \\r
14+
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
1515
*
16-
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ ' \" =end --
17-
* OpenAPI spec version: 1.0.0 ' \" =end -- \\r\\n \\n \\r
18-
* Contact: something@something.abc ' \" =end -- \\r\\n \\n \\r
16+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
17+
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
18+
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://github.com/openapitools/openapi-generator.git
2020
*/
2121

@@ -42,7 +42,7 @@ class FakeApi extends AbstractApiController {
4242

4343
/**
4444
* PUT testCodeInjectEndRnNR
45-
* Summary: To test code injection &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
45+
* Summary: To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
4646
* Notes:
4747
*
4848
* @param \Psr\Http\Message\ServerRequestInterface $request Request

samples/server/petstore-security-test/php-slim/lib/Model/ModelReturn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class ModelReturn {
1111

12-
/** @var int $return property description &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r*/
12+
/** @var int $return property description *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r*/
1313
private $return;
1414

1515
}

0 commit comments

Comments
 (0)