From 75e5843ac7aafd250697e116029443488ec4c36b Mon Sep 17 00:00:00 2001 From: Yehoshua Talansky Date: Thu, 2 Nov 2017 14:37:20 -0400 Subject: [PATCH 1/3] allow dynamic redirect urls --- config/oauth2.php | 14 ++++++++ src/Storage/FluentClient.php | 36 +++++++++++++++++--- src/Storage/FluentStorageServiceProvider.php | 3 +- 3 files changed, 48 insertions(+), 5 deletions(-) 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..96554afb 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; From 28f4a50522a17f5bbc100b2bd9945aff33bc2eb0 Mon Sep 17 00:00:00 2001 From: Yehoshua Talansky Date: Thu, 2 Nov 2017 14:44:32 -0400 Subject: [PATCH 2/3] fixed style --- src/Storage/FluentClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/FluentClient.php b/src/Storage/FluentClient.php index 96554afb..81542e71 100644 --- a/src/Storage/FluentClient.php +++ b/src/Storage/FluentClient.php @@ -144,7 +144,7 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant //populate redirect_uri in case where it is not in DB if(!$this->limitRedirectUri){ - $result->redirect_uri = "not required"; + $result->redirect_uri = 'not required'; } From 184984836b4359f28d16a9ad08159cb8bf3a32ad Mon Sep 17 00:00:00 2001 From: shua Talansky Date: Thu, 2 Nov 2017 17:16:37 -0400 Subject: [PATCH 3/3] Update FluentClient.php add space by if statement --- src/Storage/FluentClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/FluentClient.php b/src/Storage/FluentClient.php index 81542e71..617301bc 100644 --- a/src/Storage/FluentClient.php +++ b/src/Storage/FluentClient.php @@ -143,7 +143,7 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant } //populate redirect_uri in case where it is not in DB - if(!$this->limitRedirectUri){ + if (!$this->limitRedirectUri){ $result->redirect_uri = 'not required'; }