Example #1
0
 /**
  * Bootstrap any application services.
  *
  * @param Router $api
  */
 public function boot(Router $api)
 {
     $path = app_path('Api/routes.php');
     $api->version('v1', ['namespace' => $this->namespace], function ($api) use($path) {
         require $path;
     });
 }
Example #2
0
 /**
  * Send the request through the Dingo router.
  *
  * @param \Dingo\Api\Http\Request $request
  *
  * @return \Dingo\Api\Http\Response
  */
 protected function sendRequestThroughRouter(HttpRequest $request)
 {
     $this->app->instance('request', $request);
     return (new Pipeline($this->app))->send($request)->through($this->middleware)->then(function ($request) {
         return $this->router->dispatch($request);
     });
 }
Example #3
0
 /**
  * Create a new routes command instance.
  *
  * @param \Dingo\Api\Routing\Router $router
  *
  * @return void
  */
 public function __construct(Router $router)
 {
     // Ugly, but we need to bypass the constructor and directly target the
     // constructor on the command class.
     Command::__construct();
     $this->routes = $router->getRoutes();
 }
 /**
  * Perform authentication before a request is executed.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure $next
  * @param $grant
  *
  * @return mixed
  * @throws AccessDeniedException
  */
 public function handle($request, Closure $next, $grant = null)
 {
     $route = $this->router->getCurrentRoute();
     /**
      * FOR (Internal API requests)
      * @note GRANT(user) will always be able to access routes that are protected by: GRANT(client)
      *
      * For OAuth grants from password (i.e. Resource Owner: user)
      * @Auth will only check once, because user exists in auth afterwards
      *
      * For OAuth grants from client_credentials (i.e. Resource Owner: client)
      * @Auth will always check, because user is never exists in auth
      */
     if (!$this->auth->check(false)) {
         $this->auth->authenticate($route->getAuthenticationProviders());
         $provider = $this->auth->getProviderUsed();
         /** @var OAuth2 $provider */
         if ($provider instanceof OAuth2) {
             // check oauth grant type
             if (!is_null($grant) && $provider->getResourceOwnerType() !== $grant) {
                 throw new AccessDeniedException();
             }
         }
         // login user through Auth
         $user = $this->auth->getUser();
         if ($user instanceof User) {
             \Auth::login($user);
             event(new UserLoggedInEvent($user));
         }
     }
     return $next($request);
 }
Example #5
0
 /**
  * Attach the after filter to adjust the response.
  *
  * @return void
  */
 protected function attachAfterFilter()
 {
     $this->router->after(function (Request $request, Response $response) {
         $response->headers->set('X-RateLimit-Limit', $this->limiter->getThrottle()->getLimit());
         $response->headers->set('X-RateLimit-Remaining', $this->limiter->getRemainingLimit());
         $response->headers->set('X-RateLimit-Reset', $this->limiter->getRateLimitReset());
     });
 }
Example #6
0
 /**
  * Perform authentication before a request is executed.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $route = $this->router->getCurrentRoute();
     if (!$this->auth->check(false)) {
         $this->auth->authenticate($route->getAuthProviders());
     }
     return $next($request);
 }
Example #7
0
 /**
  * Handle the request.
  *
  * @param \Dingo\Api\Http\Request $request
  * @param \Closure                $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     // To prepare the controller all we need to do is call the current method on the router to fetch
     // the current route. This will create a new Dingo\Api\Routing\Route instance and prepare the
     // controller by binding it as a singleton in the container. This will result in the
     // controller only be instantiated once per request.
     $this->router->current();
     return $next($request);
 }
 /**
  * Define the routes for the application.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function map(Router $router, ApiRouter $apiRouter)
 {
     $apiRouter->version($this->version, function ($apiRouter) use($router) {
         $apiRouter->group(['namespace' => $this->namespace], function ($api) use($router) {
             $router->group(['namespace' => $this->namespace], function ($router) use($api) {
                 require app_path('Http/routes.php');
             });
         });
     });
 }
Example #9
0
 /**
  * Register the router.
  */
 protected function registerRouter()
 {
     $this->app->singleton('api.router', function ($app) {
         $router = new Router($app['Dingo\\Api\\Contract\\Routing\\Adapter'], $app['Dingo\\Api\\Contract\\Debug\\ExceptionHandler'], $app, $this->config('domain'), $this->config('prefix'));
         $router->setConditionalRequest($this->config('conditionalRequest'));
         return $router;
     });
     $this->app->singleton('Dingo\\Api\\Routing\\ResourceRegistrar', function ($app) {
         return new ResourceRegistrar($app['Dingo\\Api\\Routing\\Router']);
     });
 }
