Add jwt handling, inspired by passport

This commit is contained in:
René Preuß
2021-03-30 23:19:36 +02:00
parent 32990da8a0
commit 14bf9d5480
14 changed files with 872 additions and 56 deletions

View File

@@ -4,7 +4,12 @@ declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Providers;
use GhostZero\BitinflowAccounts\Auth\TokenGuard;
use GhostZero\BitinflowAccounts\Auth\UserProvider;
use GhostZero\BitinflowAccounts\BitinflowAccounts;
use GhostZero\BitinflowAccounts\Helpers\JwtParser;
use Illuminate\Auth\RequestGuard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
class BitinflowAccountsServiceProvider extends ServiceProvider
@@ -33,6 +38,8 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
$this->app->singleton(BitinflowAccounts::class, function () {
return new BitinflowAccounts;
});
$this->registerGuard();
}
/**
@@ -43,4 +50,37 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
{
return [BitinflowAccounts::class];
}
/**
* Register the token guard.
*
* @return void
*/
protected function registerGuard()
{
Auth::resolved(function ($auth) {
$auth->extend('bitinflow-accounts', function ($app, $name, array $config) {
return tap($this->makeGuard($config), function ($guard) {
$this->app->refresh('request', $guard, 'setRequest');
});
});
});
}
/**
* Make an instance of the token guard.
*
* @param array $config
* @return RequestGuard
*/
protected function makeGuard(array $config): RequestGuard
{
return new RequestGuard(function ($request) use ($config) {
return (new TokenGuard(
new UserProvider(Auth::createUserProvider($config['provider']), $config['provider']),
$this->app->make('encrypter'),
$this->app->make(JwtParser::class)
))->user($request);
}, $this->app['request']);
}
}