Refactored Social auth into service, Made entity an abstract class

This commit is contained in:
Dan Brown
2015-09-04 17:50:52 +01:00
parent 2dcc5105ad
commit 3d18a04c39
8 changed files with 147 additions and 77 deletions

View File

@ -2,15 +2,13 @@
namespace Oxbow\Http\Controllers\Auth;
use Oxbow\Exceptions\SocialDriverNotConfigured;
use Oxbow\Exceptions\UserNotFound;
use Oxbow\Repos\UserRepo;
use Oxbow\Services\SocialAuthService;
use Oxbow\User;
use Validator;
use Oxbow\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Laravel\Socialite\Contracts\Factory as Socialite;
class AuthController extends Controller
{
@ -31,21 +29,16 @@ class AuthController extends Controller
protected $redirectPath = '/';
protected $redirectAfterLogout = '/login';
protected $validSocialDrivers = ['google', 'github'];
protected $socialite;
protected $userRepo;
protected $socialAuthService;
/**
* Create a new authentication controller instance.
* @param Socialite $socialite
* @param UserRepo $userRepo
* @param SocialAuthService $socialAuthService
*/
public function __construct(Socialite $socialite, UserRepo $userRepo)
public function __construct(SocialAuthService $socialAuthService)
{
$this->middleware('guest', ['except' => 'getLogout']);
$this->socialite = $socialite;
$this->userRepo = $userRepo;
$this->socialAuthService = $socialAuthService;
}
/**
@ -90,7 +83,7 @@ class AuthController extends Controller
return view('auth.authenticate');
}
$socialDrivers = $this->getActiveSocialDrivers();
$socialDrivers = $this->socialAuthService->getActiveDrivers();
return view('auth.login', ['socialDrivers' => $socialDrivers]);
}
@ -102,8 +95,7 @@ class AuthController extends Controller
*/
public function getSocialLogin($socialDriver)
{
$driver = $this->validateSocialDriver($socialDriver);
return $this->socialite->driver($driver)->redirect();
return $this->socialAuthService->logIn($socialDriver);
}
/**
@ -115,61 +107,9 @@ class AuthController extends Controller
*/
public function socialCallback($socialDriver)
{
$driver = $this->validateSocialDriver($socialDriver);
// Get user details from social driver
$socialUser = $this->socialite->driver($driver)->user();
$user = $this->userRepo->getByEmail($socialUser->getEmail());
// Redirect if the email is not a current user.
if ($user === null) {
throw new UserNotFound('A user with the email ' . $socialUser->getEmail() . ' was not found.', '/login');
}
$user = $this->socialAuthService->getUserFromCallback($socialDriver);
\Auth::login($user, true);
return redirect($this->redirectPath);
}
/**
* Ensure the social driver is correct and supported.
*
* @param $socialDriver
* @return string
* @throws SocialDriverNotConfigured
*/
protected function validateSocialDriver($socialDriver)
{
$driver = trim(strtolower($socialDriver));
if (!in_array($driver, $this->validSocialDrivers)) abort(404, 'Social Driver Not Found');
if(!$this->checkSocialDriverConfigured($driver)) throw new SocialDriverNotConfigured;
return $driver;
}
/**
* Check a social driver has been configured correctly.
* @param $driver
* @return bool
*/
protected function checkSocialDriverConfigured($driver)
{
$upperName = strtoupper($driver);
$config = [env($upperName . '_APP_ID', false), env($upperName . '_APP_SECRET', false), env('APP_URL', false)];
return (!in_array(false, $config) && !in_array(null, $config));
}
/**
* Gets the names of the active social drivers.
* @return array
*/
protected function getActiveSocialDrivers()
{
$activeDrivers = [];
foreach($this->validSocialDrivers as $driverName) {
if($this->checkSocialDriverConfigured($driverName)) {
$activeDrivers[$driverName] = true;
}
}
return $activeDrivers;
}
}