diff --git a/composer.json b/composer.json index 9c371f52..a5a4fc19 100755 --- a/composer.json +++ b/composer.json @@ -36,11 +36,11 @@ "fastd/swoole": "~1.0.0", "fastd/testing": "~0.1.0", "fastd/middleware": "~1.0.0", + "fastd/basic-authenticate": "~0.1.0", "catfan/Medoo": "~1.1.0", "symfony/cache": "~3.2", "symfony/console": "^3.2", - "monolog/monolog": "~1.0", - "tuupola/slim-basic-auth": "^2.2" + "monolog/monolog": "~1.0" }, "require-dev": { "phpunit/phpunit": "^5.0" diff --git a/src/Auth/BasicAuthenticationMiddleware.php b/src/Auth/BasicAuthenticationMiddleware.php deleted file mode 100644 index 44c35384..00000000 --- a/src/Auth/BasicAuthenticationMiddleware.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @copyright 2016 - * - * @link https://www.github.com/janhuang - * @link http://www.fast-d.cn/ - */ - -namespace FastD\Auth; - -use FastD\Http\JsonResponse; -use FastD\Http\Response; -use FastD\Middleware\DelegateInterface; -use FastD\Middleware\ServerMiddleware; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; -use Slim\Middleware\HttpBasicAuthentication; - -/** - * Class BasicAuthenticationMiddleware - * @package FastD\Auth - */ -class BasicAuthenticationMiddleware extends ServerMiddleware -{ - public $defaultJson = [ - 'msg' => 'not allow access', - 'code' => 401 - ]; - - /** - * @param ServerRequestInterface $serverRequest - * @param DelegateInterface $delegate - * @return ResponseInterface - */ - public function handle(ServerRequestInterface $serverRequest, DelegateInterface $delegate) - { - $options = config()->get('basic.auth', []); - - $auth = new HttpBasicAuthentication($options); - - return $auth($serverRequest, json(isset($options['json']) ? $options['json'] : $this->defaultJson), $delegate); - } -} \ No newline at end of file diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index c128e9b6..4e60a7ff 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -90,7 +90,7 @@ public function testModel() $response = $app->handleRequest($this->createRequest('GET', '/model')); -// echo $response->getBody(); + echo $response->getBody(); } public function testAuth() @@ -99,6 +99,8 @@ public function testAuth() $response = $app->handleRequest($this->createRequest('GET', '/auth')); + echo $response->getBody(); + $this->assertEquals(json_encode([ 'msg' => 'not allow access', 'code' => 401 @@ -109,6 +111,8 @@ public function testAuth() 'PHP_AUTH_PW' => 'bar' ])); + echo $response->getBody(); + $this->assertEquals(json_encode([ 'foo' => 'bar' ]), (string) $response->getBody()); diff --git a/tests/config/app.php b/tests/config/app.php index 396a5c3d..df83cddd 100644 --- a/tests/config/app.php +++ b/tests/config/app.php @@ -44,18 +44,27 @@ * Http middleware */ 'middleware' => [ - 'basic.auth' => \FastD\Auth\BasicAuthenticationMiddleware::class + 'basic.auth' => new FastD\BasicAuthenticate\HttpBasicAuthentication([ + 'authenticator' => [ + 'class' => \FastD\BasicAuthenticate\PhpAuthenticator::class, + 'params' => [ + 'foo' => 'bar' + ] + ], + 'response' => [ + 'class' => \FastD\Http\JsonResponse::class, + 'data' => [ + 'msg' => 'not allow access', + 'code' => 401 + ] + ] + ]) ], /** - * HTTP basic auth + * User custom configure */ - 'basic.auth' => [ - 'secure' => false, // https - 'users' => [ - 'foo' => 'bar' - ] - ], + 'config' => include __DIR__ . '/config.php', /** * Database config diff --git a/tests/config/config.php b/tests/config/config.php new file mode 100644 index 00000000..0703aa04 --- /dev/null +++ b/tests/config/config.php @@ -0,0 +1,12 @@ + + * @copyright 2016 + * + * @link https://www.github.com/janhuang + * @link http://www.fast-d.cn/ + */ + +return [ + +]; \ No newline at end of file