Skip to content

Commit 9a7afd9

Browse files
committed
Refactoring authentication to make use of a manager.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 7d35539 commit 9a7afd9

14 files changed

+174
-202
lines changed

src/ApiServiceProvider.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
use Closure;
44
use Dingo\Api\Http\Response;
55
use Dingo\Api\Routing\Router;
6-
use Dingo\Api\Auth\AuthManager;
7-
use Dingo\Api\Auth\BasicProvider;
8-
use Dingo\Api\Auth\OAuth2Provider;
6+
use Dingo\Api\Auth\ProviderManager;
97
use Illuminate\Support\ServiceProvider;
108
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
119

@@ -29,7 +27,7 @@ public function boot()
2927

3028
$this->app['Dingo\Api\Authentication'] = function($app)
3129
{
32-
return $app['dingo.api.authentication'];
30+
return $app['dingo.api.auth'];
3331
};
3432

3533
// Set the static formatters on the response class so that requested formats
@@ -40,7 +38,7 @@ public function boot()
4038

4139
$this->app['router']->filter('api', function($route, $request)
4240
{
43-
$this->app['dingo.api.authentication']->authenticate();
41+
$this->app['dingo.api.auth']->authenticate();
4442
});
4543
}
4644

@@ -104,7 +102,7 @@ protected function registerDispatcher()
104102
{
105103
$this->app['dingo.api.dispatcher'] = $this->app->share(function($app)
106104
{
107-
return new Dispatcher($app['request'], $app['url'], $app['router'], $app['dingo.api.authentication']);
105+
return new Dispatcher($app['request'], $app['url'], $app['router'], $app['dingo.api.auth']);
108106
});
109107
}
110108

