|
| 1 | +<?php namespace Dingo\Api\Http\Middleware; |
| 2 | + |
| 3 | +use Dingo\Api\Http\Response; |
| 4 | +use Illuminate\Routing\Route; |
| 5 | +use Dingo\Api\Http\InternalRequest; |
| 6 | +use Symfony\Component\HttpFoundation\Request; |
| 7 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 8 | +use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
| 9 | + |
| 10 | +class Authentication implements HttpKernelInterface { |
| 11 | + |
| 12 | + /** |
| 13 | + * The wrapped kernel implementation. |
| 14 | + * |
| 15 | + * @var \Symfony\Component\HttpKernel\HttpKernelInterface |
| 16 | + */ |
| 17 | + protected $app; |
| 18 | + |
| 19 | + /** |
| 20 | + * Create a new authentication middleware instance. |
| 21 | + * |
| 22 | + * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function __construct(HttpKernelInterface $app) |
| 26 | + { |
| 27 | + $this->app = $app; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Handle a given request and return the response. |
| 32 | + * |
| 33 | + * @param \Symfony\Component\HttpFoundation\Request $request |
| 34 | + * @param int $type |
| 35 | + * @param bool $catch |
| 36 | + * @return \Symfony\Component\HttpFoundation\Response |
| 37 | + * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException |
| 38 | + */ |
| 39 | + public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
| 40 | + { |
| 41 | + // Our middleware needs to ensure that Laravel is booted before we |
| 42 | + // can do anything. This gives us access to all the booted |
| 43 | + // service providers and other container bindings. |
| 44 | + $this->app->boot(); |
| 45 | + |
| 46 | + if ($request instanceof InternalRequest or $this->app->make('dingo.api.auth')->user()) |
| 47 | + { |
| 48 | + return $this->app->handle($request, $type, $catch); |
| 49 | + } |
| 50 | + |
| 51 | + $router = $this->app->make('router'); |
| 52 | + |
| 53 | + $response = null; |
| 54 | + |
| 55 | + // If a collection exists for the request and we can match a route |
| 56 | + // from the request then we'll check to see if the route is |
| 57 | + // protected and, if it is, we'll attempt to authenticate. |
| 58 | + if ($collection = $router->getApiRouteCollectionFromRequest($request) and $route = $collection->match($request)) |
| 59 | + { |
| 60 | + if ($this->routeIsProtected($route)) |
| 61 | + { |
| 62 | + $response = $this->authenticate($request, $route); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return $response ?: $this->app->handle($request, $type, $catch); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Authenticate the request for the given route. |
| 71 | + * |
| 72 | + * @param \Symfony\Component\HttpFoundation\Request $request |
| 73 | + * @param \Illuminate\Routing\Route $route |
| 74 | + * @return null|\Dingo\Api\Http\Response |
| 75 | + * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException |
| 76 | + */ |
| 77 | + protected function authenticate(Request $request, Route $route) |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + $this->app->make('dingo.api.auth')->authenticate($request, $route); |
| 82 | + } |
| 83 | + catch (UnauthorizedHttpException $exception) |
| 84 | + { |
| 85 | + $router = $this->app->make('router'); |
| 86 | + |
| 87 | + $response = $router->handleException($exception); |
| 88 | + |
| 89 | + return Response::makeFromExisting($response)->morph($router->getRequestedFormat()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Determine if a route is protected. |
| 95 | + * |
| 96 | + * @param \Illuminate\Routing\Route $route |
| 97 | + * @return bool |
| 98 | + */ |
| 99 | + protected function routeIsProtected(Route $route) |
| 100 | + { |
| 101 | + $action = $route->getAction(); |
| 102 | + |
| 103 | + return in_array('protected', $action, true) or (isset($action['protected']) and $action['protected'] === true); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments