fixing up the authenticator to allow identiy chaining

This commit is contained in:
2018-07-10 21:55:47 +02:00
parent 598f2f8024
commit d74b76da3e
41 changed files with 513 additions and 346 deletions
-76
View File
@@ -1,76 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth;
class Authenticator
{
protected $auth;
protected $users;
protected $identities;
public function __construct($auth, $users, $identities)
{
$this->auth = $auth;
$this->users = $users;
$this->identities = $identities;
}
public function login($providerAlias, $userDetails, $callback = null, $remember = false)
{
$user = $this->getUser($providerAlias, $userDetails);
$user = $this->runCallback($callback, $user, $userDetails);
$this->updateUser($user, $providerAlias, $userDetails);
$this->auth->login($user, $remember);
}
protected function getUser($providerAlias, $details)
{
if ($this->identities->userExists($providerAlias, $details)) {
return $this->getExistingUser($providerAlias, $details);
}
return $this->users->create();
}
protected function runCallback($callback, $user, $userDetails)
{
$callback = $callback ?: function () {};
$callbackUser = $callback($user, $userDetails);
return $callbackUser ?: $user;
}
protected function updateUser($user, $providerAlias, $details)
{
$this->users->store($user);
$this->storeProviderIdentity($user, $providerAlias, $details);
}
protected function getExistingUser($providerAlias, $details)
{
$identity = $this->identities->getByProvider($providerAlias, $details);
return $this->users->findByIdentity($identity);
}
protected function storeProviderIdentity($user, $providerAlias, $details)
{
if ($this->identities->userExists($providerAlias, $details)) {
$this->updateProviderIdentity($providerAlias, $details);
} else {
$this->addProviderIdentity($user, $providerAlias, $details);
}
}
protected function updateProviderIdentity($providerAlias, $details)
{
$identity = $this->identities->getByProvider($providerAlias, $details);
$identity->access_token = $details->access_token;
$this->identities->store($identity);
}
protected function addProviderIdentity($user, $providerAlias, $details)
{
$identity = new OAuthIdentity;
$identity->user_id = $user->getKey();
$identity->provider = $providerAlias;
$identity->provider_user_id = $details->id;
$identity->access_token = $details->access_token;
$this->identities->store($identity);
}
}
-28
View File
@@ -1,28 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth;
class EloquentIdentityStore implements IdentityStore
{
public function getByProvider($provider, $providerUser)
{
return OAuthIdentity::where('provider', $provider)
->where('provider_user_id', $providerUser->id)
->first();
}
public function flush($user, $provider)
{
OAuthIdentity::where('user_id', $user->getKey())
->where('provider', $provider)
->delete();
}
public function store($identity)
{
$identity->save();
}
public function userExists($provider, $providerUser)
{
return (bool) $this->getByProvider($provider, $providerUser);
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth\Facades;
use Illuminate\Support\Facades\Facade;
class OAuth extends Facade
{
protected static function getFacadeAccessor()
{
return 'adamwathan.oauth';
}
}
-9
View File
@@ -1,9 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth;
interface IdentityStore
{
public function getByProvider($provider, $providerUser);
public function flush($user, $provider);
public function store($identity);
public function userExists($provider, $providerUser);
}
-26
View File
@@ -1,26 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Config;
/**
* @property $id
* @property $user_id
* @property $provider
* @property $provider_user_id
* @property $access_token
*/
class OAuthIdentity extends Eloquent
{
protected static $configuredTable = 'oauth_identities';
public static function configureTable($table)
{
static::$configuredTable = $table;
}
public function getTable()
{
return static::$configuredTable;
}
}
-37
View File
@@ -1,37 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth;
class OAuthManager
{
protected $redirect;
protected $authenticator;
protected $socialnorm;
public function __construct($redirect, $authenticator, $socialnorm)
{
$this->redirect = $redirect;
$this->authenticator = $authenticator;
$this->socialnorm = $socialnorm;
}
public function authorize($providerAlias)
{
return $this->redirect->to($this->socialnorm->authorize($providerAlias));
}
public function login($providerAlias, $callback = null)
{
$details = $this->socialnorm->getUser($providerAlias);
return $this->authenticator->login($providerAlias, $details, $callback, $remember = false);
}
public function loginForever($providerAlias, $callback = null)
{
$details = $this->socialnorm->getUser($providerAlias);
return $this->authenticator->login($providerAlias, $details, $callback, $remember = true);
}
public function registerProvider($alias, $provider)
{
$this->socialnorm->registerProvider($alias, $provider);
}
}
-23
View File
@@ -1,23 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth;
use SocialNorm\Session as SocialNormSession;
class Session implements SocialNormSession
{
private $store;
public function __construct($store)
{
$this->store = $store;
}
public function get($key)
{
return $this->store->get($key);
}
public function put($key, $value)
{
return $this->store->put($key, $value);
}
}
-27
View File
@@ -1,27 +0,0 @@
<?php namespace AdamWathan\EloquentOAuth;
class UserStore
{
protected $model;
public function __construct($model)
{
$this->model = $model;
}
public function create()
{
$user = new $this->model;
return $user;
}
public function store($user)
{
return $user->save();
}
public function findByIdentity($identity)
{
return $identity->belongsTo($this->model, 'user_id')->firstOrFail();
}
}