-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from jesusantguerrero/feat/stable-v2
Feat/stable v2
- Loading branch information
Showing
117 changed files
with
5,794 additions
and
4,850 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\User; | ||
use GuzzleHttp\Client; | ||
use Illuminate\Support\Facades\Auth; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
class NeatlancerController { | ||
|
||
public function connect() { | ||
// request()->session()->put('state', $state = Str::random(40)); | ||
|
||
// $query = http_build_query([ | ||
// 'client_id' => '98e6222a-6bd7-4f85-a4a6-01931fd0e3fe', | ||
// 'redirect_uri' => config('app.url') .'/oauth/accept', | ||
// 'response_type' => 'code', | ||
// 'scope' => '', | ||
// 'state' => $state, | ||
// // 'prompt' => "consent" | ||
// // 'prompt' => '', // "none", "consent", or "login" | ||
// ]); | ||
|
||
// return redirect(config('app.sso_url') . '/oauth/authorize?'.$query); | ||
return Socialite::driver('laravelpassport')->redirect(); | ||
} | ||
|
||
public function accept() { | ||
$accessInfo = Socialite::driver('laravelpassport')->user(); | ||
|
||
$guzzle = new Client(['base_uri' => config('app.sso_url')]); | ||
|
||
$raw_response = $guzzle->get('/api/current-user', [ | ||
'headers' => [ 'Authorization' => 'Bearer ' . $accessInfo->token ], | ||
]); | ||
|
||
$userInfo = json_decode($raw_response->getBody()->getContents()); | ||
|
||
$user = User::updateOrCreate([ | ||
'neatlancer_id' => $userInfo->id, | ||
], [ | ||
'name' => $userInfo->name, | ||
'email' => $userInfo->email, | ||
// 'avatar' => $userInfo?->avatar, | ||
'neatlancer_token' => $accessInfo->token, | ||
'neatlancer_refresh_token' => $accessInfo->refreshToken, | ||
]); | ||
|
||
Auth::login($user); | ||
return redirect('/dashboard'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
namespace App\Libraries; | ||
|
||
use App\Jobs\ProcessCalendar; | ||
use App\Jobs\ProcessGmail; | ||
use App\Models\User; | ||
use App\Models\Automation; | ||
use App\Models\Integration; | ||
use Exception; | ||
use Google\Client as GoogleClient; | ||
use Google\Service\Calendar; | ||
use Google\Service\Gmail; | ||
|
||
class GoogleService | ||
{ | ||
|
||
public static function getTokens($userId) { | ||
return User::find($userId); | ||
} | ||
public static function getConfigPath() { | ||
return base_path(config("integrations.google.credentials_path")); | ||
} | ||
|
||
public static function setTokens($data, $user, $integrationId = null) { | ||
if (!$integrationId && $_GET['code']) { | ||
|
||
$client = new GoogleClient([ 'client_id' => config('integrations.google.client_id')]); | ||
$client->setAuthConfig(self::getConfigPath()); | ||
$client->setAccessType('offline'); | ||
$userIdToken = $_GET['code']; | ||
$tokenResponse = $client->fetchAccessTokenWithAuthCode($userIdToken); | ||
$integration = Integration::where([ | ||
'user_id' => $user->id, | ||
'team_id' => $user->current_team_id, | ||
'name' => 'Google' | ||
])->first(); | ||
$googleUser = $client->verifyIdToken($tokenResponse["id_token"]); | ||
if ($googleUser['email'] == $user->email) { | ||
$integration->token = encrypt($tokenResponse['access_token']); | ||
$integration->save(); | ||
session(['g_token', json_encode($tokenResponse)]); | ||
return; | ||
} | ||
throw new Exception("Error obtaining the token" . $googleUser['email']); | ||
} else if ($integrationId) { | ||
$integration = Integration::find($integrationId); | ||
$integration->token = encrypt($data->refresh_token); | ||
session(['g_token', json_encode($data)]); | ||
|
||
return; | ||
}; | ||
} | ||
|
||
public static function getClient($integrationId) { | ||
$integration = Integration::find($integrationId); | ||
$client = new GoogleClient(); | ||
$client->setAuthConfig(self::getConfigPath()); | ||
if (!$accessToken = session('g_token')) { | ||
$accessToken = $client->fetchAccessTokenWithRefreshToken(decrypt($integration->token)); | ||
} | ||
|
||
$client->setAccessToken($accessToken); | ||
|
||
if ($client->isAccessTokenExpired()) { | ||
dd($accessToken); | ||
if ($refreshToken = $client->getRefreshToken()) { | ||
$tokenResponse = $client->fetchAccessTokenWithRefreshToken($refreshToken); | ||
self::setTokens((object) [ | ||
'access_token' => $accessToken, | ||
'refresh_token' => $refreshToken | ||
], | ||
$integration->user, | ||
$integrationId); | ||
$client->setAccessToken($accessToken); | ||
|
||
} | ||
} | ||
return $client; | ||
} | ||
|
||
public static function storeIntegration($data, $user) { | ||
Integration::updateOrCreate([ | ||
"team_id" => $user->current_team_id, | ||
"user_id" => $user->id, | ||
"name" => $data->service_name, | ||
"automation_service_id" => $data->service_id | ||
], [ | ||
"hash" => $user->email | ||
]); | ||
} | ||
|
||
// services | ||
public static function createItemFromCalendar($automationId, $afterResponse = null) { | ||
$automation = Automation::find($automationId); | ||
echo "$automation->name $automation->id \n"; | ||
$method = $afterResponse ? "dispatchAfterResponse" : "dispatch"; | ||
ProcessCalendar::$method($automation); | ||
return true; | ||
} | ||
|
||
public static function listCalendars(int $integrationId) { | ||
$client = self::getClient($integrationId); | ||
$service = new Calendar($client); | ||
return $service->calendarList->listCalendarList(); | ||
} | ||
|
||
public static function createItemFromGmail($automationId, $afterResponse = null) { | ||
$automation = Automation::find($automationId); | ||
echo "$automation->name $automation->id \n"; | ||
$method = $afterResponse ? "dispatchAfterResponse" : "dispatch"; | ||
ProcessGmail::$method($automation); | ||
return true; | ||
} | ||
|
||
public static function getSheetsService($integrationId) { | ||
$client = GoogleService::getClient($integrationId); | ||
$service = new Google_Service_Sheets($client); | ||
return $service; | ||
} | ||
|
||
public static function storeIntegration($data, $user) { | ||
Integration::updateOrCreate([ | ||
"team_id" => $user->current_team_id, | ||
"user_id" => $user->id, | ||
"name" => $data->service_name, | ||
"automation_service_id" => $data->service_id | ||
], [ | ||
"hash" => $user->email | ||
]); | ||
} | ||
|
||
public static function requestAccessToken($data, $user) { | ||
$client = new GoogleClient([ | ||
"client_id" => config('integrations.google.client_id') | ||
]); | ||
$client->addScope([ | ||
Gmail::GMAIL_READONLY, | ||
Calendar::CALENDAR_READONLY | ||
]); | ||
$client->setRedirectUri(config('app.url') . "/services/accept-oauth"); | ||
$client->setAccessType('offline'); | ||
$client->setLoginHint($user->email); | ||
$client->setApprovalPrompt('auto'); | ||
$client->setIncludeGrantedScopes(true); | ||
|
||
$authUrl = $client->createAuthUrl(); | ||
if ($authUrl) { | ||
self::storeIntegration($data, $user); | ||
} | ||
return $authUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.