Added passive authorization
This commit is contained in:
parent
03fbe0990a
commit
2ecb79a2fa
5 changed files with 53 additions and 31 deletions
|
@ -1,6 +1,6 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.API = function(promise) {
|
App.API = function(promise, appState) {
|
||||||
|
|
||||||
var baseUrl = '/api/';
|
var baseUrl = '/api/';
|
||||||
|
|
||||||
|
@ -26,6 +26,9 @@ App.API = function(promise) {
|
||||||
|
|
||||||
return promise.make(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
|
headers: {
|
||||||
|
'X-Authorization-Token': appState.get('loginToken') || '',
|
||||||
|
},
|
||||||
success: function(data, textStatus, xhr) {
|
success: function(data, textStatus, xhr) {
|
||||||
resolve({
|
resolve({
|
||||||
status: xhr.status,
|
status: xhr.status,
|
||||||
|
|
|
@ -4,10 +4,12 @@ namespace Szurubooru;
|
||||||
final class Dispatcher
|
final class Dispatcher
|
||||||
{
|
{
|
||||||
private $router;
|
private $router;
|
||||||
|
private $authService;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
\Szurubooru\Router $router,
|
\Szurubooru\Router $router,
|
||||||
\Szurubooru\Helpers\HttpHelper $httpHelper,
|
\Szurubooru\Helpers\HttpHelper $httpHelper,
|
||||||
|
\Szurubooru\Services\AuthService $authService,
|
||||||
\Szurubooru\ControllerRepository $controllerRepository)
|
\Szurubooru\ControllerRepository $controllerRepository)
|
||||||
{
|
{
|
||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
|
@ -15,6 +17,7 @@ final class Dispatcher
|
||||||
|
|
||||||
//if script fails prematurely, mark it as fail from advance
|
//if script fails prematurely, mark it as fail from advance
|
||||||
$this->httpHelper->setResponseCode(500);
|
$this->httpHelper->setResponseCode(500);
|
||||||
|
$this->authService = $authService;
|
||||||
|
|
||||||
foreach ($controllerRepository->getControllers() as $controller)
|
foreach ($controllerRepository->getControllers() as $controller)
|
||||||
$controller->registerRoutes($router);
|
$controller->registerRoutes($router);
|
||||||
|
@ -26,6 +29,7 @@ final class Dispatcher
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$code = 200;
|
$code = 200;
|
||||||
|
$this->authorizeFromRequestHeader();
|
||||||
$json = (array) $this->router->handle(
|
$json = (array) $this->router->handle(
|
||||||
$this->httpHelper->getRequestMethod(),
|
$this->httpHelper->getRequestMethod(),
|
||||||
$this->httpHelper->getRequestUri());
|
$this->httpHelper->getRequestUri());
|
||||||
|
@ -47,4 +51,11 @@ final class Dispatcher
|
||||||
|
|
||||||
return $json;
|
return $json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function authorizeFromRequestHeader()
|
||||||
|
{
|
||||||
|
$loginToken = $this->httpHelper->getRequestHeader('X-Authorization-Token');
|
||||||
|
if ($loginToken)
|
||||||
|
$this->authService->loginFromToken($loginToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,17 @@ class HttpHelper
|
||||||
$this->output(json_encode((array) $data));
|
$this->output(json_encode((array) $data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRequestHeaders()
|
||||||
|
{
|
||||||
|
return getallheaders();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRequestHeader($key)
|
||||||
|
{
|
||||||
|
$headers = $this->getRequestHeaders();
|
||||||
|
return isset($headers[$key]) ? $headers[$key] : null;
|
||||||
|
}
|
||||||
|
|
||||||
public function getRequestMethod()
|
public function getRequestMethod()
|
||||||
{
|
{
|
||||||
return $_SERVER['REQUEST_METHOD'];
|
return $_SERVER['REQUEST_METHOD'];
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Szurubooru\Services;
|
namespace Szurubooru\Services;
|
||||||
|
|
||||||
final class AuthService
|
class AuthService
|
||||||
{
|
{
|
||||||
private $loggedInUser = null;
|
private $loggedInUser = null;
|
||||||
private $loginToken = null;
|
private $loginToken = null;
|
||||||
|
|
|
@ -3,23 +3,31 @@ namespace Szurubooru\Tests;
|
||||||
|
|
||||||
final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
|
final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
|
||||||
{
|
{
|
||||||
|
private $routerMock;
|
||||||
|
private $httpHelperMock;
|
||||||
|
private $authServiceMock;
|
||||||
|
private $controllerRepositoryMock;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->routerMock = $this->mock(\Szurubooru\Router::class);
|
||||||
|
$this->httpHelperMock = $this->mock(\Szurubooru\Helpers\HttpHelper::class);
|
||||||
|
$this->authServiceMock = $this->mock(\Szurubooru\Services\AuthService::class);
|
||||||
|
$this->controllerRepositoryMock = $this->mock(\Szurubooru\ControllerRepository::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDispatchingArrays()
|
public function testDispatchingArrays()
|
||||||
{
|
{
|
||||||
$expected = ['test' => 'toy'];
|
$expected = ['test' => 'toy'];
|
||||||
|
|
||||||
$httpHelperMock = $this->getHttpHelperMock();
|
$this->httpHelperMock
|
||||||
$httpHelperMock
|
|
||||||
->expects($this->exactly(2))
|
->expects($this->exactly(2))
|
||||||
->method('setResponseCode')
|
->method('setResponseCode')
|
||||||
->withConsecutive([$this->equalTo(500)], [$this->equalTo(200)]);
|
->withConsecutive([$this->equalTo(500)], [$this->equalTo(200)]);
|
||||||
|
$this->routerMock->expects($this->once())->method('handle')->willReturn($expected);
|
||||||
|
$this->controllerRepositoryMock->method('getControllers')->willReturn([]);
|
||||||
|
|
||||||
$routerMock = $this->getRouterMock();
|
$dispatcher = $this->getDispatcher();
|
||||||
$routerMock->expects($this->once())->method('handle')->willReturn($expected);
|
|
||||||
|
|
||||||
$controllerRepositoryMock = $this->getControllerRepositoryMock();
|
|
||||||
$controllerRepositoryMock->method('getControllers')->willReturn([]);
|
|
||||||
|
|
||||||
$dispatcher = new \Szurubooru\Dispatcher($routerMock, $httpHelperMock, $controllerRepositoryMock);
|
|
||||||
$actual = $dispatcher->run();
|
$actual = $dispatcher->run();
|
||||||
|
|
||||||
unset($actual['__time']);
|
unset($actual['__time']);
|
||||||
|
@ -32,33 +40,22 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
|
||||||
$classData->bunny = 5;
|
$classData->bunny = 5;
|
||||||
$expected = ['bunny' => 5];
|
$expected = ['bunny' => 5];
|
||||||
|
|
||||||
$httpHelperMock = $this->getHttpHelperMock();
|
$this->routerMock->expects($this->once())->method('handle')->willReturn($classData);
|
||||||
|
$this->controllerRepositoryMock->method('getControllers')->willReturn([]);
|
||||||
|
|
||||||
$routerMock = $this->getRouterMock();
|
$dispatcher = $this->getDispatcher();
|
||||||
$routerMock->expects($this->once())->method('handle')->willReturn($classData);
|
|
||||||
|
|
||||||
$controllerRepositoryMock = $this->getControllerRepositoryMock();
|
|
||||||
$controllerRepositoryMock->method('getControllers')->willReturn([]);
|
|
||||||
|
|
||||||
$dispatcher = new \Szurubooru\Dispatcher($routerMock, $httpHelperMock, $controllerRepositoryMock);
|
|
||||||
$actual = $dispatcher->run();
|
$actual = $dispatcher->run();
|
||||||
|
|
||||||
unset($actual['__time']);
|
unset($actual['__time']);
|
||||||
$this->assertEquals($expected, $actual);
|
$this->assertEquals($expected, $actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getHttpHelperMock()
|
private function getDispatcher()
|
||||||
{
|
{
|
||||||
return $this->getMockBuilder(\Szurubooru\Helpers\HttpHelper::class)->disableOriginalConstructor()->getMock();
|
return new \Szurubooru\Dispatcher(
|
||||||
}
|
$this->routerMock,
|
||||||
|
$this->httpHelperMock,
|
||||||
private function getRouterMock()
|
$this->authServiceMock,
|
||||||
{
|
$this->controllerRepositoryMock);
|
||||||
return $this->getMockBuilder(\Szurubooru\Router::class)->disableOriginalConstructor()->getMock();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getControllerRepositoryMock()
|
|
||||||
{
|
|
||||||
return $this->getMockBuilder(\Szurubooru\ControllerRepository::class)->disableOriginalConstructor()->getMock();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue