1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Add support for Multi DB Lookup for OAuth from API. (#1605)

* bug fixes for OAuth

* Add support for Multi-DB Lookup
This commit is contained in:
David Bomba 2017-08-08 15:26:19 +10:00 committed by GitHub
parent 07296fae9b
commit f8f0a23a10
2 changed files with 17 additions and 8 deletions

View File

@ -1,10 +1,17 @@
<?php namespace App\Ninja\OAuth;
use App\Models\LookupUser;
use App\Models\User;
class OAuth {
const SOCIAL_GOOGLE = 1;
const SOCIAL_FACEBOOK = 2;
const SOCIAL_GITHUB = 3;
const SOCIAL_LINKEDIN = 4;
private $providerInstance;
private $providerId;
public function __construct()
{
@ -16,6 +23,7 @@ class OAuth {
{
case 'google';
$this->providerInstance = new Providers\Google();
$this->providerId = self::SOCIAL_GOOGLE;
return $this;
default:
@ -26,11 +34,16 @@ class OAuth {
public function getTokenResponse($token)
{
$email = null;
$user = null;
$payload = $this->providerInstance->getTokenResponse($token);
$oauthUserId = $this->providerInstance->harvestSubField($payload);
LookupUser::setServerByField('oauth_user_key', $this->providerId . '-' . $oauthUserId);
if($this->providerInstance)
$user = User::where('oauth_user_id', $this->providerInstance->getTokenResponse($token))->first();
$user = User::where('oauth_user_id', $oauthUserId)->where('oauth_provider_id', $this->providerId)->first();
if ($user)
return $user;

View File

@ -7,11 +7,7 @@ class Google implements ProviderInterface
{
$client = new \Google_Client(['client_id' => env('GOOGLE_CLIENT_ID','')]);
$payload = $client->verifyIdToken($token);
if ($payload)
return $this->harvestSubField($payload);
else
return null;
return $client->verifyIdToken($token);
}
public function harvestEmail($payload)
@ -19,7 +15,7 @@ class Google implements ProviderInterface
return $payload['email'];
}
private function harvestSubField($payload)
public function harvestSubField($payload)
{
$data = $payload->getAttributes();
return $data['payload']['sub']; // user ID