Example #10
0
 /**
  * Register the router.
  */
 protected function registerRouter()
 {
     $this->app->singleton('api.router', function ($app) {
         $router = new Router($app[Adapter::class], $app[ExceptionHandler::class], $app, $this->config('domain'), $this->config('prefix'));
         $router->setConditionalRequest($this->config('conditionalRequest'));
         return $router;
     });
     $this->app->singleton(ResourceRegistrar::class, function ($app) {
         return new ResourceRegistrar($app[Router::class]);
     });
 }
 /**
  * Checks if the user has permission to visit the route.
  * By default it will check if the current route name is in the user permissions.
  * If $permissions is provided, it will check if the $permission value is in the user permissions
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  * @param null|string                      $permission
  * @return mixed
  */
 public function handle($request, Closure $next, $permission = null)
 {
     if (is_null($permission)) {
         $name = $this->router->getCurrentRoute()->getName();
         $permission = is_null($name) ? $this->router->getCurrentRoute()->getActionName() : $name;
     }
     if (Entrust::can($permission)) {
         return $next($request);
     } else {
         return response('Unauthorized.', 401);
     }
 }
Example #12
0
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = new Collection();
     foreach ($this->router->getRoutes() as $collections) {
         foreach ($collections as $route) {
             if ($controller = $route->getController()) {
                 $controllers[] = $controller;
             }
         }
     }
     return $controllers;
 }
 public function __construct(RouteCollection $collection, Router $router, Config $config)
 {
     $this->routes = $collection;
     $standardsTree = $config['api.standardsTree'];
     $subtype = $config['api.subtype'];
     $defaultFormat = $config['api.defaultFormat'];
     foreach ($router->getAdapterRoutes() as $versionName => $versionGroup) {
         foreach ($versionGroup as $route) {
             $routeInfo = (new RouteInfo($route, ['router' => 'Dingo', 'version' => $versionName, 'headers' => [['key' => 'Accept', 'value' => "application/{$standardsTree}.{$subtype}.{$versionName}+{$defaultFormat}"]]]))->toArray();
             $this->routes->push($routeInfo);
         }
     }
 }
 /**
  * Merge routes defined by packages into API router.
  *
  * @param  \Dingo\Api\Routing\Router  $api
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 protected function mergePackgeRoutes(ApiRouter &$api, Router $router)
 {
     foreach ($router->getRoutes() as $route) {
         $api->version(array_keys($api->getRoutes()), function ($api) use($route) {
             $action = $route->getAction();
             // Remove prefix if present
             if (isset($action['prefix'])) {
                 unset($action['prefix']);
             }
             $api->addRoute($route->getMethods(), $route->uri(), $action);
         });
     }
 }
 /**
  * Replace the request input with the previous request input.
  *
  * @return void
  */
 protected function replaceRequestInput()
 {
     array_pop($this->requestStack);
     $previous = end($this->requestStack);
     $this->router->setCurrentRequest($previous);
     $this->request->replace($previous->input());
 }
