diff --git a/config/oauth2.php b/config/oauth2.php index cd11554a..fec72ae5 100755 --- a/config/oauth2.php +++ b/config/oauth2.php @@ -25,6 +25,20 @@ | Please see this link to find available grant types | http://git.io/vJLAv | + | This is an example that supports return type code and refresh tokens + | 'grant_types' => [ + | 'authorization_code' => [ + | 'class' => '\League\OAuth2\Server\Grant\AuthCodeGrant', + | 'access_token_ttl' => 36000, + | 'auth_token_ttl' => 36000 + | ], + | 'refresh_token' => [ + | 'class' => '\League\OAuth2\Server\Grant\RefreshTokenGrant', + | 'access_token_ttl' => 36000, + | 'refresh_token_ttl' => 360000000 + | ] + |], + | */ 'grant_types' => [ diff --git a/src/Storage/FluentClient.php b/src/Storage/FluentClient.php index 32df5127..617301bc 100644 --- a/src/Storage/FluentClient.php +++ b/src/Storage/FluentClient.php @@ -30,17 +30,24 @@ class FluentClient extends AbstractFluentAdapter implements ClientInterface * @var bool */ protected $limitClientsToGrants = false; - + /** + * Pre-register redirect url is required. + * + * @var bool + */ + protected $limitRedirectUri = false; /** * Create a new fluent client instance. * * @param \Illuminate\Database\ConnectionResolverInterface $resolver * @param bool $limitClientsToGrants + * @param bool $limitRedirectUri */ - public function __construct(Resolver $resolver, $limitClientsToGrants = false) + public function __construct(Resolver $resolver, $limitClientsToGrants = false, $limitRedirectUri = false) { parent::__construct($resolver); $this->limitClientsToGrants = $limitClientsToGrants; + $this->limitRedirectUri = $limitRedirectUri; } /** @@ -77,7 +84,7 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant { $query = null; - if (!is_null($redirectUri) && is_null($clientSecret)) { + if (!$this->limitRedirectUri === false && !is_null($redirectUri) && is_null($clientSecret)) { $query = $this->getConnection()->table('oauth_clients') ->select( 'oauth_clients.id as id', @@ -87,6 +94,13 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant ->join('oauth_client_endpoints', 'oauth_clients.id', '=', 'oauth_client_endpoints.client_id') ->where('oauth_clients.id', $clientId) ->where('oauth_client_endpoints.redirect_uri', $redirectUri); + } elseif (!is_null($redirectUri) && is_null($clientSecret)) { + $query = $this->getConnection()->table('oauth_clients') + ->select( + 'oauth_clients.id as id', + 'oauth_clients.secret as secret', + 'oauth_clients.name as name') + ->where('oauth_clients.id', $clientId); } elseif (!is_null($clientSecret) && is_null($redirectUri)) { $query = $this->getConnection()->table('oauth_clients') ->select( @@ -95,7 +109,7 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant 'oauth_clients.name as name') ->where('oauth_clients.id', $clientId) ->where('oauth_clients.secret', $clientSecret); - } elseif (!is_null($clientSecret) && !is_null($redirectUri)) { + } elseif ($this->limitRedirectUri === true && !is_null($clientSecret) && !is_null($redirectUri)) { $query = $this->getConnection()->table('oauth_clients') ->select( 'oauth_clients.id as id', @@ -106,6 +120,14 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant ->where('oauth_clients.id', $clientId) ->where('oauth_clients.secret', $clientSecret) ->where('oauth_client_endpoints.redirect_uri', $redirectUri); + } elseif (!is_null($clientSecret) && !is_null($redirectUri)) { + $query = $this->getConnection()->table('oauth_clients') + ->select( + 'oauth_clients.id as id', + 'oauth_clients.secret as secret', + 'oauth_clients.name as name') + ->where('oauth_clients.id', $clientId) + ->where('oauth_clients.secret', $clientSecret); } if ($this->limitClientsToGrants === true && !is_null($grantType)) { @@ -120,6 +142,12 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant return; } + //populate redirect_uri in case where it is not in DB + if (!$this->limitRedirectUri){ + $result->redirect_uri = 'not required'; + } + + return $this->hydrateEntity($result); } diff --git a/src/Storage/FluentStorageServiceProvider.php b/src/Storage/FluentStorageServiceProvider.php index 43968d98..c4668124 100644 --- a/src/Storage/FluentStorageServiceProvider.php +++ b/src/Storage/FluentStorageServiceProvider.php @@ -75,7 +75,8 @@ public function registerStorageBindings(Application $app) $app->singleton(FluentClient::class, function ($app) use ($provider) { $limitClientsToGrants = $app['config']->get('oauth2.limit_clients_to_grants'); - $storage = new FluentClient($provider->app['db'], $limitClientsToGrants); + $limitRedirectUri = $app['config']->get('oauth2.limit_clients_to_predefined_url'); + $storage = new FluentClient($provider->app['db'], $limitClientsToGrants, $limitRedirectUri); $storage->setConnectionName($provider->getConnectionName()); return $storage;