From 8e7dc5fca4e58733b11ddc28d50c3c4805579250 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 17 Jun 2022 11:48:17 +1000 Subject: [PATCH] Microsoft oauth --- app/Http/Controllers/Auth/LoginController.php | 49 ++++++++++++----- composer.json | 1 + composer.lock | 53 ++++++++++++++++++- 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index ec52fd49644a..8f2f5484caf8 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -23,6 +23,7 @@ use App\Jobs\Util\SystemLogger; use App\Libraries\MultiDB; use App\Libraries\OAuth\OAuth; use App\Libraries\OAuth\Providers\Google; +use App\Models\Account; use App\Models\Client; use App\Models\Company; use App\Models\CompanyToken; @@ -363,17 +364,18 @@ class LoginController extends BaseController if (request()->input('provider') == 'google') { return $this->handleGoogleOauth(); } elseif (request()->input('provider') == 'microsoft') { - if (request()->has('token')) { - return $this->handleSocialiteLogin('microsoft', request()->get('token')); - } else { - $message = 'Bearer token missing for the microsoft login'; - } + // if (request()->has('token')) { + // return $this->handleSocialiteLogin('microsoft', request()->get('token')); + // } else { + // $message = 'Bearer token missing for the microsoft login'; + // } + return $this->handleMicrosoftOauth(); } elseif (request()->input('provider') == 'apple') { - if (request()->has('token')) { - return $this->handleSocialiteLogin('apple', request()->get('token')); - } else { - $message = 'Token is missing for the apple login'; - } + // if (request()->has('token')) { + // return $this->handleSocialiteLogin('apple', request()->get('token')); + // } else { + // $message = 'Token is missing for the apple login'; + // } } return response() @@ -483,6 +485,9 @@ class LoginController extends BaseController $cu = CompanyUser::query()->where('user_id', auth()->user()->id); + if($cu->count() == 0) + return $cu; + if (CompanyUser::query()->where('user_id', auth()->user()->id)->where('company_id', auth()->user()->account->default_company_id)->exists()) $set_company = auth()->user()->account->default_company; else { @@ -521,6 +526,24 @@ class LoginController extends BaseController } + private function handleMicrosoftOauth() + { + if(request()->has('accessToken')) + $accessToken = request()->input('accessToken'); + else + return response()->json(['message' => 'Invalid response from oauth server'], 400); + + $graph = new Microsoft\Graph(); + $graph->setAccessToken($accessToken); + + $user = $graph->createRequest("GET", "/me") + ->setReturnType(Microsoft\Graph\Model\User::class) + ->execute(); + + nlog($user); + + } + private function handleGoogleOauth() { $user = false; @@ -599,9 +622,6 @@ class LoginController extends BaseController $cu = $this->hydrateCompanyUser(); - // $cu = CompanyUser::query() - // ->where('user_id', auth()->user()->id); - if ($cu->count() == 0) return response()->json(['message' => 'User found, but not attached to any companies, please see your administrator'], 400); @@ -628,6 +648,9 @@ class LoginController extends BaseController $account = CreateAccount::dispatchNow($new_account, request()->getClientIp()); + if(!$account instanceOf Account) + return $account; + Auth::login($account->default_company->owner(), true); auth()->user()->email_verified_at = now(); auth()->user()->save(); diff --git a/composer.json b/composer.json index bbcce36e61c8..c67ba67be29d 100644 --- a/composer.json +++ b/composer.json @@ -71,6 +71,7 @@ "league/fractal": "^0.17.0", "league/omnipay": "^3.1", "livewire/livewire": "^2.6", + "microsoft/microsoft-graph": "^1.69", "mollie/mollie-api-php": "^2.36", "nelexa/zip": "^4.0", "nwidart/laravel-modules": "8.3", diff --git a/composer.lock b/composer.lock index 049fe7120a95..36f2634ab10b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6845489fdc254427c4536e22f025ff51", + "content-hash": "df84a1903809a8e781d937e679821e74", "packages": [ { "name": "afosto/yaac", @@ -4928,6 +4928,57 @@ ], "time": "2022-04-07T21:38:12+00:00" }, + { + "name": "microsoft/microsoft-graph", + "version": "1.69.0", + "source": { + "type": "git", + "url": "https://github.com/microsoftgraph/msgraph-sdk-php.git", + "reference": "dc867afdb2c89ea7ead37d6bcfcaf0389e7a85f4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/microsoftgraph/msgraph-sdk-php/zipball/dc867afdb2c89ea7ead37d6bcfcaf0389e7a85f4", + "reference": "dc867afdb2c89ea7ead37d6bcfcaf0389e7a85f4", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^6.0 || ^7.0", + "php": "^8.0 || ^7.3", + "psr/http-message": "^1.0" + }, + "require-dev": { + "mikey179/vfsstream": "^1.2", + "phpstan/phpstan": "^0.12.90 || ^1.0.0", + "phpunit/phpunit": "^8.0 || ^9.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Microsoft\\Graph\\": "src/", + "Beta\\Microsoft\\Graph\\": "src/Beta/Microsoft/Graph/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Microsoft Graph Client Tooling", + "email": "graphtooling@service.microsoft.com", + "role": "Developer" + } + ], + "description": "The Microsoft Graph SDK for PHP", + "homepage": "https://developer.microsoft.com/en-us/graph", + "support": { + "issues": "https://github.com/microsoftgraph/msgraph-sdk-php/issues", + "source": "https://github.com/microsoftgraph/msgraph-sdk-php/tree/1.69.0" + }, + "time": "2022-06-15T11:11:33+00:00" + }, { "name": "mollie/mollie-api-php", "version": "v2.44.1",