Skip to content

Commit

Permalink
Extract Authorizer and ProviderRegistrar
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Dec 27, 2014
1 parent f33030b commit ff01cb4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
27 changes: 27 additions & 0 deletions src/AdamWathan/EloquentOAuth/Authorizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace AdamWathan\EloquentOAuth;

use Illuminate\Routing\Redirector as Redirect;
use AdamWathan\EloquentOAuth\Providers\ProviderInterface;

class Authorizer
{
protected $redirect;
protected $stateManager;

public function __construct(Redirect $redirect, StateManager $stateManager)
{
$this->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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
32 changes: 9 additions & 23 deletions src/AdamWathan/EloquentOAuth/OAuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,37 @@

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;
}

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)
Expand All @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions src/AdamWathan/EloquentOAuth/ProviderRegistrar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace AdamWathan\EloquentOAuth;

use AdamWathan\EloquentOAuth\Providers\ProviderInterface;
use AdamWathan\EloquentOAuth\Exceptions\ProviderNotRegisteredException;

class ProviderRegistrar
{
private $providers = array();

public function registerProvider($alias, ProviderInterface $provider)
{
$this->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]);
}
}

0 comments on commit ff01cb4

Please sign in to comment.