2019-07-08 02:08:57 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-07-08 02:08:57 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-07-08 02:08:57 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Contact;
|
|
|
|
|
|
|
|
use App\Http\Controllers\BaseController;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Jobs\Account\CreateAccount;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Libraries\OAuth\OAuth;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\User;
|
2019-07-09 12:32:26 +02:00
|
|
|
use App\Transformers\ClientContactLoginTransformer;
|
2019-07-08 02:08:57 +02:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
|
|
|
class LoginController extends BaseController
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Login Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller handles authenticating users for the application and
|
|
|
|
| redirecting them to your home screen. The controller uses a trait
|
|
|
|
| to conveniently provide its functionality to your applications.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use AuthenticatesUsers;
|
|
|
|
use UserSessionAttributes;
|
|
|
|
|
|
|
|
protected $entity_type = ClientContact::class;
|
|
|
|
|
2019-07-09 12:32:26 +02:00
|
|
|
protected $entity_transformer = ClientContactLoginTransformer::class;
|
2019-07-08 02:08:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Login via API.
|
2019-07-08 02:08:57 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request The request
|
2019-07-08 02:08:57 +02:00
|
|
|
*
|
|
|
|
* @return Response|User Process user login.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2019-07-08 02:08:57 +02:00
|
|
|
*/
|
|
|
|
public function apiLogin(Request $request)
|
|
|
|
{
|
|
|
|
Auth::shouldUse('contact');
|
|
|
|
|
|
|
|
$this->validateLogin($request);
|
|
|
|
|
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
|
|
|
$this->fireLockoutEvent($request);
|
|
|
|
|
|
|
|
return response()->json(['message' => 'Too many login attempts, you are being throttled']);
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($this->attemptLogin($request)) {
|
2019-07-08 02:08:57 +02:00
|
|
|
return $this->itemResponse($this->guard()->user());
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-07-08 02:08:57 +02:00
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
|
|
|
|
return response()->json(['message' => ctrans('texts.invalid_credentials')]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Redirect the user to the provider authentication page.
|
2019-07-08 02:08:57 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param string $provider
|
2019-07-08 02:08:57 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-30 22:59:12 +01:00
|
|
|
public function redirectToProvider(string $provider)
|
2019-07-08 02:08:57 +02:00
|
|
|
{
|
|
|
|
//'https://www.googleapis.com/auth/gmail.send','email','profile','openid'
|
|
|
|
//
|
2019-12-30 22:59:12 +01:00
|
|
|
if (request()->has('code')) {
|
2019-07-08 02:08:57 +02:00
|
|
|
return $this->handleProviderCallback($provider);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-07-08 02:08:57 +02:00
|
|
|
return Socialite::driver($provider)->scopes()->redirect();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-08 02:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function redirectToProviderAndCreate(string $provider)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
$redirect_url = config('services.'.$provider.'.redirect').'/create';
|
2019-07-08 02:08:57 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (request()->has('code')) {
|
2019-07-08 02:08:57 +02:00
|
|
|
return $this->handleProviderCallbackAndCreate($provider);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-07-08 02:08:57 +02:00
|
|
|
return Socialite::driver($provider)->redirectUrl($redirect_url)->redirect();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-08 02:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
public function handleProviderCallbackAndCreate(string $provider)
|
|
|
|
{
|
|
|
|
$socialite_user = Socialite::driver($provider)
|
|
|
|
->stateless()
|
|
|
|
->user();
|
|
|
|
|
|
|
|
if($user = OAuth::handleAuth($socialite_user, $provider))
|
|
|
|
{
|
|
|
|
Auth::login($user, true);
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
return redirect($this->redirectTo);
|
|
|
|
}
|
|
|
|
else if(MultiDB::checkUserEmailExists($socialite_user->getEmail()))
|
|
|
|
{
|
|
|
|
Session::flash('error', 'User exists in system, but not with this authentication method'); //todo add translations
|
|
|
|
|
|
|
|
return view('auth.login');
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-08 02:08:57 +02:00
|
|
|
else {
|
2019-12-30 22:59:12 +01:00
|
|
|
//todo
|
2019-07-08 02:08:57 +02:00
|
|
|
$name = OAuth::splitName($socialite_user->getName());
|
|
|
|
|
|
|
|
$new_account = [
|
|
|
|
'first_name' => $name[0],
|
|
|
|
'last_name' => $name[1],
|
|
|
|
'password' => '',
|
|
|
|
'email' => $socialite_user->getEmail(),
|
|
|
|
'oauth_user_id' => $socialite_user->getId(),
|
|
|
|
'oauth_provider_id' => $provider
|
|
|
|
];
|
|
|
|
|
|
|
|
$account = CreateAccount::dispatchNow($new_account);
|
|
|
|
|
|
|
|
Auth::login($account->default_company->owner(), true);
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
$cookie = cookie('db', $account->default_company->db);
|
|
|
|
|
|
|
|
return redirect($this->redirectTo)->withCookie($cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* We use this function when OAUTHING via the web interface.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* @return redirect
|
|
|
|
|
|
|
|
public function handleProviderCallback(string $provider)
|
2019-07-08 02:08:57 +02:00
|
|
|
{
|
|
|
|
$socialite_user = Socialite::driver($provider)
|
|
|
|
->stateless()
|
|
|
|
->user();
|
|
|
|
|
|
|
|
if($user = OAuth::handleAuth($socialite_user, $provider))
|
|
|
|
{
|
|
|
|
Auth::login($user, true);
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
return redirect($this->redirectTo);
|
|
|
|
}
|
|
|
|
else if(MultiDB::checkUserEmailExists($socialite_user->getEmail()))
|
|
|
|
{
|
|
|
|
Session::flash('error', 'User exists in system, but not with this authentication method'); //todo add translations
|
|
|
|
|
|
|
|
return view('auth.login');
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-08 02:08:57 +02:00
|
|
|
else {
|
2019-12-30 22:59:12 +01:00
|
|
|
//todo
|
2019-07-08 02:08:57 +02:00
|
|
|
$name = OAuth::splitName($socialite_user->getName());
|
|
|
|
|
|
|
|
$new_account = [
|
|
|
|
'first_name' => $name[0],
|
|
|
|
'last_name' => $name[1],
|
|
|
|
'password' => '',
|
|
|
|
'email' => $socialite_user->getEmail(),
|
|
|
|
'oauth_user_id' => $socialite_user->getId(),
|
|
|
|
'oauth_provider_id' => $provider
|
|
|
|
];
|
|
|
|
|
|
|
|
$account = CreateAccount::dispatchNow($new_account);
|
|
|
|
|
|
|
|
Auth::login($account->default_company->owner(), true);
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
$cookie = cookie('db', $account->default_company->db);
|
|
|
|
|
|
|
|
return redirect($this->redirectTo)->withCookie($cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
*/
|
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
/**
|
2019-12-30 22:59:12 +01:00
|
|
|
* A client side authentication has taken place.
|
2019-07-08 02:08:57 +02:00
|
|
|
* We now digest the token and confirm authentication with
|
|
|
|
* the authentication server, the correct user object
|
|
|
|
* is returned to us here and we send back the correct
|
|
|
|
* user object payload - or error.
|
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
* This can be extended to a create route also - need to pass a ?create query parameter and
|
2019-07-08 02:08:57 +02:00
|
|
|
* then process the signup
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-07-08 02:08:57 +02:00
|
|
|
* return User $user
|
|
|
|
*/
|
|
|
|
public function oauthApiLogin()
|
|
|
|
{
|
|
|
|
$user = false;
|
|
|
|
|
|
|
|
$oauth = new OAuth();
|
|
|
|
|
|
|
|
$user = $oauth->getProvider(request()->input('provider'))->getTokenResponse(request()->input('token'));
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($user) {
|
2019-07-08 02:08:57 +02:00
|
|
|
return $this->itemResponse($user);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-07-08 02:08:57 +02:00
|
|
|
return $this->errorResponse(['message' => 'Invalid credentials'], 401);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-08 02:08:57 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/*
|
2019-07-08 02:08:57 +02:00
|
|
|
* Received the returning object from the provider
|
|
|
|
* which we will use to resolve the user, we return the response in JSON format
|
|
|
|
*
|
|
|
|
* @return json
|
2019-12-30 22:59:12 +01:00
|
|
|
|
|
|
|
public function handleProviderCallbackApiUser(string $provider)
|
2019-07-08 02:08:57 +02:00
|
|
|
{
|
|
|
|
$socialite_user = Socialite::driver($provider)->stateless()->user();
|
|
|
|
|
|
|
|
if($user = OAuth::handleAuth($socialite_user, $provider))
|
|
|
|
{
|
|
|
|
return $this->itemResponse($user);
|
|
|
|
}
|
|
|
|
else if(MultiDB::checkUserEmailExists($socialite_user->getEmail()))
|
|
|
|
{
|
|
|
|
|
|
|
|
return $this->errorResponse(['message'=>'User exists in system, but not with this authentication method'], 400);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-07-08 02:08:57 +02:00
|
|
|
else {
|
2019-12-30 22:59:12 +01:00
|
|
|
//todo
|
2019-07-08 02:08:57 +02:00
|
|
|
$name = OAuth::splitName($socialite_user->getName());
|
|
|
|
|
|
|
|
$new_account = [
|
|
|
|
'first_name' => $name[0],
|
|
|
|
'last_name' => $name[1],
|
|
|
|
'password' => '',
|
|
|
|
'email' => $socialite_user->getEmail(),
|
|
|
|
];
|
|
|
|
|
|
|
|
$account = CreateAccount::dispatchNow($new_account);
|
|
|
|
|
|
|
|
return $this->itemResponse($account->default_company->owner());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|