Skip to content

Commit

Permalink
Add http authorization for all url except api.ping
Browse files Browse the repository at this point in the history
  • Loading branch information
ooghry committed Aug 9, 2016
1 parent 11178af commit 6c64ae3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/autoload/dependencies.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'factories' => [
Application::class => ApplicationFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
App\Middleware\AuthMiddleware::class => App\Middleware\AuthMiddlewareFactory::class,
],
],
];
5 changes: 3 additions & 2 deletions config/autoload/middleware-pipeline.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'factories' => [
Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class,
Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class,
],
]
],
// This can be used to seed pre- and/or post-routing middleware
'middleware_pipeline' => [
Expand Down Expand Up @@ -40,6 +40,7 @@
// - pre-conditions
// - modifications to outgoing responses
Helper\ServerUrlMiddleware::class,
App\Middleware\AuthMiddleware::class,
],
'priority' => 10000,
],
Expand All @@ -62,7 +63,7 @@
'middleware' => [
// Add error middleware here.
],
'error' => true,
'error' => true,
'priority' => -10000,
],
],
Expand Down
49 changes: 49 additions & 0 deletions src/App/Middleware/AuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Helper\UrlHelper;

class AuthMiddleware
{
private $helper;

public function __construct(UrlHelper $helper)
{
$this->helper=$helper;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
if($this->helper->generate('api.ping')!=$request->getUri()->getPath()) {
$auth = $this->parseAuth($request->getHeaderLine('Authorization'));
if (!$auth or !$this->checkUserPass($auth['user'], $auth['pass'])) {
return $response
->withHeader('WWW-Authenticate', 'Basic realm=""')
->withStatus(401);
}
}
$response = $next($request, $response);
return $response;
}

private function parseAuth($header)
{
if (strpos($header, 'Basic') !== 0) {
return false;
}
$header = explode(':', base64_decode(substr($header, 6)));
return [
'user' => $header[0],
'pass' => isset($header[1]) ?? $header[1],
];
}

private function checkUserPass($user,$pass)
{
return ($user=='myuser' and $pass=='mypass');
}
}
17 changes: 17 additions & 0 deletions src/App/Middleware/AuthMiddlewareFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace App\Middleware;

use Interop\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Helper\UrlHelper;

class AuthMiddlewareFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$helper=new UrlHelper($container->get(RouterInterface::class));
return new AuthMiddleware($helper);
}
}

0 comments on commit 6c64ae3

Please sign in to comment.