Initial integration with Quicken (Intuit API), rework oauth tables, update/test google login

This commit is contained in:
Deon George
2022-08-12 14:53:06 +10:00
parent 70571cb6ac
commit 8fd79ce23e
16 changed files with 299 additions and 99 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
@@ -10,9 +11,7 @@ use Laravel\Socialite\Facades\Socialite;
use App\Http\Controllers\Controller;
use App\Mail\SocialLink;
use App\Models\Oauth;
use App\Models\AccountOauth;
use App\Models\User;
use App\Models\{ProviderOauth,ProviderToken,User,UserOauth};
use App\Providers\RouteServiceProvider;
class SocialLoginController extends Controller
@@ -26,10 +25,13 @@ class SocialLoginController extends Controller
{
$openiduser = Socialite::with($provider)->user();
$oo = Oauth::firstOrCreate(['name'=>$provider,'active'=>TRUE]);
if (! $openiduser)
return redirect('/home')->with('error','No user details obtained.');
$oo = ProviderOauth::firstOrCreate(['name'=>$provider,'active'=>TRUE]);
// See if this user has connected and linked previously
$aoo = $oo->accounts->where('userid',$openiduser->id);
$aoo = $oo->users->where('userid',$openiduser->id);
if ($aoo->count() == 1) {
$aoo = $aoo->first();
@@ -59,10 +61,10 @@ class SocialLoginController extends Controller
// See if their is an account with this email address
if ($uo->count() == 1) {
$aoo = new AccountOauth;
$aoo = new UserOauth;
$aoo->userid = $openiduser->id;
$aoo->oauth_data = $openiduser->user;
$oo->accounts()->save($aoo);
$oo->users()->save($aoo);
return $this->link($provider,$aoo,$uo->first());
@@ -78,16 +80,43 @@ class SocialLoginController extends Controller
return redirect()->intended(RouteServiceProvider::HOME);
}
public function handleBearerTokenCallback($provider)
{
$openiduser = Socialite::with($provider)->user();
if (! $openiduser)
return redirect('/home')->with('error','No user details obtained.');
$po = ProviderOauth::where('name',$provider)->singleOrFail();
$uoo = ProviderToken::where('user_id',Auth::id())->where('provider_oauth_id',$po->id)->firstOrNew();
$uoo->user_id = Auth::id();
$uoo->access_token = $openiduser->token;
$uoo->access_token_expires_at = Carbon::now()->addSeconds($openiduser->expiresIn);
$uoo->refresh_token = $openiduser->refreshToken;
$uoo->refresh_token_expires_at = Carbon::now()->addSeconds($openiduser->refresh_token_expires_in);
$uoo->realm_id = $openiduser->realmid;
$po->tokens()->save($uoo);
return redirect()
->intended(RouteServiceProvider::HOME)
->with('success','Token refreshed.');
}
/**
* We have identified the user and oauth, just need them to confirm the link
*
* @param $provider
* @param $provider
* @param UserOauth $ao
* @param User $uo
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return \Illuminate\View\View
*/
public function link($provider,AccountOauth $ao,User $uo)
public function link($provider,UserOauth $ao,User $uo): \Illuminate\View\View
{
Mail::to($uo->email)->send(new SocialLink($ao));
// @note If this is sent now (send()), it results in the caller to be executed a second time (handleProviderCallback()).
Mail::to($uo->email)->queue(new SocialLink($ao));
return view('auth.social_link')
->with('oauthid',$ao->id)
@@ -97,7 +126,7 @@ class SocialLoginController extends Controller
public function linkcomplete(Request $request,$provider)
{
// Load our oauth id
$aoo = AccountOauth::findOrFail($request->post('oauthid'));
$aoo = UserOauth::findOrFail($request->post('oauthid'));
// Check our email matches
if (Arr::get($aoo->oauth_data,'email','invalid') !== $request->post('email'))

View File

@@ -6,24 +6,26 @@ use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\{AccountOauth,User};
use App\Models\{Site,User,UserOauth};
class SocialLink extends Mailable
{
use Queueable, SerializesModels;
public string $token;
public User $user;
public Site $site;
public ?User $user;
/**
* Create a new message instance.
*
* @param AccountOauth $o
* @param UserOauth $o
*/
public function __construct(AccountOauth $o)
public function __construct(UserOauth $o)
{
$this->site = $o->site;
$this->token = $o->link_token;
$this->user = $o->account->user;
$this->user = $o->user;
}
/**
@@ -37,7 +39,7 @@ class SocialLink extends Mailable
->markdown('email.system.social_link')
->subject('Link your Account')
->with([
'site'=>$this->user->site,
]);
'site'=>$this->site,
]);
}
}

View File

@@ -1,46 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey;
class AccountOauth extends Model
{
use NextKey;
const RECORD_ID = 'account_oauth';
public $incrementing = FALSE;
protected $table = 'ab_account_oauth';
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
public $dateFormat = 'U';
protected $casts = [
'oauth_data'=>'array',
];
public function account()
{
return $this->belongsTo(Account::class);
}
public function site()
{
return $this->belongsTo(Site::class);
}
public function User()
{
return $this->belongsTo(User::class);
}
/**
* Get a link token to use when validating account.
*/
public function getLinkTokenAttribute(): string
{
return strtoupper(substr(md5($this->id.$this->date_last),0,8));
}
}

View File

@@ -1,24 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\NextKey;
class Oauth extends Model
{
use NextKey;
const RECORD_ID = 'oauth';
public $incrementing = FALSE;
protected $table = 'ab_oauth';
public $timestamps = FALSE;
protected $fillable = ['name','active'];
public function accounts()
{
return $this->hasMany(AccountOauth::class);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ProviderOauth extends Model
{
protected $table = 'provider_oauth';
protected $fillable = ['name','active'];
/* RELATIONS */
public function tokens()
{
return $this->hasMany(ProviderToken::class);
}
public function users()
{
return $this->hasMany(UserOauth::class);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\SiteID;
class ProviderToken extends Model
{
use SiteID;
protected $dates = [
'access_token_expires_at',
'refresh_token_expires_at',
];
}

31
app/Models/UserOauth.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\SiteID;
class UserOauth extends Model
{
use SiteID;
protected $table = 'user_oauth';
protected $casts = [
'oauth_data'=>'json',
];
public function User()
{
return $this->belongsTo(User::class);
}
/**
* Get a link token to use when validating account.
*/
public function getLinkTokenAttribute(): string
{
return strtoupper(substr(md5($this->id.$this->date_last),0,8));
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;
use Leenooks\Traits\SingleOrFail;
class AppServiceProvider extends ServiceProvider
@@ -16,7 +17,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
//
Passport::ignoreMigrations();
}
/**

View File

@@ -2,12 +2,15 @@
namespace App\Providers;
use Intuit\Traits\IntuitSocialite;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
use IntuitSocialite;
/**
* The policy mappings for the application.
*
@@ -25,6 +28,7 @@ class AuthServiceProvider extends ServiceProvider
public function boot()
{
$this->registerPolicies();
$this->bootIntuitSocialite();
Passport::routes();
// Passport::enableImplicitGrant();