@@ -128,14 +126,14 @@ protected function registerExceptionHandler()
128126
*/
129127
protected function registerAuthentication()
130128
{
131-
$this->app['dingo.api.authentication'] = $this->app->share(function($app)
129+
$this->app['dingo.api.auth.manager'] = $this->app->share(function($app)
132130
{
133-
$providers = [];
131+
return new ProviderManager($app);
132+
});
134133

135-
$resolvers = [
136-
'basic' => function($app, $options) { return new BasicProvider($app['auth'], $options); },
137-
'oauth2' => function($app, $options) { return new OAuth2Provider($app['dingo.oauth.resource'], $options); }
138-
];
134+
$this->app['dingo.api.auth'] = $this->app->share(function($app)
135+
{
136+
$providers = [];
139137

140138
foreach ($app['config']['api::auth'] as $key => $value)
141139
{
@@ -151,7 +149,7 @@ protected function registerAuthentication()
151149
$options = call_user_func($options, $app);
152150
}
153151

154-
$providers[$provider] = $resolvers[$provider]($app, $options);
152+
$providers[$provider] = $app['dingo.api.auth.manager']->driver($provider)->setOptions($options);
155153
}
156154

157155
return new Authentication($app['router'], $app['auth'], $providers);

src/Auth/AuthorizationProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use Exception;
44
use Illuminate\Http\Request;
55

6-
abstract class AuthorizationProvider implements ProviderInterface {
6+
abstract class AuthorizationProvider extends Provider {
77

88
/**
9-
* Array of provider speicifc options.
9+
* Array of provider specific options.
1010
*
1111
* @var array
1212
*/
@@ -33,4 +33,4 @@ public function validateAuthorizationHeader(Request $request)
3333
*/
3434
abstract public function getAuthorizationMethod();
3535

36-
}
36+
}

src/Auth/BasicProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Dingo\Api\Auth;
22

33
use Illuminate\Http\Request;
4+
use Illuminate\Routing\Route;
45
use Illuminate\Auth\AuthManager;
56
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
67

@@ -24,22 +25,21 @@ class BasicProvider extends AuthorizationProvider {
2425
* Create a new Dingo\Api\Auth\BasicProvider instance.
2526
*
2627
* @param \Illuminate\Auth\AuthManager $auth
27-
* @param array $options
2828
* @return void
2929
*/
30-
public function __construct(AuthManager $auth, array $options)
30+
public function __construct(AuthManager $auth)
3131
{
3232
$this->auth = $auth;
33-
$this->options = array_merge($this->options, $options);
3433
}
3534

3635
/**
3736
* Authenticate request with Basic.
3837
*
3938
* @param \Illuminate\Http\Request $request
39+
* @param \Illuminate\Routing\Route $route
4040
* @return int
4141
*/
42-
public function authenticate(Request $request)
42+
public function authenticate(Request $request, Route $route)
4343
{
4444
$this->validateAuthorizationHeader($request);
4545

@@ -61,4 +61,4 @@ public function getAuthorizationMethod()
6161
return 'basic';
6262
}
6363

64-
}
64+
}

src/Auth/OAuth2Provider.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Exception;
44
use Illuminate\Http\Request;
5+
use Illuminate\Routing\Route;
56
use Dingo\OAuth2\Server\Resource;
67
use Dingo\OAuth2\Exception\InvalidTokenException;
78
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
@@ -15,33 +16,25 @@ class OAuth2Provider extends AuthorizationProvider {
1516
*/
1617
protected $resource;
1718

18-
/**
19-
* Array of request scopes.
20-
*
21-
* @var array
22-
*/
23-
protected $scopes = [];
24-
2519
/**
2620
* Create a new Dingo\Api\Auth\OAuth2Provider instance.
2721
*
2822
* @param \Dingo\OAuth2\Server\Resource $resource
29-
* @param array $options
3023
* @return void
3124
*/
32-
public function __construct(Resource $resource, array $options)
25+
public function __construct(Resource $resource)
3326
{
3427
$this->resource = $resource;
35-
$this->options = $options;
3628
}
3729

3830
/**
39-
* Authenticate request with OAuth2.
31+
* Authenticate request with the OAuth 2.0 resource server.
4032
*
4133
* @param \Illuminate\Http\Request $request
34+
* @param \Illuminate\Routing\Route $route
4235
* @return int
4336
*/
44-
public function authenticate(Request $request)
37+
public function authenticate(Request $request, Route $route)
4538
{
4639
try
4740
{
@@ -58,9 +51,11 @@ public function authenticate(Request $request)
5851
}
5952
}
6053

54+
$scopes = $this->getRouteScopes($route);
55+
6156
try
6257
{
63-
$token = $this->resource->validateRequest($this->scopes);
58+
$token = $this->resource->validateRequest($scopes);
6459

6560
return $token->getUserId();
6661
}
@@ -70,6 +65,19 @@ public function authenticate(Request $request)
7065
}
7166
}
7267

68+
/**
69+
* Get the routes scopes.
70+
*
71+
* @param \Illuminate\Routing\Route $route
72+
* @return array
73+
*/
74+
protected function getRouteScopes(Route $route)
75+
{
76+
$action = $route->getAction();
77+
78+
return isset($action['scopes']) ? (array) $action['scopes'] : [];
79+
}
80+
7381
/**
7482
* Get the providers authorization method.
7583
*

src/Auth/Provider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php namespace Dingo\Api\Auth;
2+
3+
use Illuminate\Http\Request;
4+
use Illuminate\Routing\Route;
5+
6+
abstract class Provider {
7+
8+
/**
9+
* Array of provider specific options.
10+
*
11+
* @var array
12+
*/
13+
protected $options = [];
14+
15+
/**
16+
* Set the provider specific options.
17+
*
18+
* @param array $options
19+
* @return \Dingo\Api\Auth\Provider
20+
*/
21+
public function setOptions(array $options)
22+
{
23+
$this->options = array_merge($this->options, $options);
24+
25+
return $this;
26+
}
27+
28+
/**
29+
* Authenticate the request and return the authenticated users ID.
30+
*
31+
* @param \Illuminate\Http\Request $request
32+
* @param \Illuminate\Routing\Route $route
33+
* @return int
34+
*/
35+
abstract public function authenticate(Request $request, Route $route);
36+
37+
}

