1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
4
|
|
|
* @license https://flipboxfactory.com/software/saml-sp/license |
5
|
|
|
* @link https://www.flipboxfactory.com/software/saml-sp/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace flipbox\saml\sp; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\console\Application as ConsoleApplication; |
12
|
|
|
use craft\events\RegisterComponentTypesEvent; |
13
|
|
|
use craft\events\RegisterUrlRulesEvent; |
14
|
|
|
use craft\helpers\UrlHelper; |
15
|
|
|
use craft\services\Fields; |
16
|
|
|
use craft\web\UrlManager; |
17
|
|
|
use flipbox\saml\core\AbstractPlugin; |
18
|
|
|
use flipbox\saml\core\models\SettingsInterface; |
19
|
|
|
use flipbox\saml\core\SamlPluginInterface; |
20
|
|
|
use flipbox\saml\core\services\Session; |
21
|
|
|
use flipbox\saml\sp\fields\ExternalIdentity; |
22
|
|
|
use flipbox\saml\sp\models\Settings; |
23
|
|
|
use flipbox\saml\sp\records\ProviderIdentityRecord; |
24
|
|
|
use flipbox\saml\sp\records\ProviderRecord; |
25
|
|
|
use flipbox\saml\sp\services\bindings\Factory; |
26
|
|
|
use flipbox\saml\sp\services\bindings\HttpPost; |
27
|
|
|
use flipbox\saml\sp\services\bindings\HttpRedirect; |
28
|
|
|
use flipbox\saml\sp\services\Login; |
29
|
|
|
use flipbox\saml\sp\services\login\User; |
30
|
|
|
use flipbox\saml\sp\services\login\UserGroups; |
31
|
|
|
use flipbox\saml\sp\services\messages\AuthnRequest; |
32
|
|
|
use flipbox\saml\sp\services\messages\LogoutRequest; |
33
|
|
|
use flipbox\saml\sp\services\messages\LogoutResponse; |
34
|
|
|
use flipbox\saml\sp\services\messages\Metadata; |
35
|
|
|
use flipbox\saml\sp\services\messages\Response; |
36
|
|
|
use flipbox\saml\sp\services\Provider; |
37
|
|
|
use flipbox\saml\sp\services\ProviderIdentity; |
38
|
|
|
use yii\base\Event; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class Saml |
42
|
|
|
* @package flipbox\saml\sp |
43
|
|
|
*/ |
44
|
|
|
class Saml extends AbstractPlugin implements SamlPluginInterface |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function init() |
50
|
|
|
{ |
51
|
|
|
parent::init(); |
52
|
|
|
|
53
|
|
|
$this->initComponents(); |
54
|
|
|
$this->initEvents(); |
55
|
|
|
|
56
|
|
|
// Switch target to console controllers |
57
|
|
|
if (Craft::$app instanceof ConsoleApplication) { |
58
|
|
|
$this->controllerNamespace = __NAMESPACE__ . '\cli'; |
59
|
|
|
$this->controllerMap = [ |
60
|
|
|
'metadata' => \flipbox\saml\sp\cli\Metadata::class, |
61
|
|
|
'keychain' => \flipbox\saml\sp\cli\KeyChain::class, |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Events |
68
|
|
|
*/ |
69
|
|
|
protected function initEvents() |
70
|
|
|
{ |
71
|
|
|
/** |
72
|
|
|
* CP routes |
73
|
|
|
*/ |
74
|
|
|
Event::on( |
75
|
|
|
UrlManager::class, |
76
|
|
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
77
|
|
|
[self::class, 'onRegisterCpUrlRules'] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Clean Frontend Endpoints |
83
|
|
|
*/ |
84
|
|
|
Event::on( |
85
|
|
|
UrlManager::class, |
86
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
87
|
|
|
[static::class, 'onRegisterSiteUrlRules'] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
Event::on( |
91
|
|
|
Fields::class, |
92
|
|
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
93
|
|
|
function (RegisterComponentTypesEvent $event) { |
94
|
|
|
$event->types[] = ExternalIdentity::class; |
95
|
|
|
} |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Components |
101
|
|
|
*/ |
102
|
|
|
public function initComponents() |
103
|
|
|
{ |
104
|
|
|
$this->setComponents( |
105
|
|
|
[ |
106
|
|
|
'authnRequest' => AuthnRequest::class, |
107
|
|
|
'httpPost' => HttpPost::class, |
108
|
|
|
'httpRedirect' => HttpRedirect::class, |
109
|
|
|
'bindingFactory' => Factory::class, |
110
|
|
|
'login' => Login::class, |
111
|
|
|
'user' => User::class, |
112
|
|
|
'userGroups' => UserGroups::class, |
113
|
|
|
'logoutRequest' => LogoutRequest::class, |
114
|
|
|
'logoutResponse' => LogoutResponse::class, |
115
|
|
|
'provider' => Provider::class, |
116
|
|
|
'providerIdentity' => ProviderIdentity::class, |
117
|
|
|
'metadata' => Metadata::class, |
118
|
|
|
'response' => Response::class, |
119
|
|
|
'session' => Session::class, |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param RegisterUrlRulesEvent $event |
126
|
|
|
*/ |
127
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
128
|
|
|
{ |
129
|
|
|
$event->rules = array_merge( |
130
|
|
|
$event->rules, |
131
|
|
|
[ |
132
|
|
|
'saml-sp/' => 'saml-sp/cp/view/general/setup', |
133
|
|
|
'saml-sp/settings' => 'saml-sp/cp/view/general/settings', |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Keychain |
137
|
|
|
*/ |
138
|
|
|
'saml-sp/keychain' => 'saml-sp/cp/view/keychain/index', |
139
|
|
|
'saml-sp/keychain/new' => 'saml-sp/cp/view/keychain/edit', |
140
|
|
|
'saml-sp/keychain/new-openssl' => 'saml-sp/cp/view/keychain/edit/openssl', |
141
|
|
|
'saml-sp/keychain/<keypairId:\d+>' => 'saml-sp/cp/view/keychain/edit', |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Metadata |
145
|
|
|
*/ |
146
|
|
|
'saml-sp/metadata' => 'saml-sp/cp/view/metadata/default', |
147
|
|
|
'saml-sp/metadata/new' => 'saml-sp/cp/view/metadata/edit', |
148
|
|
|
'saml-sp/metadata/new-idp' => 'saml-sp/cp/view/metadata/edit/new-idp', |
149
|
|
|
'saml-sp/metadata/new-sp' => 'saml-sp/cp/view/metadata/edit/new-sp', |
150
|
|
|
'saml-sp/metadata/my-provider' => 'saml-sp/cp/view/metadata/edit/my-provider', |
151
|
|
|
'saml-sp/metadata/<providerId:\d+>' => 'saml-sp/cp/view/metadata/edit', |
152
|
|
|
], |
153
|
|
|
static::getInstance()->getSettings()->enableCpLoginButtons ? |
154
|
|
|
[ |
155
|
|
|
'login' => 'saml-sp/cp/view/login', |
156
|
|
|
] : [] |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
|
|
public function getSettingsResponse() |
164
|
|
|
{ |
165
|
|
|
|
166
|
|
|
Craft::$app->getResponse()->redirect( |
167
|
|
|
UrlHelper::cpUrl('saml-sp/settings') |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
Craft::$app->end(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param RegisterUrlRulesEvent $event |
175
|
|
|
*/ |
176
|
|
|
public static function onRegisterSiteUrlRules(RegisterUrlRulesEvent $event) |
177
|
|
|
{ |
178
|
|
|
$event->rules = array_merge( |
179
|
|
|
$event->rules, |
180
|
|
|
[ |
181
|
|
|
/** |
182
|
|
|
* LOGIN |
183
|
|
|
*/ |
184
|
|
|
'POST,GET /sso/login' => 'saml-sp/login', |
185
|
|
|
sprintf( |
186
|
|
|
'GET %s', |
187
|
|
|
(string)static::getInstance()->getSettings()->loginRequestEndpoint |
188
|
|
|
) => 'saml-sp/login/request', |
189
|
|
|
sprintf( |
190
|
|
|
'GET %s/<uid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}>', |
191
|
|
|
(string)static::getInstance()->getSettings()->loginRequestEndpoint |
192
|
|
|
) => 'saml-sp/login/request', |
193
|
|
|
/** |
194
|
|
|
* LOGOUT |
195
|
|
|
*/ |
196
|
|
|
'POST,GET /sso/logout' => 'saml-sp/logout', |
197
|
|
|
sprintf( |
198
|
|
|
'GET %s', |
199
|
|
|
(string)static::getInstance()->getSettings()->logoutRequestEndpoint |
200
|
|
|
) => 'saml-sp/logout/request', |
201
|
|
|
] |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return Settings |
208
|
|
|
*/ |
209
|
|
|
public function getSettings(): SettingsInterface |
210
|
|
|
{ |
211
|
|
|
return parent::getSettings(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @inheritdoc |
216
|
|
|
*/ |
217
|
|
|
public function createSettingsModel() |
218
|
|
|
{ |
219
|
|
|
return new Settings(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Components |
224
|
|
|
*/ |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
228
|
|
|
* @return AuthnRequest |
229
|
|
|
*/ |
230
|
|
|
public function getAuthnRequest() |
231
|
|
|
{ |
232
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
233
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
234
|
|
|
return $this->get('authnRequest'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
239
|
|
|
* @return Response |
240
|
|
|
*/ |
241
|
|
|
public function getResponse() |
242
|
|
|
{ |
243
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
244
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
245
|
|
|
return $this->get('response'); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
250
|
|
|
* @return Login |
251
|
|
|
*/ |
252
|
|
|
public function getLogin() |
253
|
|
|
{ |
254
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
255
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
256
|
|
|
return $this->get('login'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
261
|
|
|
* @return User |
262
|
|
|
*/ |
263
|
|
|
public function getUser() |
264
|
|
|
{ |
265
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
266
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
267
|
|
|
return $this->get('user'); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
272
|
|
|
* @return UserGroups |
273
|
|
|
*/ |
274
|
|
|
public function getUserGroups() |
275
|
|
|
{ |
276
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
277
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
278
|
|
|
return $this->get('userGroups'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
283
|
|
|
* @return Session |
284
|
|
|
* @throws \yii\base\InvalidConfigException |
285
|
|
|
*/ |
286
|
|
|
public function getSession() |
287
|
|
|
{ |
288
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
289
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
290
|
|
|
return $this->get('session'); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return Factory |
295
|
|
|
* @throws \yii\base\InvalidConfigException |
296
|
|
|
*/ |
297
|
|
|
public function getBindingFactory() |
298
|
|
|
{ |
299
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
300
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
301
|
|
|
return $this->get('bindingFactory'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Util Methods |
306
|
|
|
*/ |
307
|
|
|
|
308
|
|
|
public function getMyType() |
309
|
|
|
{ |
310
|
|
|
return static::SP; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function getProviderRecordClass() |
317
|
|
|
{ |
318
|
|
|
return ProviderRecord::class; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
public function getProviderIdentityRecordClass() |
325
|
|
|
{ |
326
|
|
|
return ProviderIdentityRecord::class; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|