diff --git a/src/AdamWathan/EloquentOAuth/Authorizer.php b/src/AdamWathan/EloquentOAuth/Authorizer.php new file mode 100644 index 0000000..9dfe235 --- /dev/null +++ b/src/AdamWathan/EloquentOAuth/Authorizer.php @@ -0,0 +1,27 @@ +redirect = $redirect; + $this->stateManager = $stateManager; + } + + public function authorize(ProviderInterface $provider) + { + $state = $this->generateState(); + return $this->redirect->to($provider->authorizeUrl($state)); + } + + protected function generateState() + { + return $this->stateManager->generateState(); + } +} diff --git a/src/AdamWathan/EloquentOAuth/EloquentOAuthServiceProvider.php b/src/AdamWathan/EloquentOAuth/EloquentOAuthServiceProvider.php index 5214b47..513132a 100644 --- a/src/AdamWathan/EloquentOAuth/EloquentOAuthServiceProvider.php +++ b/src/AdamWathan/EloquentOAuth/EloquentOAuthServiceProvider.php @@ -47,7 +47,8 @@ protected function registerOAuthManager() $this->configureOAuthIdentitiesTable(); $users = new UserStore($app['config']['auth.model']); $stateManager = new StateManager($app['session.store'], $app['request']); - $oauth = new OAuthManager($app['auth'], $app['redirect'], $stateManager, $users, new IdentityStore); + $authorizer = new Authorizer($app['redirect'], $stateManager); + $oauth = new OAuthManager($authorizer, new ProviderRegistrar, $app['auth'], $stateManager, $users, new IdentityStore); $this->registerProviders($oauth); return $oauth; }); diff --git a/src/AdamWathan/EloquentOAuth/OAuthManager.php b/src/AdamWathan/EloquentOAuth/OAuthManager.php index a118b74..5d88a55 100644 --- a/src/AdamWathan/EloquentOAuth/OAuthManager.php +++ b/src/AdamWathan/EloquentOAuth/OAuthManager.php @@ -2,24 +2,24 @@ use Closure; use Illuminate\Auth\AuthManager as Auth; -use Illuminate\Routing\Redirector as Redirect; use AdamWathan\EloquentOAuth\Exceptions\ProviderNotRegisteredException; use AdamWathan\EloquentOAuth\Exceptions\InvalidAuthorizationCodeException; use AdamWathan\EloquentOAuth\Providers\ProviderInterface; class OAuthManager { + protected $authorizer; + protected $providers; protected $auth; - protected $redirect; protected $stateManager; protected $users; protected $identities; - protected $providers = array(); - public function __construct(Auth $auth, Redirect $redirect, StateManager $stateManager, UserStore $users, IdentityStore $identities) + public function __construct(Authorizer $authorizer, ProviderRegistrar $providers, Auth $auth, StateManager $stateManager, UserStore $users, IdentityStore $identities) { + $this->authorizer = $authorizer; + $this->providers = $providers; $this->auth = $auth; - $this->redirect = $redirect; $this->stateManager = $stateManager; $this->users = $users; $this->identities = $identities; @@ -27,13 +27,12 @@ public function __construct(Auth $auth, Redirect $redirect, StateManager $stateM public function registerProvider($alias, ProviderInterface $provider) { - $this->providers[$alias] = $provider; + $this->providers->registerProvider($alias, $provider); } - public function authorize($provider) + public function authorize($providerAlias) { - $state = $this->generateState(); - return $this->redirect->to($this->getProvider($provider)->authorizeUrl($state)); + return $this->authorizer->authorize($this->getProvider($providerAlias)); } public function login($provider, Closure $callback = null) @@ -49,22 +48,9 @@ public function login($provider, Closure $callback = null) return $user; } - protected function generateState() - { - return $this->stateManager->generateState(); - } - protected function getProvider($providerAlias) { - if (! $this->hasProvider($providerAlias)) { - throw new ProviderNotRegisteredException("No provider has been registered under the alias '{$providerAlias}'"); - } - return $this->providers[$providerAlias]; - } - - protected function hasProvider($alias) - { - return isset($this->providers[$alias]); + return $this->providers->getProvider($providerAlias); } protected function verifyState() diff --git a/src/AdamWathan/EloquentOAuth/ProviderRegistrar.php b/src/AdamWathan/EloquentOAuth/ProviderRegistrar.php new file mode 100644 index 0000000..00350c9 --- /dev/null +++ b/src/AdamWathan/EloquentOAuth/ProviderRegistrar.php @@ -0,0 +1,27 @@ +providers[$alias] = $provider; + } + + public function getProvider($providerAlias) + { + if (! $this->hasProvider($providerAlias)) { + throw new ProviderNotRegisteredException("No provider has been registered under the alias '{$providerAlias}'"); + } + return $this->providers[$providerAlias]; + } + + protected function hasProvider($alias) + { + return isset($this->providers[$alias]); + } +}