-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add http authorization for all url except api.ping
- Loading branch information
Showing
4 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |