2019-05-20 07:37:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2022-03-19 00:14:20 +01:00
|
|
|
use App\Extensions\RemoteUserProvider;
|
2022-11-14 17:13:24 +01:00
|
|
|
use App\Extensions\WebauthnCredentialBroker;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Facades\Settings;
|
2023-02-22 20:21:36 +01:00
|
|
|
use App\Models\TwoFAccount;
|
|
|
|
use App\Policies\TwoFAccountPolicy;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Services\Auth\ReverseProxyGuard;
|
2022-11-14 17:13:24 +01:00
|
|
|
use Illuminate\Auth\Passwords\DatabaseTokenRepository;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2022-11-14 17:13:24 +01:00
|
|
|
use Illuminate\Support\Str;
|
2022-11-22 15:15:52 +01:00
|
|
|
use RuntimeException;
|
2019-05-20 07:37:41 +02:00
|
|
|
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
2022-11-14 17:13:24 +01:00
|
|
|
* The model to policy mappings for the application.
|
2019-05-20 07:37:41 +02:00
|
|
|
*
|
2022-11-14 17:13:24 +01:00
|
|
|
* @var array<class-string, class-string>
|
2019-05-20 07:37:41 +02:00
|
|
|
*/
|
2022-11-14 17:13:24 +01:00
|
|
|
protected $policies = [
|
2023-02-22 20:21:36 +01:00
|
|
|
TwoFAccount::class => TwoFAccountPolicy::class,
|
2022-11-14 17:13:24 +01:00
|
|
|
];
|
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
/**
|
2022-11-14 17:13:24 +01:00
|
|
|
* Register the service provider.
|
2019-05-20 07:37:41 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2022-11-14 17:13:24 +01:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2019-05-20 07:37:41 +02:00
|
|
|
*/
|
2022-11-22 15:15:52 +01:00
|
|
|
public function register() : void
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2022-11-14 17:13:24 +01:00
|
|
|
$this->app->singleton(
|
|
|
|
WebauthnCredentialBroker::class,
|
|
|
|
static function ($app) {
|
2022-11-22 15:15:52 +01:00
|
|
|
if (! $config = $app['config']['auth.passwords.webauthn']) {
|
2022-11-14 17:13:24 +01:00
|
|
|
throw new RuntimeException('You must set the [webauthn] key broker in [auth] config.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $app['config']['app.key'];
|
|
|
|
|
|
|
|
if (Str::startsWith($key, 'base64:')) {
|
|
|
|
$key = base64_decode(substr($key, 7));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WebauthnCredentialBroker(
|
|
|
|
new DatabaseTokenRepository(
|
|
|
|
$app['db']->connection($config['connection'] ?? null),
|
|
|
|
$app['hash'],
|
|
|
|
$config['table'],
|
|
|
|
$key,
|
|
|
|
$config['expire'],
|
|
|
|
$config['throttle'] ?? 0
|
|
|
|
),
|
|
|
|
$app['auth']->createUserProvider($config['provider'] ?? null)
|
2022-03-15 14:47:07 +01:00
|
|
|
);
|
2022-03-24 14:58:30 +01:00
|
|
|
}
|
|
|
|
);
|
2022-11-14 17:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register any authentication / authorization services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
|
|
|
$this->registerPolicies();
|
2022-03-15 14:47:07 +01:00
|
|
|
|
2022-03-24 14:58:30 +01:00
|
|
|
// Register a custom provider for reverse-proxy authentication
|
2022-03-19 00:14:20 +01:00
|
|
|
Auth::provider('remote-user', function ($app, array $config) {
|
|
|
|
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2022-03-19 00:14:20 +01:00
|
|
|
return new RemoteUserProvider;
|
|
|
|
});
|
|
|
|
|
2022-03-24 14:58:30 +01:00
|
|
|
// Register a custom driver for reverse-proxy authentication
|
2022-11-22 15:15:52 +01:00
|
|
|
Auth::extend('reverse-proxy', function ($app, string $name, array $config) {
|
2022-03-19 00:14:20 +01:00
|
|
|
// Return an instance of Illuminate\Contracts\Auth\Guard...
|
|
|
|
|
|
|
|
return new ReverseProxyGuard(Auth::createUserProvider($config['provider']));
|
|
|
|
});
|
|
|
|
|
2022-11-14 17:13:24 +01:00
|
|
|
// Previously we were using a custom user provider derived from the Larapass user provider
|
|
|
|
// in order to honor the "useWebauthnOnly" user option.
|
|
|
|
// Since Laragear\WebAuthn now replaces DarkGhostHunter\Larapass, the new approach is
|
|
|
|
// simplier: We overload the 'eloquent-webauthn' registration from Laragear\WebAuthn\WebAuthnServiceProvider
|
|
|
|
// with a custom closure that uses the "useWebauthnOnly" user option
|
|
|
|
Auth::provider(
|
|
|
|
'eloquent-webauthn',
|
2022-11-22 15:15:52 +01:00
|
|
|
static function (\Illuminate\Contracts\Foundation\Application $app, array $config) : \Laragear\WebAuthn\Auth\WebAuthnUserProvider {
|
2022-11-14 17:13:24 +01:00
|
|
|
return new \Laragear\WebAuthn\Auth\WebAuthnUserProvider(
|
|
|
|
$app->make('hash'),
|
|
|
|
$config['model'],
|
|
|
|
$app->make(\Laragear\WebAuthn\Assertion\Validator\AssertionValidator::class),
|
|
|
|
Settings::get('useWebauthnOnly') ? false : true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-10-22 14:18:13 +02:00
|
|
|
// Normally we should set the Passport routes here using Passport::routes().
|
|
|
|
// If so the passport routes would be set for both 'web' and 'api' middlewares without
|
|
|
|
// possibility to exclude the web middleware (we can only pass additional middlewares to Passport::routes())
|
2022-11-22 15:15:52 +01:00
|
|
|
//
|
2021-10-22 14:18:13 +02:00
|
|
|
// The problem is that 2Fauth front-end uses the Laravel FreshApiToken to consum its API as a first party app.
|
|
|
|
// So we have a laravel_token cookie added to each response to perform the authentication.
|
2019-05-20 07:37:41 +02:00
|
|
|
//
|
2021-10-22 14:18:13 +02:00
|
|
|
// Don't know why but when passing through the web middleware the requests to Personal Access Tokens management routes return
|
|
|
|
// responses with inconsistent cookies that make the next request unauthorized.
|
|
|
|
// To avoid this the Passport routes for PAT management are set in the /routes/api.php file
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
}
|