2019-05-22 05:18:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-22 05:18:18 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-22 05:18:18 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-22 05:18:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Libraries\OAuth;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
2020-05-12 11:56:30 +02:00
|
|
|
use App\Libraries\OAuth\Providers\Google;
|
2019-05-22 05:18:18 +02:00
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class OAuth.
|
2019-05-22 05:18:18 +02:00
|
|
|
*/
|
|
|
|
class OAuth
|
|
|
|
{
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Socialite Providers.
|
2019-05-22 05:18:18 +02:00
|
|
|
*/
|
|
|
|
const SOCIAL_GOOGLE = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-22 05:18:18 +02:00
|
|
|
const SOCIAL_FACEBOOK = 2;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-22 05:18:18 +02:00
|
|
|
const SOCIAL_GITHUB = 3;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-22 05:18:18 +02:00
|
|
|
const SOCIAL_LINKEDIN = 4;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-22 05:18:18 +02:00
|
|
|
const SOCIAL_TWITTER = 5;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-22 05:18:18 +02:00
|
|
|
const SOCIAL_BITBUCKET = 6;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-06-11 04:07:46 +02:00
|
|
|
const SOCIAL_MICROSOFT = 7;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-06-11 05:26:47 +02:00
|
|
|
const SOCIAL_APPLE = 8;
|
2019-05-22 05:18:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Socialite $user
|
2023-07-26 04:59:36 +02:00
|
|
|
* @return bool | \App\Models\User | \App\Models\User | null
|
2019-05-22 05:18:18 +02:00
|
|
|
*/
|
2021-06-10 06:32:53 +02:00
|
|
|
public static function handleAuth($socialite_user, $provider)
|
2019-05-22 05:18:18 +02:00
|
|
|
{
|
|
|
|
/** 1. Ensure user arrives on the correct provider **/
|
|
|
|
$query = [
|
2022-06-11 05:26:47 +02:00
|
|
|
'oauth_user_id' => $socialite_user->getId(),
|
|
|
|
'oauth_provider_id' => $provider,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2022-06-21 11:57:17 +02:00
|
|
|
$first_name = trim(preg_replace('#'.preg_quote($last_name, '/').'#', '', $name));
|
2019-05-22 05:18:18 +02:00
|
|
|
|
|
|
|
return [$first_name, $last_name];
|
|
|
|
}
|
|
|
|
|
2022-06-11 05:26:47 +02:00
|
|
|
public static function providerToString(int $social_provider): string
|
2019-05-22 05:18:18 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
switch ($social_provider) {
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_GOOGLE:
|
2019-05-22 05:18:18 +02:00
|
|
|
return 'google';
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_FACEBOOK:
|
2019-05-22 05:18:18 +02:00
|
|
|
return 'facebook';
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_GITHUB:
|
2019-05-22 05:18:18 +02:00
|
|
|
return 'github';
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_LINKEDIN:
|
2019-05-22 05:18:18 +02:00
|
|
|
return 'linkedin';
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_TWITTER:
|
2019-05-22 05:18:18 +02:00
|
|
|
return 'twitter';
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_BITBUCKET:
|
2019-05-22 05:18:18 +02:00
|
|
|
return 'bitbucket';
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_MICROSOFT:
|
2022-06-11 04:07:46 +02:00
|
|
|
return 'microsoft';
|
2023-05-29 04:54:41 +02:00
|
|
|
case self::SOCIAL_APPLE:
|
2022-06-11 05:26:47 +02:00
|
|
|
return 'apple';
|
2019-05-22 05:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 05:26:47 +02:00
|
|
|
public static function providerToInt(string $social_provider): int
|
2019-05-22 05:18:18 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
switch ($social_provider) {
|
2019-05-22 05:18:18 +02:00
|
|
|
case 'google':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_GOOGLE;
|
2019-05-22 05:18:18 +02:00
|
|
|
case 'facebook':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_FACEBOOK;
|
2019-05-22 05:18:18 +02:00
|
|
|
case 'github':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_GITHUB;
|
2019-05-22 05:18:18 +02:00
|
|
|
case 'linkedin':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_LINKEDIN;
|
2019-05-22 05:18:18 +02:00
|
|
|
case 'twitter':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_TWITTER;
|
2019-05-22 05:18:18 +02:00
|
|
|
case 'bitbucket':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_BITBUCKET;
|
2022-06-11 04:07:46 +02:00
|
|
|
case 'microsoft':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_MICROSOFT;
|
2022-06-11 05:26:47 +02:00
|
|
|
case 'apple':
|
2023-05-29 04:54:41 +02:00
|
|
|
return self::SOCIAL_APPLE;
|
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 Google();
|
|
|
|
$this->provider_id = self::SOCIAL_GOOGLE;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-05-22 05:18:18 +02:00
|
|
|
return $this;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTokenResponse($token)
|
|
|
|
{
|
|
|
|
$user = false;
|
|
|
|
|
|
|
|
$payload = $this->provider_instance->getTokenResponse($token);
|
|
|
|
|
|
|
|
$oauth_user_id = $this->provider_instance->harvestSubField($payload);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'oauth_user_id' => $oauth_user_id,
|
2020-09-06 11:38:10 +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 ($this->provider_instance) {
|
2019-05-22 05:18:18 +02:00
|
|
|
$user = MultiDB::hasUser($data);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-05-22 05:18:18 +02:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|