Skip to content

Commit 4bde851

Browse files
committed
Refactoring auth and providing helper methods to access OAuth related details.
Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
1 parent 7cb28b3 commit 4bde851

File tree

7 files changed

+113
-39
lines changed

7 files changed

+113
-39
lines changed

src/Auth/DingoOAuth2Provider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Dingo\OAuth2\Exception\InvalidTokenException;
88
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
99

10-
class DingoOAuth2Provider extends AuthorizationProvider {
10+
class DingoOAuth2Provider extends AuthorizationProvider implements OAuth2ProviderInterface {
1111

1212
/**
1313
* OAuth 2.0 resource server instance.
@@ -88,4 +88,15 @@ public function getAuthorizationMethod()
8888
return 'bearer';
8989
}
9090

91+
/**
92+
* Determine if the authenticated access token has a given scope.
93+
*
94+
* @param string $scope
95+
* @return bool
96+
*/
97+
public function hasScope($scope)
98+
{
99+
return $this->resource->getToken()->hasScope($scope);
100+
}
101+
91102
}

src/Auth/LeagueOAuth2Provider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use League\OAuth2\Server\Exception\InvalidAccessTokenException;
88
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
99

10-
class LeagueOAuth2Provider extends AuthorizationProvider {
10+
class LeagueOAuth2Provider extends AuthorizationProvider implements OAuth2ProviderInterface {
1111

1212
/**
1313
* OAuth 2.0 resource server instance.
@@ -103,4 +103,15 @@ public function getAuthorizationMethod()
103103
return 'bearer';
104104
}
105105

106+
/**
107+
* Determine if the authenticated access token has a given scope.
108+
*
109+
* @param string $scope
110+
* @return bool
111+
*/
112+
public function hasScope($scope)
113+
{
114+
return $this->resource->hasScope($scope);
115+
}
116+
106117
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace Dingo\Api\Auth;
2+
3+
interface OAuth2ProviderInterface {
4+
5+
/**
6+
* Determine if the authenticated access token has a given scope.
7+
*
8+
* @param string $scope
9+
* @return bool
10+
*/
11+
public function hasScope($scope);
12+
13+
}

src/Auth/ProviderManager.php

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

3+
use RuntimeException;
34
use Illuminate\Support\Manager;
45

56
class ProviderManager extends Manager {
67

78
/**
8-
* Create Dingo's OAuth 2.0 authentication driver.
9-
*
10-
* @return \Dingo\Api\Auth\DingoOAuth2Provider
11-
*/
12-
public function createDingoOAuth2Driver()
13-
{
14-
return new DingoOAuth2Provider($this->app['dingo.oauth.resource']);
15-
}
16-
17-
/**
18-
* Create League's OAuth 2.0 authentication driver.
9+
* Create OAuth 2.0 authentication driver.
1910
*
2011
* @return \Dingo\Api\Auth\LeagueOAuth2Provider
2112
*/
22-
public function createLeagueOAuth2Driver()
13+
public function createOAuth2Driver()
2314
{
24-
$httpHeadersOnly = $this->app['config']->get('lucadegasperi/oauth2-server-laravel::oauth2.http_headers_only');
15+
if ($this->app->bound('oauth2.resource-server'))
16+
{
17+
$httpHeadersOnly = $this->app['config']->get('lucadegasperi/oauth2-server-laravel::oauth2.http_headers_only');
2518

26-
return new LeagueOAuth2Provider($this->app['oauth2.resource-server'], $httpHeadersOnly);
19+
return new LeagueOAuth2Provider($this->app['oauth2.resource-server'], $httpHeadersOnly);
20+
}
21+
elseif ($this->app->bound('dingo.oauth.resource'))
22+
{
23+
return new DingoOAuth2Provider($this->app['dingo.oauth.resource']);
24+
}
25+
26+
throw new RuntimeException('Unable to resolve either OAuth 2.0 resource server binding.');
2727
}
2828

2929
/**
@@ -36,16 +36,4 @@ public function createBasicDriver()
3636
return new BasicProvider($this->app['auth']);
3737
}
3838

39-
/**
40-
* Create a new driver instance.
41-
*
42-
* @param string $driver
43-
* @return mixed
44-
* @throws \InvalidArgumentException
45-
*/
46-
protected function createDriver($driver)
47-
{
48-
return parent::createDriver(str_replace('.', '', $driver));
49-
}
50-
5139
}

src/Auth/Shield.php

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

33
use Exception;
4+
use BadMethodCallException;
45
use Illuminate\Http\Request;
56
use Dingo\Api\Http\Response;
67
use Dingo\Api\Routing\Router;
@@ -25,6 +26,13 @@ class Shield {
2526
*/
2627
protected $providers;
2728

29+
/**
30+
* The provider used for authentication.
31+
*
32+
* @var \Dingo\Api\Auth\Provider
33+
*/
34+
protected $provider;
35+
2836
/**
2937
* Authenticated user ID.
3038
*
@@ -68,7 +76,11 @@ public function authenticate(Request $request, Route $route)
6876
{
6977
try
7078
{
71-
return $this->userId = $provider->authenticate($request, $route);
79+
$id = $provider->authenticate($request, $route);
80+
81+
$this->provider = $provider;
82+
83+
return $this->userId = $id;
7284
}
7385
catch (UnauthorizedHttpException $exception)
7486
{
@@ -144,4 +156,44 @@ public function check()
144156
return ! is_null($this->user());
145157
}
146158

159+
/**
160+
* Get the provider used for authentication.
161+
*
162+
* @return \Dingo\Api\Auth\Provider
163+
*/
164+
public function getProvider()
165+
{
166+
return $this->provider;
167+
}
168+
169+
/**
170+
* Determine if the provider used was an OAuth 2.0 provider.
171+
*
172+
* @return bool
173+
*/
174+
public function usedOAuth()
175+
{
176+
return $this->getProvider() instanceof OAuth2ProviderInterface;
177+
}
178+
179+
/**
180+
* Magically call methods on the authenticated provider used.
181+
*
182+
* @param string $method
183+
* @param array $parameters
184+
* @return mixed
185+
* @throws \BadMethodCallException
186+
*/
187+
public function __call($method, $parameters)
188+
{
189+
$provider = $this->getProvider();
190+
191+
if (method_exists($provider, $method))
192+
{
193+
return call_user_func_array([$provider, $method], $parameters);
194+
}
195+
196+
throw new BadMethodCallException('Method "'.$method.'" not found.');
197+
}
198+
147199
}

src/Facades/API.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,23 @@ public static function error(Closure $callback)
2828
}
2929

3030
/**
31-
* Get the authenticated access token.
31+
* Get the authentication provider.
3232
*
33-
* @return \Dingo\OAuth2\Entity\Token
33+
* @return \Dingo\Api\Auth\Provider
3434
*/
35-
public static function token()
35+
public static function auth()
3636
{
37-
return static::$app['dingo.oauth.resource']->getToken();
37+
return static::$app['dingo.api.auth'];
3838
}
3939

4040
/**
41-
* Issue an access token to the API.
41+
* Determine if authentication was done using OAuth 2.0.
4242
*
43-
* @param array $payload
44-
* @return mixed
43+
* @return bool
4544
*/
46-
public static function issueToken(array $payload)
45+
public static function usedOAuth()
4746
{
48-
return static::$app['dingo.oauth.authorization']->issueAccessToken($payload);
47+
return static::$app['dingo.api.auth']->usedOAuth();
4948
}
5049

5150
/**

src/config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
| The authentication providers that should be used when attempting to
6060
| authenticate an incoming API request.
6161
|
62-
| Available: "basic", "dingo.oauth2", "league.oauth2"
62+
| Available: "basic", "oauth2"
6363
|
6464
*/
6565

0 commit comments

Comments
 (0)