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