Merge branch 'develop' of github.com:invoiceninja/invoiceninja into develop

This commit is contained in:
Hillel Coren 2017-08-08 20:22:01 +03:00
commit 5eaa938809
2 changed files with 18 additions and 10 deletions

View File

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

View File

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