3 namespace BookStack\Access;
5 use BookStack\Exceptions\SocialDriverNotConfigured;
6 use Illuminate\Support\Facades\Event;
7 use Illuminate\Support\Str;
8 use SocialiteProviders\Manager\SocialiteWasCalled;
10 class SocialDriverManager
13 * The default built-in social drivers we support.
17 protected array $validDrivers = [
31 * Callbacks to run when configuring a social driver
32 * for an initial redirect action.
33 * Array is keyed by social driver name.
34 * Callbacks are passed an instance of the driver.
36 * @var array<string, callable>
38 protected array $configureForRedirectCallbacks = [];
41 * Check if the current config for the given driver allows auto-registration.
43 public function isAutoRegisterEnabled(string $driver): bool
45 return $this->getDriverConfigProperty($driver, 'auto_register') === true;
49 * Check if the current config for the given driver allow email address auto-confirmation.
51 public function isAutoConfirmEmailEnabled(string $driver): bool
53 return $this->getDriverConfigProperty($driver, 'auto_confirm') === true;
57 * Gets the names of the active social drivers, keyed by driver id.
58 * @returns array<string, string>
60 public function getActive(): array
64 foreach ($this->validDrivers as $driverKey) {
65 if ($this->checkDriverConfigured($driverKey)) {
66 $activeDrivers[$driverKey] = $this->getName($driverKey);
70 return $activeDrivers;
74 * Get the configure-for-redirect callback for the given driver.
75 * This is a callable that allows modification of the driver at redirect time.
76 * Commonly used to perform custom dynamic configuration where required.
77 * The callback is passed a \Laravel\Socialite\Contracts\Provider instance.
79 public function getConfigureForRedirectCallback(string $driver): callable
81 return $this->configureForRedirectCallbacks[$driver] ?? (fn() => true);
85 * Add a custom socialite driver to be used.
86 * Driver name should be lower_snake_case.
87 * Config array should mirror the structure of a service
88 * within the `Config/services.php` file.
89 * Handler should be a Class@method handler to the SocialiteWasCalled event.
91 public function addSocialDriver(
94 string $socialiteHandler,
95 ?callable $configureForRedirect = null
97 $this->validDrivers[] = $driverName;
98 config()->set('services.' . $driverName, $config);
99 config()->set('services.' . $driverName . '.redirect', url('/login/service/' . $driverName . '/callback'));
100 config()->set('services.' . $driverName . '.name', $config['name'] ?? $driverName);
101 Event::listen(SocialiteWasCalled::class, $socialiteHandler);
102 if (!is_null($configureForRedirect)) {
103 $this->configureForRedirectCallbacks[$driverName] = $configureForRedirect;
108 * Get the presentational name for a driver.
110 protected function getName(string $driver): string
112 return $this->getDriverConfigProperty($driver, 'name') ?? '';
115 protected function getDriverConfigProperty(string $driver, string $property): mixed
117 return config("services.{$driver}.{$property}");
121 * Ensure the social driver is correct and supported.
123 * @throws SocialDriverNotConfigured
125 public function ensureDriverActive(string $driverName): void
127 if (!in_array($driverName, $this->validDrivers)) {
128 abort(404, trans('errors.social_driver_not_found'));
131 if (!$this->checkDriverConfigured($driverName)) {
132 throw new SocialDriverNotConfigured(trans('errors.social_driver_not_configured', ['socialAccount' => Str::title($driverName)]));
137 * Check a social driver has been configured correctly.
139 protected function checkDriverConfigured(string $driver): bool
141 $lowerName = strtolower($driver);
142 $configPrefix = 'services.' . $lowerName . '.';
143 $config = [config($configPrefix . 'client_id'), config($configPrefix . 'client_secret'), config('services.callback_url')];
145 return !in_array(false, $config) && !in_array(null, $config);