Skip to content

Commit

Permalink
Refactoring auth and providing helper methods to access OAuth related…
Browse files Browse the repository at this point in the history
… details.

Signed-off-by: Jason Lewis <[email protected]>
  • Loading branch information
jasonlewis committed Apr 28, 2014
1 parent 7cb28b3 commit 4bde851
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 39 deletions.
13 changes: 12 additions & 1 deletion src/Auth/DingoOAuth2Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Dingo\OAuth2\Exception\InvalidTokenException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class DingoOAuth2Provider extends AuthorizationProvider {
class DingoOAuth2Provider extends AuthorizationProvider implements OAuth2ProviderInterface {

/**
* OAuth 2.0 resource server instance.
Expand Down Expand Up @@ -88,4 +88,15 @@ public function getAuthorizationMethod()
return 'bearer';
}

/**
* Determine if the authenticated access token has a given scope.
*
* @param string $scope
* @return bool
*/
public function hasScope($scope)
{
return $this->resource->getToken()->hasScope($scope);
}

}
13 changes: 12 additions & 1 deletion src/Auth/LeagueOAuth2Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use League\OAuth2\Server\Exception\InvalidAccessTokenException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class LeagueOAuth2Provider extends AuthorizationProvider {
class LeagueOAuth2Provider extends AuthorizationProvider implements OAuth2ProviderInterface {

/**
* OAuth 2.0 resource server instance.
Expand Down Expand Up @@ -103,4 +103,15 @@ public function getAuthorizationMethod()
return 'bearer';
}

/**
* Determine if the authenticated access token has a given scope.
*
* @param string $scope
* @return bool
*/
public function hasScope($scope)
{
return $this->resource->hasScope($scope);
}

}
13 changes: 13 additions & 0 deletions src/Auth/OAuth2ProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Dingo\Api\Auth;

interface OAuth2ProviderInterface {

/**
* Determine if the authenticated access token has a given scope.
*
* @param string $scope
* @return bool
*/
public function hasScope($scope);

}
40 changes: 14 additions & 26 deletions src/Auth/ProviderManager.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<?php namespace Dingo\Api\Auth;

use RuntimeException;
use Illuminate\Support\Manager;

class ProviderManager extends Manager {

/**
* Create Dingo's OAuth 2.0 authentication driver.
*
* @return \Dingo\Api\Auth\DingoOAuth2Provider
*/
public function createDingoOAuth2Driver()
{
return new DingoOAuth2Provider($this->app['dingo.oauth.resource']);
}

/**
* Create League's OAuth 2.0 authentication driver.
* Create OAuth 2.0 authentication driver.
*
* @return \Dingo\Api\Auth\LeagueOAuth2Provider
*/
public function createLeagueOAuth2Driver()
public function createOAuth2Driver()
{
$httpHeadersOnly = $this->app['config']->get('lucadegasperi/oauth2-server-laravel::oauth2.http_headers_only');
if ($this->app->bound('oauth2.resource-server'))
{
$httpHeadersOnly = $this->app['config']->get('lucadegasperi/oauth2-server-laravel::oauth2.http_headers_only');

return new LeagueOAuth2Provider($this->app['oauth2.resource-server'], $httpHeadersOnly);
return new LeagueOAuth2Provider($this->app['oauth2.resource-server'], $httpHeadersOnly);
}
elseif ($this->app->bound('dingo.oauth.resource'))
{
return new DingoOAuth2Provider($this->app['dingo.oauth.resource']);
}

throw new RuntimeException('Unable to resolve either OAuth 2.0 resource server binding.');
}

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

/**
* Create a new driver instance.
*
* @param string $driver
* @return mixed
* @throws \InvalidArgumentException
*/
protected function createDriver($driver)
{
return parent::createDriver(str_replace('.', '', $driver));
}

}
54 changes: 53 additions & 1 deletion src/Auth/Shield.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Dingo\Api\Auth;

use Exception;
use BadMethodCallException;
use Illuminate\Http\Request;
use Dingo\Api\Http\Response;
use Dingo\Api\Routing\Router;
Expand All @@ -25,6 +26,13 @@ class Shield {
*/
protected $providers;

/**
* The provider used for authentication.
*
* @var \Dingo\Api\Auth\Provider
*/
protected $provider;

/**
* Authenticated user ID.
*
Expand Down Expand Up @@ -68,7 +76,11 @@ public function authenticate(Request $request, Route $route)
{
try
{
return $this->userId = $provider->authenticate($request, $route);
$id = $provider->authenticate($request, $route);

$this->provider = $provider;

return $this->userId = $id;
}
catch (UnauthorizedHttpException $exception)
{
Expand Down Expand Up @@ -144,4 +156,44 @@ public function check()
return ! is_null($this->user());
}

/**
* Get the provider used for authentication.
*
* @return \Dingo\Api\Auth\Provider
*/
public function getProvider()
{
return $this->provider;
}

/**
* Determine if the provider used was an OAuth 2.0 provider.
*
* @return bool
*/
public function usedOAuth()
{
return $this->getProvider() instanceof OAuth2ProviderInterface;
}

/**
* Magically call methods on the authenticated provider used.
*
* @param string $method
* @param array $parameters
* @return mixed
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
$provider = $this->getProvider();

if (method_exists($provider, $method))
{
return call_user_func_array([$provider, $method], $parameters);
}

throw new BadMethodCallException('Method "'.$method.'" not found.');
}

}
17 changes: 8 additions & 9 deletions src/Facades/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,23 @@ public static function error(Closure $callback)
}

/**
* Get the authenticated access token.
* Get the authentication provider.
*
* @return \Dingo\OAuth2\Entity\Token
* @return \Dingo\Api\Auth\Provider
*/
public static function token()
public static function auth()
{
return static::$app['dingo.oauth.resource']->getToken();
return static::$app['dingo.api.auth'];
}

/**
* Issue an access token to the API.
* Determine if authentication was done using OAuth 2.0.
*
* @param array $payload
* @return mixed
* @return bool
*/
public static function issueToken(array $payload)
public static function usedOAuth()
{
return static::$app['dingo.oauth.authorization']->issueAccessToken($payload);
return static::$app['dingo.api.auth']->usedOAuth();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
| The authentication providers that should be used when attempting to
| authenticate an incoming API request.
|
| Available: "basic", "dingo.oauth2", "league.oauth2"
| Available: "basic", "oauth2"
|
*/

Expand Down

0 comments on commit 4bde851

Please sign in to comment.