2016-01-10 03:23:35 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\App\Providers;
|
2016-01-10 03:23:35 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Access\ExternalBaseUserProvider;
|
|
|
|
use BookStack\Access\Guards\AsyncExternalBaseSessionGuard;
|
|
|
|
use BookStack\Access\Guards\LdapSessionGuard;
|
|
|
|
use BookStack\Access\LdapService;
|
|
|
|
use BookStack\Access\LoginService;
|
|
|
|
use BookStack\Access\RegistrationService;
|
2019-12-30 22:51:28 +08:00
|
|
|
use BookStack\Api\ApiTokenGuard;
|
2023-09-16 20:18:35 +08:00
|
|
|
use BookStack\Users\Models\User;
|
2021-09-26 22:48:22 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-01-10 03:23:35 +08:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2021-12-19 00:31:48 +08:00
|
|
|
use Illuminate\Validation\Rules\Password;
|
2016-01-10 03:23:35 +08:00
|
|
|
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bootstrap the application services.
|
|
|
|
*/
|
2024-03-16 23:12:14 +08:00
|
|
|
public function boot(): void
|
2016-01-10 03:23:35 +08:00
|
|
|
{
|
2021-12-19 00:31:48 +08:00
|
|
|
// Password Configuration
|
2022-02-04 00:52:28 +08:00
|
|
|
// Changes here must be reflected in ApiDocsGenerate@getValidationAsString.
|
2022-09-27 09:48:05 +08:00
|
|
|
Password::defaults(fn () => Password::min(8));
|
2021-12-19 00:31:48 +08:00
|
|
|
|
|
|
|
// Custom guards
|
2019-12-30 22:51:28 +08:00
|
|
|
Auth::extend('api-token', function ($app, $name, array $config) {
|
2021-08-06 05:07:08 +08:00
|
|
|
return new ApiTokenGuard($app['request'], $app->make(LoginService::class));
|
2019-12-30 22:51:28 +08:00
|
|
|
});
|
2020-02-01 19:42:22 +08:00
|
|
|
|
|
|
|
Auth::extend('ldap-session', function ($app, $name, array $config) {
|
|
|
|
$provider = Auth::createUserProvider($config['provider']);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2020-02-01 19:42:22 +08:00
|
|
|
return new LdapSessionGuard(
|
|
|
|
$name,
|
|
|
|
$provider,
|
2021-08-06 05:07:08 +08:00
|
|
|
$app['session.store'],
|
2020-02-01 19:42:22 +08:00
|
|
|
$app[LdapService::class],
|
2020-02-03 01:31:00 +08:00
|
|
|
$app[RegistrationService::class]
|
2020-02-01 19:42:22 +08:00
|
|
|
);
|
|
|
|
});
|
2020-02-02 18:59:03 +08:00
|
|
|
|
2021-10-07 06:05:26 +08:00
|
|
|
Auth::extend('async-external-session', function ($app, $name, array $config) {
|
2020-02-02 18:59:03 +08:00
|
|
|
$provider = Auth::createUserProvider($config['provider']);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2021-10-07 06:05:26 +08:00
|
|
|
return new AsyncExternalBaseSessionGuard(
|
2020-02-02 18:59:03 +08:00
|
|
|
$name,
|
|
|
|
$provider,
|
2021-08-06 05:07:08 +08:00
|
|
|
$app['session.store'],
|
2020-02-03 01:31:00 +08:00
|
|
|
$app[RegistrationService::class]
|
2020-02-02 18:59:03 +08:00
|
|
|
);
|
|
|
|
});
|
2016-01-10 03:23:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the application services.
|
|
|
|
*/
|
2024-03-16 23:12:14 +08:00
|
|
|
public function register(): void
|
2016-01-10 03:23:35 +08:00
|
|
|
{
|
2020-02-01 19:42:22 +08:00
|
|
|
Auth::provider('external-users', function ($app, array $config) {
|
|
|
|
return new ExternalBaseUserProvider($config['model']);
|
2016-01-10 03:23:35 +08:00
|
|
|
});
|
2023-09-16 20:18:35 +08:00
|
|
|
|
|
|
|
// Bind and provide the default system user as a singleton to the app instance when needed.
|
|
|
|
// This effectively "caches" fetching the user at an app-instance level.
|
|
|
|
$this->app->singleton('users.default', function () {
|
|
|
|
return User::query()->where('system_name', '=', 'public')->first();
|
|
|
|
});
|
2016-01-10 03:23:35 +08:00
|
|
|
}
|
|
|
|
}
|