src/Auth/ProviderInterface.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Auth/ProviderManager.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace Dingo\Api\Auth;
2+
3+
use Illuminate\Support\Manager;
4+
5+
class ProviderManager extends Manager {
6+
7+
/**
8+
* Create OAuth 2.0 provider.
9+
*
10+
* @return \Dingo\Api\Auth\BasicProvider
11+
*/
12+
public function createOAuth2Driver()
13+
{
14+
return new OAuth2Provider($this->app['dingo.oauth.resource']);
15+
}
16+
17+
/**
18+
* Create basic provider.
19+
*
20+
* @return \Dingo\Api\Auth\BasicProvider
21+
*/
22+
public function createBasicDriver()
23+
{
24+
return new BasicProvider($this->app['auth']);
25+
}
26+
27+
}

src/Authentication.php

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Illuminate\Routing\Route;
77
use Illuminate\Auth\AuthManager;
88
use Dingo\Api\Http\InternalRequest;
9-
use Dingo\Api\Auth\ProviderInterface;
10-
use Dingo\Api\Auth\AuthorizationProvider;
119
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
1210

1311
class Authentication {
@@ -83,15 +81,14 @@ public function authenticate()
8381

8482
$exceptionStack = [];
8583

86-
$this->registerOAuth2Scopes($route);
87-
8884
// Spin through each of the registered authentication providers and attempt to
89-
// authenticate through one of them.
85+
// authenticate through one of them. This allows a developer to implement
86+
// and allow a number of different authentication mechanisms.
9087
foreach ($this->providers as $provider)
9188
{
9289
try
9390
{
94-
return $this->userId = $provider->authenticate($request);
91+
return $this->userId = $provider->authenticate($request, $route);
9592
}
9693
catch (UnauthorizedHttpException $exception)
9794
{
@@ -115,27 +112,6 @@ public function authenticate()
115112
throw $exception;
116113
}
117114

118-
/**
119-
* Register the OAuth 2.0 scopes on the "oauth2" provider.
120-
*
121-
* @param \Illuminate\Routing\Route $route
122-
* @return void
123-
*/
124-
protected function registerOAuth2Scopes(Route $route)
125-
{
126-
// If authenticating via OAuth2 a route can be protected by defining its scopes.
127-
// We'll grab the scopes for this route and pass them through to the
128-
// authentication providers.
129-
if (isset($this->providers['oauth2']))
130-
{
131-
$action = $route->getAction();
132-
133-
$scopes = isset($action['scopes']) ? (array) $action['scopes'] : [];
134-
135-
$this->providers['oauth2']->setScopes($scopes);
136-
}
137-
}
138-
139115
/**
140116
* Determine if a route is protected.
141117
*
@@ -192,18 +168,4 @@ public function setUser($user)
192168
return $this;
193169
}
194170

195-
/**
196-
* Extend the authentication layer by registering a custom provider.
197-
*
198-
* @param string $key
199-
* @param \Dingo\Api\Auth\ProviderInterface $provider
200-
* @return \Dingo\Api\Authentication
201-
*/
202-
public function extend($key, ProviderInterface $provider)
203-
{
204-
$this->providers[$key] = $provider;
205-
206-
return $this;
207-
}
208-
209171
}

src/Facades/API.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function issueToken(array $payload)
5555
*/
5656
public static function user()
5757
{
58-
return static::$app['dingo.api.authentication']->getUser();
58+
return static::$app['dingo.api.auth']->getUser();
5959
}
6060

6161
/**
@@ -68,4 +68,4 @@ public static function internal()
6868
return static::$app['router']->getCurrentRequest() instanceof InternalRequest;
6969
}
7070

71-
}
71+
}

0 commit comments

Comments
 (0)