-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff01cb4
commit 383c0f4
Showing
3 changed files
with
109 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
adamwathan
Author
Owner
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why not $this->auth->login($user, true); ?