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\console\Application as ConsoleApplication; |
11
|
|
|
use craft\events\RegisterComponentTypesEvent; |
12
|
|
|
use craft\events\RegisterUrlRulesEvent; |
13
|
|
|
use craft\services\Fields; |
14
|
|
|
use craft\web\UrlManager; |
15
|
|
|
use flipbox\saml\core\AbstractPlugin; |
16
|
|
|
use flipbox\saml\core\containers\Saml2Container; |
17
|
|
|
use flipbox\saml\core\models\SettingsInterface; |
18
|
|
|
use flipbox\saml\core\services\Session; |
19
|
|
|
use flipbox\saml\sp\fields\ExternalIdentity; |
20
|
|
|
use flipbox\saml\sp\models\Settings; |
21
|
|
|
use flipbox\saml\sp\records\ProviderIdentityRecord; |
22
|
|
|
use flipbox\saml\sp\records\ProviderRecord; |
23
|
|
|
use flipbox\saml\sp\services\Login; |
24
|
|
|
use flipbox\saml\sp\services\login\User; |
25
|
|
|
use flipbox\saml\sp\services\login\UserGroups; |
26
|
|
|
use flipbox\saml\sp\services\messages\AuthnRequest; |
27
|
|
|
use flipbox\saml\sp\services\Provider; |
28
|
|
|
use flipbox\saml\sp\services\ProviderIdentity; |
29
|
|
|
use SAML2\Compat\AbstractContainer; |
30
|
|
|
use yii\base\Event; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class Saml |
34
|
|
|
* @package flipbox\saml\sp |
35
|
|
|
*/ |
36
|
|
|
class Saml extends AbstractPlugin |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
75 |
|
public function init() |
43
|
|
|
{ |
44
|
75 |
|
parent::init(); |
45
|
|
|
|
46
|
75 |
|
$this->initComponents(); |
47
|
75 |
|
$this->initEvents(); |
48
|
|
|
|
49
|
|
|
// Switch target to console controllers |
50
|
75 |
|
if (\Craft::$app instanceof ConsoleApplication) { |
51
|
|
|
$this->controllerNamespace = __NAMESPACE__ . '\commands'; |
52
|
|
|
} |
53
|
75 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Events |
57
|
|
|
*/ |
58
|
75 |
|
protected function initEvents() |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* CP routes |
62
|
|
|
*/ |
63
|
75 |
|
Event::on( |
64
|
75 |
|
UrlManager::class, |
65
|
75 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
66
|
75 |
|
[self::class, 'onRegisterCpUrlRules'] |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Clean Frontend Endpoints |
71
|
|
|
*/ |
72
|
75 |
|
Event::on( |
73
|
75 |
|
UrlManager::class, |
74
|
75 |
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
75
|
75 |
|
[static::class, 'onRegisterSiteUrlRules'] |
76
|
|
|
); |
77
|
|
|
|
78
|
75 |
|
Event::on( |
79
|
75 |
|
Fields::class, |
80
|
75 |
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
81
|
|
|
function (RegisterComponentTypesEvent $event) { |
82
|
|
|
$event->types[] = ExternalIdentity::class; |
83
|
75 |
|
} |
84
|
|
|
); |
85
|
75 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Components |
89
|
|
|
*/ |
90
|
75 |
|
public function initComponents() |
91
|
|
|
{ |
92
|
75 |
|
$this->setComponents( |
93
|
|
|
[ |
94
|
75 |
|
'authnRequest' => AuthnRequest::class, |
95
|
|
|
'login' => Login::class, |
96
|
|
|
'user' => User::class, |
97
|
|
|
'userGroups' => UserGroups::class, |
98
|
|
|
'provider' => Provider::class, |
99
|
|
|
'providerIdentity' => ProviderIdentity::class, |
100
|
|
|
'session' => Session::class, |
101
|
|
|
] |
102
|
|
|
); |
103
|
75 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param RegisterUrlRulesEvent $event |
107
|
|
|
*/ |
108
|
3 |
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
109
|
|
|
{ |
110
|
3 |
|
if (\Craft::$app->getIsLive()) { |
111
|
3 |
|
$event->rules = array_merge( |
112
|
3 |
|
$event->rules, |
113
|
3 |
|
static::getInstance()->getSettings()->enableCpLoginButtons ? |
114
|
|
|
[ |
115
|
3 |
|
'login' => 'saml-sp/cp/view/login', |
116
|
3 |
|
] : [] |
117
|
|
|
); |
118
|
|
|
} |
119
|
3 |
|
parent::onRegisterCpUrlRules($event); |
120
|
3 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return Settings |
124
|
|
|
*/ |
125
|
42 |
|
public function getSettings(): SettingsInterface |
126
|
|
|
{ |
127
|
42 |
|
return parent::getSettings(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
42 |
|
protected function createSettingsModel() |
134
|
|
|
{ |
135
|
42 |
|
return new Settings([ |
136
|
42 |
|
'myType' => Settings::SP, |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Components |
142
|
|
|
*/ |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
146
|
|
|
* @return Provider |
147
|
|
|
*/ |
148
|
30 |
|
public function getProvider() |
149
|
|
|
{ |
150
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
151
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
152
|
30 |
|
return $this->get('provider'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
157
|
|
|
* @return ProviderIdentity |
158
|
|
|
*/ |
159
|
9 |
|
public function getProviderIdentity() |
160
|
|
|
{ |
161
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
162
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
163
|
9 |
|
return $this->get('providerIdentity'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
168
|
|
|
* @return AuthnRequest |
169
|
|
|
*/ |
170
|
12 |
|
public function getAuthnRequest() |
171
|
|
|
{ |
172
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
173
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
174
|
12 |
|
return $this->get('authnRequest'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
179
|
|
|
* @return Login |
180
|
|
|
*/ |
181
|
6 |
|
public function getLogin() |
182
|
|
|
{ |
183
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
184
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
185
|
6 |
|
return $this->get('login'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
190
|
|
|
* @return User |
191
|
|
|
*/ |
192
|
12 |
|
public function getUser() |
193
|
|
|
{ |
194
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
195
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
196
|
12 |
|
return $this->get('user'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
201
|
|
|
* @return UserGroups |
202
|
|
|
*/ |
203
|
9 |
|
public function getUserGroups() |
204
|
|
|
{ |
205
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
206
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
207
|
9 |
|
return $this->get('userGroups'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
212
|
|
|
* @return Session |
213
|
|
|
* @throws \yii\base\InvalidConfigException |
214
|
|
|
*/ |
215
|
6 |
|
public function getSession() |
216
|
|
|
{ |
217
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
218
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
219
|
6 |
|
return $this->get('session'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Util Methods |
224
|
|
|
*/ |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return Saml2Container |
228
|
|
|
*/ |
229
|
18 |
|
public function loadSaml2Container(): AbstractContainer |
230
|
|
|
{ |
231
|
18 |
|
$container = new Saml2Container($this); |
232
|
|
|
|
233
|
18 |
|
\SAML2\Compat\ContainerSingleton::setContainer($container); |
234
|
|
|
|
235
|
18 |
|
return $container; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
33 |
|
public function getProviderRecordClass() |
242
|
|
|
{ |
243
|
33 |
|
return ProviderRecord::class; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
6 |
|
public function getProviderIdentityRecordClass() |
250
|
|
|
{ |
251
|
6 |
|
return ProviderIdentityRecord::class; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|