Example #16
0
 /**
  * Perform rate limiting before a request is executed.
  *
  * @param \Dingo\Api\Http\Request $request
  * @param \Closure                $next
  *
  * @throws \Symfony\Component\HttpKernel\Exception\HttpException
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $route = $this->router->getCurrentRoute();
     if ($route->hasThrottle()) {
         $this->handler->setThrottle($route->getThrottle());
     }
     $this->handler->rateLimitRequest($request, $route->getRateLimit(), $route->getRateExpiration());
     if ($this->handler->exceededRateLimit()) {
         throw new HttpException(403, 'You have exceeded your rate limit.', null, $this->getHeaders());
     }
     $response = $next($request);
     if ($this->handler->requestWasRateLimited()) {
         return $this->responseWithHeaders($response);
     }
     return $response;
 }
Example #17
0
 /**
  * Gather the middlewares for the route.
  *
  * @param \Dingo\Api\Http\Request $request
  *
  * @return array
  */
 protected function gatherRouteMiddlewares($request)
 {
     if ($route = $request->route()) {
         return $this->router->gatherRouteMiddlewares($route);
     }
     return [];
 }
Example #18
0
 /**
  * Compile the routes into a displayable format.
  *
  * @return array
  */
 protected function getRoutes()
 {
     $routes = [];
     foreach ($this->router->getRoutes() as $collection) {
         foreach ($collection->getRoutes() as $route) {
             $routes[] = $this->filterRoute(['host' => $route->domain(), 'uri' => implode('|', $route->methods()) . ' ' . $route->uri(), 'name' => $route->getName(), 'action' => $route->getActionName(), 'protected' => $route->isProtected() ? 'Yes' : 'No', 'versions' => implode(', ', $route->versions()), 'scopes' => implode(', ', $route->scopes()), 'rate' => $this->routeRateLimit($route)]);
         }
     }
     if ($sort = $this->option('sort')) {
         $routes = Arr::sort($routes, function ($value) use($sort) {
             return $value[$sort];
         });
     }
     if ($this->option('reverse')) {
         $routes = array_reverse($routes);
     }
     return array_filter(array_unique($routes, SORT_REGULAR));
 }
Example #19
0
 /**
  * Authenticate the current request.
  *
  * @param array $providers
  *
  * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
  *
  * @return mixed
  */
 public function authenticate(array $providers = [])
 {
     $exceptionStack = [];
     // Spin through each of the registered authentication providers and attempt to
     // authenticate through one of them. This allows a developer to implement
     // and allow a number of different authentication mechanisms.
     foreach ($this->filterProviders($providers) as $provider) {
         try {
             $user = $provider->authenticate($this->router->getCurrentRequest(), $this->router->getCurrentRoute());
             $this->providerUsed = $provider;
             return $this->user = $user;
         } catch (UnauthorizedHttpException $exception) {
             $exceptionStack[] = $exception;
         } catch (BadRequestHttpException $exception) {
             // We won't add this exception to the stack as it's thrown when the provider
             // is unable to authenticate due to the correct authorization header not
             // being set. We will throw an exception for this below.
         }
     }
     $this->throwUnauthorizedException($exceptionStack);
 }
Example #20
0
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = new Collection();
     foreach ($this->router->getRoutes() as $collections) {
         foreach ($collections as $route) {
             if ($controller = $route->getController()) {
                 $this->addControllerIfNotExists($controllers, $controller);
             }
         }
     }
     return $controllers;
 }
Example #21
0
File: Docs.php Project: rit-sse/api
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = [];
     foreach ($this->router->getRoutes() as $route) {
         $actionName = $route->getActionName();
         if (!empty($actionName) && $actionName !== 'Closure') {
             $segments = explode('@', $actionName);
             $controllers[] = $segments[0];
         }
     }
     $controllerCollection = new Collection(array_unique($controllers));
     return $controllerCollection;
 }
Example #22
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  * @return mixed
  *
  */
 public function handle($request, \Closure $next)
 {
     $route = $this->router->getCurrentRoute();
     if ($route->usesController()) {
         /** @var Controller $controller */
         $controller = $route->getController();
         if (property_exists($controller, 'crsfExclusions') and method_exists($controller, 'getCrsfExclusions')) {
             $excludedMethodNames = $controller->getCrsfExclusions();
             $action = $route->getAction();
             $use = isset($action['uses']) ? $action['uses'] : $action['controller'];
             if (!is_null($use)) {
                 list(, $methods) = explode('@', $use);
                 foreach (explode(',', $methods) as $method) {
                     if (method_exists($controller, $method) and in_array($method, $excludedMethodNames)) {
                         // Skip it!
                         return $next($request);
                     }
                 }
             }
         }
     }
     parent::handle($request, $next);
 }
