Skip to content

Commit

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

use Closure;
use Illuminate\Auth\AuthManager as Auth;
use AdamWathan\EloquentOAuth\Exceptions\ProviderNotRegisteredException;
use AdamWathan\EloquentOAuth\Exceptions\InvalidAuthorizationCodeException;
use AdamWathan\EloquentOAuth\Providers\ProviderInterface;

class Authenticator
{
protected $auth;
protected $stateManager;
protected $users;
protected $identities;

public function __construct(Auth $auth, StateManager $stateManager, UserStore $users, IdentityStore $identities)
{
$this->auth = $auth;
$this->stateManager = $stateManager;
$this->users = $users;
$this->identities = $identities;
}

public function login($providerAlias, $provider, Closure $callback = null)
{
$this->verifyState();
$details = $provider->getUserDetails();
$user = $this->getUser($providerAlias, $details);
if ($callback) {
$callback($user, $details);
}
$this->updateUser($user, $provider, $details);
$this->auth->login($user);

This comment has been minimized.

Copy link
@KlitnyjS

KlitnyjS Mar 14, 2015

Why not $this->auth->login($user, true); ?

This comment has been minimized.

Copy link
@adamwathan

adamwathan Mar 14, 2015

Author Owner

I have thought about this a bit but ultimately opted to not do "remember me" stuff by default at least. The user's session will remain open as long as the browser is open and logging back in is just a single click once they've authorized the app.

I may add it as an optional parameter to OAuth::login() though 👍

return $user;
}

protected function verifyState()
{
if (! $this->stateManager->verifyState()) {
throw new InvalidAuthorizationCodeException;
}
}

protected function getUser($provider, $details)
{
if ($this->userExists($provider, $details)) {
$user = $this->getExistingUser($provider, $details);
} else {
$user = $this->createUser();
}
return $user;
}

protected function updateUser($user, $provider, $details)
{
$this->users->store($user);
$this->updateAccessToken($user, $provider, $details);
}

protected function userExists($provider, ProviderUserDetails $details)
{
return $this->identities->userExists($provider, $details);
}

protected function getExistingUser($provider, $details)
{
$identity = $this->getIdentity($provider, $details);
return $this->users->findByIdentity($identity);
}

protected function getIdentity($provider, ProviderUserDetails $details)
{
return $this->identities->getByProvider($provider, $details);
}

protected function createUser()
{
$user = $this->users->create();
return $user;
}

protected function updateAccessToken($user, $provider, ProviderUserDetails $details)
{
$this->flushAccessTokens($user, $provider);
$this->addAccessToken($user, $provider, $details);
}

protected function flushAccessTokens($user, $provider)
{
$this->identities->flush($user, $provider);
}

protected function addAccessToken($user, $provider, ProviderUserDetails $details)
{
$identity = new OAuthIdentity;
$identity->user_id = $user->getKey();
$identity->provider = $provider;
$identity->provider_user_id = $details->userId;
$identity->access_token = $details->accessToken;
$this->identities->store($identity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ protected function registerOAuthManager()
$users = new UserStore($app['config']['auth.model']);
$stateManager = new StateManager($app['session.store'], $app['request']);
$authorizer = new Authorizer($app['redirect'], $stateManager);
$oauth = new OAuthManager($authorizer, new ProviderRegistrar, $app['auth'], $stateManager, $users, new IdentityStore);
$authenticator = new Authenticator($app['auth'], $stateManager, $users, new IdentityStore);
$oauth = new OAuthManager($authorizer, new ProviderRegistrar, $authenticator);
$this->registerProviders($oauth);
return $oauth;
});
Expand Down
95 changes: 5 additions & 90 deletions src/AdamWathan/EloquentOAuth/OAuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ class OAuthManager
{
protected $authorizer;
protected $providers;
protected $auth;
protected $stateManager;
protected $users;
protected $identities;
protected $authenticator;

public function __construct(Authorizer $authorizer, ProviderRegistrar $providers, Auth $auth, StateManager $stateManager, UserStore $users, IdentityStore $identities)
public function __construct(Authorizer $authorizer, ProviderRegistrar $providers, Authenticator $authenticator)
{
$this->authorizer = $authorizer;
$this->providers = $providers;
$this->auth = $auth;
$this->stateManager = $stateManager;
$this->users = $users;
$this->identities = $identities;
$this->authenticator = $authenticator;
}

public function registerProvider($alias, ProviderInterface $provider)
Expand All @@ -35,92 +29,13 @@ public function authorize($providerAlias)
return $this->authorizer->authorize($this->getProvider($providerAlias));
}

public function login($provider, Closure $callback = null)
public function login($providerAlias, Closure $callback = null)
{
$this->verifyState();
$details = $this->getUserDetails($provider);
$user = $this->getUser($provider, $details);
if ($callback) {
$callback($user, $details);
}
$this->updateUser($user, $provider, $details);
$this->auth->login($user);
return $user;
return $this->authenticator->login($providerAlias, $this->getProvider($providerAlias), $callback);
}

protected function getProvider($providerAlias)
{
return $this->providers->getProvider($providerAlias);
}

protected function verifyState()
{
if (! $this->stateManager->verifyState()) {
throw new InvalidAuthorizationCodeException;
}
}

protected function getUserDetails($provider)
{
return $this->getProvider($provider)->getUserDetails();
}

protected function getUser($provider, $details)
{
if ($this->userExists($provider, $details)) {
$user = $this->getExistingUser($provider, $details);
} else {
$user = $this->createUser();
}
return $user;
}

protected function updateUser($user, $provider, $details)
{
$this->users->store($user);
$this->updateAccessToken($user, $provider, $details);
}

protected function userExists($provider, ProviderUserDetails $details)
{
return $this->identities->userExists($provider, $details);
}

protected function getExistingUser($provider, $details)
{
$identity = $this->getIdentity($provider, $details);
return $this->users->findByIdentity($identity);
}

protected function getIdentity($provider, ProviderUserDetails $details)
{
return $this->identities->getByProvider($provider, $details);
}

protected function createUser()
{
$user = $this->users->create();
return $user;
}

protected function updateAccessToken($user, $provider, ProviderUserDetails $details)
{
$this->flushAccessTokens($user, $provider);
$this->addAccessToken($user, $provider, $details);
}

protected function flushAccessTokens($user, $provider)
{
$this->identities->flush($user, $provider);
}

protected function addAccessToken($user, $provider, ProviderUserDetails $details)
{
$identity = new OAuthIdentity;
$identity->user_id = $user->getKey();
$identity->provider = $provider;
$identity->provider_user_id = $details->userId;
$identity->access_token = $details->accessToken;
$this->identities->store($identity);
}
}

0 comments on commit 383c0f4

Please sign in to comment.