]> BookStack Code Mirror - bookstack/blob - app/Access/SocialDriverManager.php
PHP: Addressed 8.4 deprecations within app itself
[bookstack] / app / Access / SocialDriverManager.php
1 <?php
2
3 namespace BookStack\Access;
4
5 use BookStack\Exceptions\SocialDriverNotConfigured;
6 use Illuminate\Support\Facades\Event;
7 use Illuminate\Support\Str;
8 use SocialiteProviders\Manager\SocialiteWasCalled;
9
10 class SocialDriverManager
11 {
12     /**
13      * The default built-in social drivers we support.
14      *
15      * @var string[]
16      */
17     protected array $validDrivers = [
18         'google',
19         'github',
20         'facebook',
21         'slack',
22         'twitter',
23         'azure',
24         'okta',
25         'gitlab',
26         'twitch',
27         'discord',
28     ];
29
30     /**
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.
35      *
36      * @var array<string, callable>
37      */
38     protected array $configureForRedirectCallbacks = [];
39
40     /**
41      * Check if the current config for the given driver allows auto-registration.
42      */
43     public function isAutoRegisterEnabled(string $driver): bool
44     {
45         return $this->getDriverConfigProperty($driver, 'auto_register') === true;
46     }
47
48     /**
49      * Check if the current config for the given driver allow email address auto-confirmation.
50      */
51     public function isAutoConfirmEmailEnabled(string $driver): bool
52     {
53         return $this->getDriverConfigProperty($driver, 'auto_confirm') === true;
54     }
55
56     /**
57      * Gets the names of the active social drivers, keyed by driver id.
58      * @returns array<string, string>
59      */
60     public function getActive(): array
61     {
62         $activeDrivers = [];
63
64         foreach ($this->validDrivers as $driverKey) {
65             if ($this->checkDriverConfigured($driverKey)) {
66                 $activeDrivers[$driverKey] = $this->getName($driverKey);
67             }
68         }
69
70         return $activeDrivers;
71     }
72
73     /**
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.
78      */
79     public function getConfigureForRedirectCallback(string $driver): callable
80     {
81         return $this->configureForRedirectCallbacks[$driver] ?? (fn() => true);
82     }
83
84     /**
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.
90      */
91     public function addSocialDriver(
92         string $driverName,
93         array $config,
94         string $socialiteHandler,
95         ?callable $configureForRedirect = null
96     ) {
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;
104         }
105     }
106
107     /**
108      * Get the presentational name for a driver.
109      */
110     protected function getName(string $driver): string
111     {
112         return $this->getDriverConfigProperty($driver, 'name') ?? '';
113     }
114
115     protected function getDriverConfigProperty(string $driver, string $property): mixed
116     {
117         return config("services.{$driver}.{$property}");
118     }
119
120     /**
121      * Ensure the social driver is correct and supported.
122      *
123      * @throws SocialDriverNotConfigured
124      */
125     public function ensureDriverActive(string $driverName): void
126     {
127         if (!in_array($driverName, $this->validDrivers)) {
128             abort(404, trans('errors.social_driver_not_found'));
129         }
130
131         if (!$this->checkDriverConfigured($driverName)) {
132             throw new SocialDriverNotConfigured(trans('errors.social_driver_not_configured', ['socialAccount' => Str::title($driverName)]));
133         }
134     }
135
136     /**
137      * Check a social driver has been configured correctly.
138      */
139     protected function checkDriverConfigured(string $driver): bool
140     {
141         $lowerName = strtolower($driver);
142         $configPrefix = 'services.' . $lowerName . '.';
143         $config = [config($configPrefix . 'client_id'), config($configPrefix . 'client_secret'), config('services.callback_url')];
144
145         return !in_array(false, $config) && !in_array(null, $config);
146     }
147 }