Example #23
0
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = new Collection();
     foreach ($this->router->getRoutes() as $collections) {
         foreach ($collections as $route) {
             if ($controller = $route->getController()) {
                 if (!$controllers->has(get_class($controller))) {
                     $controllers->put(get_class($controller), $controller);
                 }
             }
         }
     }
     return $controllers;
 }
Example #24
0
 /**
  * Refresh the request stack.
  *
  * This is done by resetting the authentication, popping
  * the last request from the stack, replacing the input,
  * and resetting the version and parameters.
  *
  * @return void
  */
 protected function refreshRequestStack()
 {
     if (!$this->persistAuthentication) {
         $this->auth->setUser(null);
         $this->persistAuthentication = true;
     }
     if ($route = array_pop($this->routeStack)) {
         $this->router->setCurrentRoute($route);
     }
     $this->replaceRequestInstance();
     $this->clearCachedFacadeInstance();
     $this->raw = false;
     $this->version = $this->domain = $this->content = null;
     $this->parameters = $this->uploads = [];
 }
 /**
  * Replace the bound router.
  *
  * @return void
  */
 protected function replaceBoundRouter()
 {
     $routes = $this->app['router']->getRoutes();
     $this->app->bindShared('router', function ($app) use($routes) {
         $router = new Router($app['events'], $app['api.properties'], $app);
         if ($app['env'] == 'testing') {
             $router->disableFilters();
         }
         $router->setControllerDispatcher(new ControllerDispatcher($router, $app));
         $router->setConditionalRequest($app['config']->get('api::conditional_request'));
         $router->setStrict($app['config']->get('api::strict'));
         $router->addExistingRoutes($routes);
         return $router;
     });
 }
 /**
  * Register and replace the bound router.
  *
  * @return void
  */
 protected function registerRouter()
 {
     $this->app['router'] = $this->app->share(function ($app) {
         $router = new Router($app['events'], $app);
         if ($app['env'] == 'testing') {
             $router->disableFilters();
         }
         return $router;
     });
 }
Example #27
0
 /**
  * Handle the revising of a controller for API requests.
  *
  * @param \Illuminate\Routing\Route $route
  * @param \Illuminate\Http\Request  $request
  *
  * @return void
  */
 public function handle(Route $route, Request $request)
 {
     if ($this->router->isApiRequest($request)) {
         $this->reviser->revise($route);
     }
 }
Example #28
0
 /**
  * Register the router.
  *
  * @return void
  */
 protected function registerRouter()
 {
     $this->app->singleton('api.router', function ($app) {
         $config = $app['config']['api'];
         $router = new Router($app['api.router.adapter'], new Http\Parser\Accept($config['standardsTree'], $config['subtype'], $config['version'], $config['defaultFormat']), $app['Dingo\\Api\\Contract\\Debug\\ExceptionHandler'], $app, $config['domain'], $config['prefix']);
         $router->setConditionalRequest($config['conditionalRequest']);
         return $router;
     });
     $this->app->singleton('Dingo\\Api\\Routing\\ResourceRegistrar', function ($app) {
         return new ResourceRegistrar($app['api.router']);
     });
 }
Example #29
0
 /**
  * Replace the request instance with the previous request instance.
  *
  * @return void
  */
 protected function replaceRequestInstance()
 {
     array_pop($this->requestStack);
     $this->container->instance('request', end($this->requestStack));
     $this->router->setCurrentRequest($this->container['request']);
 }
 public function map(\Dingo\Api\Routing\Router $api)
 {
     $api->version(['version' => 'v1'], function ($api) {
         return require app_path('Api/v1/Http/routes.php');
     });
 }