2018-10-18 07:04:36 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
namespace App\Libraries;
|
|
|
|
|
2018-12-02 11:42:06 +01:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2018-10-18 07:04:36 +02:00
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class OAuth
|
|
|
|
* @package App\Libraries
|
|
|
|
*/
|
|
|
|
class OAuth
|
|
|
|
{
|
2019-05-22 05:18:18 +02:00
|
|
|
protected $provider_instance;
|
|
|
|
|
|
|
|
protected $provider_id;
|
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
/**
|
|
|
|
* Socialite Providers
|
|
|
|
*/
|
|
|
|
const SOCIAL_GOOGLE = 1;
|
|
|
|
const SOCIAL_FACEBOOK = 2;
|
|
|
|
const SOCIAL_GITHUB = 3;
|
|
|
|
const SOCIAL_LINKEDIN = 4;
|
|
|
|
const SOCIAL_TWITTER = 5;
|
|
|
|
const SOCIAL_BITBUCKET = 6;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Socialite $user
|
|
|
|
*/
|
|
|
|
|
2019-05-22 02:56:47 +02:00
|
|
|
public static function handleAuth(object $user, string $provider)
|
2018-10-18 07:04:36 +02:00
|
|
|
{
|
2018-12-02 11:42:06 +01:00
|
|
|
/** 1. Ensure user arrives on the correct provider **/
|
|
|
|
|
|
|
|
$query = [
|
2019-01-25 12:14:09 +01:00
|
|
|
'oauth_user_id' =>$user->getId(),
|
2018-12-02 11:42:06 +01:00
|
|
|
'oauth_provider_id'=>$provider
|
|
|
|
];
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($user = MultiDB::hasUser($query)) {
|
2018-12-02 11:42:06 +01:00
|
|
|
return $user;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-05-22 02:56:47 +02:00
|
|
|
return false;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-05-22 02:56:47 +02:00
|
|
|
}
|
2018-12-02 11:42:06 +01:00
|
|
|
|
2019-05-22 02:56:47 +02:00
|
|
|
/* Splits a socialite user name into first and last names */
|
|
|
|
public static function splitName($name)
|
|
|
|
{
|
|
|
|
$name = trim($name);
|
|
|
|
$last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
|
|
|
|
$first_name = trim(preg_replace('#' . preg_quote($last_name, '/') . '#', '', $name));
|
2018-12-02 11:42:06 +01:00
|
|
|
|
2019-05-22 02:56:47 +02:00
|
|
|
return [$first_name, $last_name];
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2018-12-02 11:42:06 +01:00
|
|
|
public static function providerToString(int $social_provider) : string
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
switch ($social_provider) {
|
2018-10-22 14:04:37 +02:00
|
|
|
case SOCIAL_GOOGLE:
|
|
|
|
return 'google';
|
|
|
|
case SOCIAL_FACEBOOK:
|
|
|
|
return 'facebook';
|
|
|
|
case SOCIAL_GITHUB:
|
|
|
|
return 'github';
|
|
|
|
case SOCIAL_LINKEDIN:
|
|
|
|
return 'linkedin';
|
|
|
|
case SOCIAL_TWITTER:
|
|
|
|
return 'twitter';
|
|
|
|
case SOCIAL_BITBUCKET:
|
|
|
|
return 'bitbucket';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 11:42:06 +01:00
|
|
|
public static function providerToInt(string $social_provider) : int
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
switch ($social_provider) {
|
2018-10-22 14:04:37 +02:00
|
|
|
case 'google':
|
|
|
|
return SOCIAL_GOOGLE;
|
|
|
|
case 'facebook':
|
|
|
|
return SOCIAL_FACEBOOK;
|
|
|
|
case 'github':
|
|
|
|
return SOCIAL_GITHUB;
|
|
|
|
case 'linkedin':
|
|
|
|
return SOCIAL_LINKEDIN;
|
|
|
|
case 'twitter':
|
|
|
|
return SOCIAL_TWITTER;
|
|
|
|
case 'bitbucket':
|
|
|
|
return SOCIAL_BITBUCKET;
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 05:18:18 +02:00
|
|
|
|
|
|
|
public function getProvider($provider)
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
switch ($provider) {
|
|
|
|
case 'google':
|
2019-05-22 05:18:18 +02:00
|
|
|
$this->provider_instance = new Providers\Google();
|
|
|
|
$this->provider_id = self::SOCIAL_GOOGLE;
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTokenResponse($token)
|
|
|
|
{
|
2019-05-23 02:25:55 +02:00
|
|
|
$user = false;
|
2019-05-22 05:18:18 +02:00
|
|
|
|
|
|
|
$payload = $this->provider_instance->getTokenResponse($token);
|
|
|
|
|
2019-05-23 02:25:55 +02:00
|
|
|
$oauth_user_id = $this->provider_instance->harvestSubField($payload);
|
2019-05-22 05:18:18 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$query = [
|
2019-12-04 03:00:59 +01:00
|
|
|
'oauth_user_id' => $oauth_user_id,
|
2019-05-23 02:25:55 +02:00
|
|
|
'oauth_provider_id'=> $this->provider_id
|
|
|
|
];
|
2019-05-22 05:18:18 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($user = MultiDB::hasUser($query)) {
|
2019-05-22 05:18:18 +02:00
|
|
|
return $user;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-05-22 05:18:18 +02:00
|
|
|
return false;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-05-22 05:18:18 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|