mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 06:47:07 +00:00
[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 commit204ee02fd8. * Revert "[Slim] Add Basic Authentication Middleware" This reverts commit6a8e03079a. * [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
This commit is contained in:
committed by
William Cheng
parent
f793ac25c7
commit
58e0946b1e
@@ -7,4 +7,16 @@ is an example of building a PHP Slim server.
|
||||
|
||||
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:
|
||||
|
||||
[TEMPLATES](https://github.com/openapitools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/slim/)
|
||||
[TEMPLATES](https://github.com/openapitools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/php-slim-server/)
|
||||
|
||||
{{#authMethods}}
|
||||
{{^hasMore}}
|
||||
## Authentication
|
||||
{{/hasMore}}
|
||||
{{/authMethods}}
|
||||
{{#authMethods}}
|
||||
{{#isBasic}}
|
||||
> Important! To make Basic Authentication work you need to implement `authenticator` function in [SlimRouter]({{srcBasePath}}/SlimRouter.php) class.
|
||||
> Documentation [tuupola/slim-basic-auth](https://github.com/tuupola/slim-basic-auth)
|
||||
{{/isBasic}}
|
||||
{{/authMethods}}
|
||||
|
||||
@@ -39,6 +39,7 @@ use {{apiPackage}}\{{classname}};
|
||||
use Slim\App;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use InvalidArgumentException;
|
||||
use Tuupola\Middleware\HttpBasicAuthentication;
|
||||
|
||||
/**
|
||||
* SlimRouter Class Doc Comment
|
||||
@@ -66,10 +67,29 @@ class SlimRouter {
|
||||
public function __construct($container = []) {
|
||||
$app = new App($container);
|
||||
|
||||
$basicAuth = new HttpBasicAuthentication([
|
||||
"secure" => false,
|
||||
"authenticator" => function ($arguments) {
|
||||
$user = $arguments["user"];
|
||||
$password = $arguments["password"];
|
||||
return false;
|
||||
}
|
||||
]);
|
||||
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
$app->{{httpMethod}}('{{{basePathWithoutHost}}}{{{path}}}', {{classname}}::class . ':{{operationId}}');
|
||||
$app->{{httpMethod}}(
|
||||
'{{{basePathWithoutHost}}}{{{path}}}', {{classname}}::class . ':{{operationId}}'
|
||||
{{#hasAuthMethods}}
|
||||
{{#authMethods}}
|
||||
{{#isBasic}}
|
||||
)->add(
|
||||
$basicAuth
|
||||
{{/isBasic}}
|
||||
{{/authMethods}}
|
||||
{{/hasAuthMethods}}
|
||||
);
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"minimum-stability": "RC",
|
||||
"require": {
|
||||
"php": ">=5.5",
|
||||
"slim/slim": "3.*"
|
||||
"slim/slim": "3.*",
|
||||
"tuupola/slim-basic-auth": "^3.0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^4.8"
|
||||
|
||||
Reference in New Issue
Block a user