Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
100 changes: 43 additions & 57 deletions src/ApiServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Dingo\Api;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheeky space snuck in:

screen shot 2014-05-21 at 2 38 36 pm


use Dingo\Api\Auth\SentryShield;
use RuntimeException;
use Dingo\Api\Auth\Shield;
use Dingo\Api\Http\Response;
Expand All @@ -10,19 +11,24 @@
use Illuminate\Support\ServiceProvider;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class ApiServiceProvider extends ServiceProvider {
class ApiServiceProvider extends ServiceProvider
{

/**
* Boot the service provider.
*
*
* @return void
*/
public function boot()
{
$this->package('dingo/api', 'api', __DIR__);

$this->app['Dingo\Api\Dispatcher'] = function($app) { return $app['dingo.api.dispatcher']; };
$this->app['Dingo\Api\Auth\Shield'] = function($app) { return $app['dingo.api.auth']; };
$this->app['Dingo\Api\Dispatcher'] = function ($app) {
return $app['dingo.api.dispatcher'];
};
$this->app['Dingo\Api\Auth\Shield'] = function ($app) {
return $app['dingo.api.auth'];
};

$formats = $this->prepareResponseFormats();

Expand All @@ -32,25 +38,22 @@ public function boot()

/**
* Prepare the response formats.
*
*
* @return array
*/
protected function prepareResponseFormats()
{
$formats = [];

foreach ($this->app['config']['api::formats'] as $key => $format)
{
if (is_callable($format))
{
foreach ($this->app['config']['api::formats'] as $key => $format) {
if (is_callable($format)) {
$format = call_user_func($format, $this->app);
}

$formats[$key] = $format;
}

if (empty($formats))
{
if (empty($formats)) {
throw new RuntimeException('No registered response formats.');
}

Expand All @@ -59,7 +62,7 @@ protected function prepareResponseFormats()

/**
* Register bindings for the service provider.
*
*
* @return void
*/
public function register()
Expand All @@ -68,11 +71,25 @@ public function register()
$this->registerRouter();
$this->registerTransformer();
$this->registerExceptionHandler();
$this->registerAuthentication();
$this->registerMiddlewares();

$this->app->booting(function($app)
{
$this->app->booting(function ($app) {
// grab the setting for the auth driver your using.
$shield_type = $app['config']->get('api::auth_provider');
$shield_class = $app['config']->get("api::auth_drivers.$shield_type");

$app['dingo.api.auth'] = $app->share(function ($app) use ($shield_type, $shield_class) {
$providers = [];
foreach ($app['config']['api::auth'] as $key => $provider) {
if (is_callable($provider)) {
$provider = call_user_func($provider, $app);
}
$providers[$key] = $provider;
}

return new $shield_class($app[$shield_type], $app, $providers);
});

$router = $app['router'];

$router->setExceptionHandler($app['dingo.api.exception']);
Expand All @@ -86,17 +103,15 @@ public function register()

/**
* Register and replace the bound router.
*
*
* @return void
*/
protected function registerRouter()
{
$this->app['router'] = $this->app->share(function($app)
{
$this->app['router'] = $this->app->share(function ($app) {
$router = new Router($app['events'], $app);

if ($app['env'] == 'testing')
{
if ($app['env'] == 'testing') {
$router->disableFilters();
}

Expand All @@ -106,30 +121,27 @@ protected function registerRouter()

/**
* Register the API dispatcher.
*
*
* @return void
*/
protected function registerDispatcher()
{
$this->app['dingo.api.dispatcher'] = $this->app->share(function($app)
{
$this->app['dingo.api.dispatcher'] = $this->app->share(function ($app) {
return new Dispatcher($app['request'], $app['url'], $app['router'], $app['dingo.api.auth']);
});
}

/**
* Register the API transformer.
*
*
* @return void
*/
protected function registerTransformer()
{
$this->app['dingo.api.transformer'] = $this->app->share(function($app)
{
$this->app['dingo.api.transformer'] = $this->app->share(function ($app) {
$factory = new Factory($app);

if ($app['config']->has('api::transformer'))
{
if ($app['config']->has('api::transformer')) {
$transformer = call_user_func($app['config']['api::transformer'], $app);

$factory->setTransformer($transformer);
Expand All @@ -141,45 +153,19 @@ protected function registerTransformer()

/**
* Register the exception handler.
*
*
* @return void
*/
protected function registerExceptionHandler()
{
$this->app['dingo.api.exception'] = $this->app->share(function($app)
{
$this->app['dingo.api.exception'] = $this->app->share(function ($app) {
return new ExceptionHandler;
});
}

/**
* Register the API authentication.
*
* @return void
*/
protected function registerAuthentication()
{
$this->app['dingo.api.auth'] = $this->app->share(function($app)
{
$providers = [];

foreach ($app['config']['api::auth'] as $key => $provider)
{
if (is_callable($provider))
{
$provider = call_user_func($provider, $app);
}

$providers[$key] = $provider;
}

return new Shield($app['auth'], $app, $providers);
});
}

/**
* Register the middlewares.
*
*
* @return void
*/
protected function registerMiddlewares()
Expand Down
14 changes: 7 additions & 7 deletions src/Auth/AuthorizationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

abstract class AuthorizationProvider extends Provider {
abstract class AuthorizationProvider extends Provider
{

/**
* Array of provider specific options.
*
*
* @var array
*/
protected $options = [];

/**
* Validate the requests authorization header for the provider.
*
* @param \Illuminate\Http\Request $request
*
* @param \Illuminate\Http\Request $request
* @return bool
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
public function validateAuthorizationHeader(Request $request)
{
if ( ! starts_with(strtolower($request->headers->get('authorization')), $this->getAuthorizationMethod()))
{
if (!starts_with(strtolower($request->headers->get('authorization')), $this->getAuthorizationMethod())) {
throw new BadRequestHttpException;
}
}

/**
* Get the providers authorization method.
*
*
* @return string
*/
abstract public function getAuthorizationMethod();
Expand Down
21 changes: 10 additions & 11 deletions src/Auth/BasicProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
use Illuminate\Auth\AuthManager;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class BasicProvider extends AuthorizationProvider {
class BasicProvider extends AuthorizationProvider
{

/**
* Illuminate authentication manager.
*
*
* @var \Illuminate\Auth\AuthManager
*/
protected $auth;

/**
* Basic auth identifier.
*
*
* @var string
*/
protected $identifier;

/**
* Create a new Dingo\Api\Auth\BasicProvider instance.
*
* @param \Illuminate\Auth\AuthManager $auth
*
* @param \Illuminate\Auth\AuthManager $auth
* @return void
*/
public function __construct(AuthManager $auth, $identifier = 'email')
Expand All @@ -35,17 +36,16 @@ public function __construct(AuthManager $auth, $identifier = 'email')

/**
* Authenticate request with Basic.
*
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Routing\Route $route
* @param \Illuminate\Routing\Route $route
* @return int
*/
public function authenticate(Request $request, Route $route)
{
$this->validateAuthorizationHeader($request);

if ($response = $this->auth->onceBasic($this->identifier) and $response->getStatusCode() === 401)
{
if ($response = $this->auth->onceBasic($this->identifier) and $response->getStatusCode() === 401) {
throw new UnauthorizedHttpException('Basic', 'Invalid authentication credentials.');
}

Expand All @@ -54,12 +54,11 @@ public function authenticate(Request $request, Route $route)

/**
* Get the providers authorization method.
*
*
* @return string
*/
public function getAuthorizationMethod()
{
return 'basic';
}

}
Loading