-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plugin.php
190 lines (172 loc) · 6.55 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Winter\SSO;
use Backend\Facades\Backend;
use Backend\Models\User;
use Backend\Models\UserRole;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\SocialiteServiceProvider;
use System\Classes\PluginBase;
use System\Classes\SettingsManager;
use Winter\Storm\Exception\ApplicationException;
use Winter\Storm\Support\Facades\Config;
use Winter\Storm\Support\Facades\Event;
/**
* SSO Plugin Information File
* @TODO:
* - Add backend DB configuration for providers (and all settings)
* - Add backend configuration for the user to set their SSO integrations
*/
class Plugin extends PluginBase
{
/**
* Flag that allows this plugin to run on protected routes, required to extend the auth controller.
*/
public $elevated = true;
/**
* Returns information about this plugin.
*/
public function pluginDetails(): array
{
return [
'name' => 'winter.sso::lang.plugin.name',
'description' => 'winter.sso::lang.plugin.description',
'author' => 'Winter CMS',
'icon' => 'icon-lock',
];
}
/**
* Returns the permissions provided by this plugin
*/
public function registerPermissions(): array
{
return [
'winter.sso.view_logs' => [
'label' => 'winter.sso::lang.permissions.view_logs',
'tab' => 'winter.sso::lang.plugin.name',
'roles' => [UserRole::CODE_DEVELOPER],
],
];
}
/**
* Returns the settings provided by this plugin
*/
public function registerSettings(): array
{
return [
'logs' => [
'label' => 'winter.sso::lang.models.log.label_plural',
'description' => 'winter.sso::lang.models.log.menu_description',
'icon' => 'icon-key',
'url' => Backend::url('winter/sso/logs'),
'permissions' => ['winter.sso.view_logs'],
'category' => SettingsManager::CATEGORY_LOGS,
],
];
}
/**
* Register method, called when the plugin is first registered.
*/
public function register(): void
{
$this->forceEmailLogin();
$this->registerSocialite();
}
/**
* Enforce the use of email addresses as the login attribute.
*/
protected function forceEmailLogin(): void
{
// Force email login attribute on SSO callback route
if (str_starts_with(Request::url(), Backend::url('winter/sso/handle/callback/'))) {
User::$loginAttribute = 'email';
}
User::extend(function ($model) {
$model->addDynamicMethod('getSsoValue', function (string $provider, mixed $key, $default = null) use ($model) {
return $model->metadata['winter.sso'][$provider][$key] ?? $default;
});
$model->addDynamicMethod('setSsoValues', function (string $provider, array $values) use ($model) {
$metadata = is_array($model->metadata) ? $model->metadata : [];
foreach ($values as $key => $value) {
$metadata['winter.sso'][$provider][$key] = $value;
}
$model->metadata = $metadata;
$model->save();
});
});
}
/**
* Boot method, called right before the request route.
*/
public function boot(): void
{
// Secure sessions with same_site set to strict prevents Socialite's SSO session data from being saved
// @TODO: Warn the user about this, perhaps in the system configuration warnings dashboard widget
if (Config::get('session.secure') === true && Config::get('session.same_site') === 'strict') {
Config::set('session.same_site', 'lax');
}
$this->configureProviders();
$this->extendAuthController();
}
/**
* Ensure the configuration for the providers is set.
*/
protected function configureProviders(): void
{
// Populate the services configuration with the socialite providers
$services = Config::get('services', []);
$providers = Config::get('winter.sso::providers', []);
$enabledProviders = Config::get('winter.sso::enabled_providers', []);
foreach ($providers as $provider => $config) {
if (
!in_array($provider, $enabledProviders)
|| empty($config['client_id'])
|| !empty($services[$provider]['client_id'])
) {
continue;
}
$config = array_merge([
'redirect' => Backend::url('winter/sso/handle/callback/' . $provider),
], $config);
// Set the service configuration for the provider
Config::set("services.{$provider}", $config);
}
}
/**
* Extend the auth controller to add the SSO login buttons.
*/
protected function extendAuthController(): void
{
// Extend the signin view to add the SSO buttons for the enabled providers
Event::listen('backend.auth.extendSigninView', function ($controller) {
$controller->addCss('/plugins/winter/sso/assets/dist/css/sso.css', 'Winter.SSO');
if ($view = View::make("winter.sso::providers", ['providers' => Config::get('winter.sso::enabled_providers', [])])) {
// save signin_url to redirect
Session::put('signin_url', Request::url());
echo $view;
}
});
if (Config::get('winter.sso::prevent_native_auth', false)) {
\Backend\Controllers\Auth::extend(function ($controller) {
// Disable the login form visually
$controller->addViewPath(plugins_path('winter/sso/controllers/auth/prevent_native'));
// Disable server processing of any auth AJAX handlers to protect against manually crafted requests
$controller->bindEvent('ajax.beforeRunHandler', function ($handler) {
if ($handler === 'onSubmit') {
throw new ApplicationException("Native authentication is disabled.");
}
});
});
}
}
/**
* Register the Socialite service provider.
*/
protected function registerSocialite(): void
{
$this->app->register(SocialiteServiceProvider::class);
$this->app->alias('Socialite', Socialite::class);
}
}