1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

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

This commit is contained in:
Hillel Coren 2017-03-30 20:33:09 +03:00
commit 4666387aa6
5 changed files with 85 additions and 17 deletions

View File

@ -6,6 +6,7 @@ use App\Events\UserSignedUp;
use App\Http\Requests\RegisterRequest;
use App\Http\Requests\UpdateAccountRequest;
use App\Models\Account;
use App\Ninja\OAuth\OAuth;
use App\Ninja\Repositories\AccountRepository;
use App\Ninja\Transformers\AccountTransformer;
use App\Ninja\Transformers\UserAccountTransformer;
@ -188,25 +189,15 @@ class AccountApiController extends BaseAPIController
$token = $request->input('token');
$provider = $request->input('provider');
try {
$user = Socialite::driver($provider)->stateless()->userFromToken($token);
} catch (Exception $exception) {
return $this->errorResponse(['message' => $exception->getMessage()], 401);
}
$oAuth = new OAuth();
$user = $oAuth->getProvider($provider)->getTokenResponse($token);
if ($user) {
$providerId = AuthService::getProviderId($provider);
$user = $this->accountRepo->findUserByOauth($providerId, $user->id);
}
if ($user) {
if($user) {
Auth::login($user);
return $this->processLogin($request);
} else {
sleep(ERROR_DELAY);
return $this->errorResponse(['message' => 'Invalid credentials'], 401);
}
else
return $this->errorResponse(['message' => 'Invalid credentials'], 401);
}
}

44
app/Ninja/OAuth/OAuth.php Normal file
View File

@ -0,0 +1,44 @@
<?php namespace App\Ninja\OAuth;
use App\Models\User;
class OAuth {
private $providerInstance;
public function __construct()
{
}
public function getProvider($provider)
{
switch ($provider)
{
case 'google';
$this->providerInstance = new Providers\Google();
return $this;
default:
return null;
break;
}
}
public function getTokenResponse($token)
{
$email = null;
$user = null;
if($this->providerInstance)
$user = User::where('email', $this->providerInstance->getTokenResponse($token))->first();
if ($user)
return $user;
else
return false;
}
}
?>

View File

@ -0,0 +1,23 @@
<?php namespace App\Ninja\OAuth\Providers;
class Google implements ProviderInterface
{
public function getTokenResponse($token)
{
$client = new \Google_Client(['client_id' => env('GOOGLE_CLIENT_ID','')]);
$payload = $client->verifyIdToken($token);
if ($payload)
return $this->harvestEmail($payload);
else
return null;
}
public function harvestEmail($payload)
{
return $payload['email'];
}
}

View File

@ -0,0 +1,9 @@
<?php namespace App\Ninja\OAuth\Providers;
interface ProviderInterface
{
public function getTokenResponse($token);
public function harvestEmail($response);
}

View File

@ -83,7 +83,8 @@
"nwidart/laravel-modules": "^1.14",
"jonnyw/php-phantomjs": "4.*",
"collizo4sky/omnipay-wepay": "^1.3",
"barryvdh/laravel-cors": "^0.9.1"
"barryvdh/laravel-cors": "^0.9.1",
"google/apiclient":"^2.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",