More cross-domain improvements

This commit is contained in:
Alejandro Celaya 2016-07-05 19:19:23 +02:00
parent bd36c65a73
commit baf5936cf1
8 changed files with 73 additions and 124 deletions

View File

@ -23,13 +23,13 @@ return [
'name' => 'rest-create-shortcode',
'path' => '/rest/short-codes',
'middleware' => Rest\CreateShortcodeMiddleware::class,
'allowed_methods' => ['POST'],
'allowed_methods' => ['POST', 'OPTIONS'],
],
[
'name' => 'rest-resolve-url',
'path' => '/rest/short-codes/{shortCode}',
'middleware' => Rest\ResolveUrlMiddleware::class,
'allowed_methods' => ['GET'],
'allowed_methods' => ['GET', 'OPTIONS'],
],
[
'name' => 'rest-list-shortened-url',
@ -41,7 +41,7 @@ return [
'name' => 'rest-get-visits',
'path' => '/rest/visits/{shortCode}',
'middleware' => Rest\GetVisitsMiddleware::class,
'allowed_methods' => ['GET'],
'allowed_methods' => ['GET', 'OPTIONS'],
],
],

View File

@ -59,10 +59,12 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
// If current route is the authenticate route, continue to the next middleware
// If current route is the authenticate route or an OPTIONS request, continue to the next middleware
/** @var RouteResult $routeResult */
$routeResult = $request->getAttribute(RouteResult::class);
if (isset($routeResult) && $routeResult->getMatchedRouteName() === 'rest-authenticate') {
if ((isset($routeResult) && $routeResult->getMatchedRouteName() === 'rest-authenticate')
|| strtolower($request->getMethod()) === 'options'
) {
return $out($request, $response);
}

View File

@ -0,0 +1,51 @@
<?php
namespace Acelaya\UrlShortener\Middleware\Rest;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
abstract class AbstractRestMiddleware implements MiddlewareInterface
{
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
if (strtolower($request->getMethod()) === 'options') {
return $response;
}
return $this->dispatch($request, $response, $out);
}
/**
* @param Request $request
* @param Response $response
* @param callable|null $out
* @return null|Response
*/
abstract protected function dispatch(Request $request, Response $response, callable $out = null);
}

View File

@ -9,9 +9,8 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Stratigility\MiddlewareInterface;
class AuthenticateMiddleware implements MiddlewareInterface
class AuthenticateMiddleware extends AbstractRestMiddleware
{
/**
* @var RestTokenServiceInterface
@ -30,36 +29,13 @@ class AuthenticateMiddleware implements MiddlewareInterface
}
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @param callable|null $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function dispatch(Request $request, Response $response, callable $out = null)
{
if (strtolower($request->getMethod()) === 'options') {
return $response;
}
$authData = $request->getParsedBody();
if (! isset($authData['username'], $authData['password'])) {
return new JsonResponse([

View File

@ -10,9 +10,8 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\Uri;
use Zend\Stratigility\MiddlewareInterface;
class CreateShortcodeMiddleware implements MiddlewareInterface
class CreateShortcodeMiddleware extends AbstractRestMiddleware
{
/**
* @var UrlShortener|UrlShortenerInterface
@ -38,31 +37,12 @@ class CreateShortcodeMiddleware implements MiddlewareInterface
}
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @param callable|null $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function dispatch(Request $request, Response $response, callable $out = null)
{
$postData = $request->getParsedBody();
if (! isset($postData['longUrl'])) {

View File

@ -9,9 +9,8 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Stratigility\MiddlewareInterface;
class GetVisitsMiddleware implements MiddlewareInterface
class GetVisitsMiddleware extends AbstractRestMiddleware
{
/**
* @var VisitsTrackerInterface
@ -30,31 +29,12 @@ class GetVisitsMiddleware implements MiddlewareInterface
}
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @param callable|null $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function dispatch(Request $request, Response $response, callable $out = null)
{
$shortCode = $request->getAttribute('shortCode');

View File

@ -10,9 +10,8 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Stdlib\ArrayUtils;
use Zend\Stratigility\MiddlewareInterface;
class ListShortcodesMiddleware implements MiddlewareInterface
class ListShortcodesMiddleware extends AbstractRestMiddleware
{
use PaginatorSerializerTrait;
@ -33,31 +32,12 @@ class ListShortcodesMiddleware implements MiddlewareInterface
}
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @param callable|null $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function dispatch(Request $request, Response $response, callable $out = null)
{
try {
$query = $request->getQueryParams();

View File

@ -9,9 +9,8 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Stratigility\MiddlewareInterface;
class ResolveUrlMiddleware implements MiddlewareInterface
class ResolveUrlMiddleware extends AbstractRestMiddleware
{
/**
* @var UrlShortenerInterface
@ -30,31 +29,12 @@ class ResolveUrlMiddleware implements MiddlewareInterface
}
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @param callable|null $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function dispatch(Request $request, Response $response, callable $out = null)
{
$shortCode = $request->getAttribute('shortCode');