|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: dsmrt |
|
5
|
|
|
* Date: 1/9/18 |
|
6
|
|
|
* Time: 9:11 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\saml\idp; |
|
10
|
|
|
|
|
11
|
|
|
use craft\events\RegisterComponentTypesEvent; |
|
12
|
|
|
use craft\events\UserGroupEvent; |
|
13
|
|
|
use craft\models\UserGroup; |
|
14
|
|
|
use craft\services\Fields; |
|
15
|
|
|
use craft\services\UserGroups; |
|
16
|
|
|
use craft\web\UrlManager; |
|
17
|
|
|
use craft\web\User; |
|
18
|
|
|
use flipbox\saml\core\AbstractPlugin; |
|
19
|
|
|
use flipbox\saml\core\containers\Saml2Container; |
|
20
|
|
|
use flipbox\saml\core\models\SettingsInterface; |
|
21
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
|
22
|
|
|
use flipbox\saml\idp\fields\ExternalIdentity; |
|
23
|
|
|
use flipbox\saml\idp\models\Settings; |
|
24
|
|
|
use flipbox\saml\idp\records\ProviderIdentityRecord; |
|
25
|
|
|
use flipbox\saml\idp\records\ProviderRecord; |
|
26
|
|
|
use flipbox\saml\idp\services\messages\AuthnRequest; |
|
27
|
|
|
use flipbox\saml\idp\services\messages\Response; |
|
28
|
|
|
use flipbox\saml\idp\services\messages\ResponseAssertion; |
|
29
|
|
|
use flipbox\saml\idp\services\Provider; |
|
30
|
|
|
use flipbox\saml\idp\services\ProviderIdentity; |
|
31
|
|
|
use flipbox\saml\idp\services\Session; |
|
32
|
|
|
use SAML2\Compat\AbstractContainer; |
|
33
|
|
|
use yii\base\Event; |
|
34
|
|
|
|
|
35
|
|
|
class Saml extends AbstractPlugin |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
38 |
|
public function init() |
|
39
|
|
|
{ |
|
40
|
38 |
|
parent::init(); |
|
41
|
|
|
|
|
42
|
38 |
|
$this->initCore(); |
|
43
|
38 |
|
$this->initComponents(); |
|
44
|
38 |
|
$this->initEvents(); |
|
45
|
38 |
|
} |
|
46
|
|
|
|
|
47
|
38 |
|
public function initComponents() |
|
48
|
|
|
{ |
|
49
|
38 |
|
$this->setComponents([ |
|
50
|
38 |
|
'authnRequest' => AuthnRequest::class, |
|
51
|
|
|
'provider' => Provider::class, |
|
52
|
|
|
'providerIdentity' => ProviderIdentity::class, |
|
53
|
|
|
'response' => Response::class, |
|
54
|
|
|
'responseAssertion' => ResponseAssertion::class, |
|
55
|
|
|
'session' => Session::class, |
|
56
|
|
|
]); |
|
57
|
38 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Init events |
|
61
|
|
|
*/ |
|
62
|
38 |
|
public function initEvents() |
|
63
|
|
|
{ |
|
64
|
|
|
/** |
|
65
|
|
|
* After login |
|
66
|
|
|
*/ |
|
67
|
38 |
|
Event::on( |
|
68
|
38 |
|
User::class, |
|
69
|
38 |
|
User::EVENT_AFTER_LOGIN, |
|
70
|
|
|
[ |
|
71
|
38 |
|
$this->getResponse(), |
|
72
|
38 |
|
'createAndSendFromSession', |
|
73
|
|
|
] |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* CP routes |
|
78
|
|
|
*/ |
|
79
|
38 |
|
Event::on( |
|
80
|
38 |
|
UrlManager::class, |
|
81
|
38 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
|
82
|
38 |
|
[self::class, 'onRegisterCpUrlRules'] |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Clean Frontend Endpoints |
|
87
|
|
|
*/ |
|
88
|
38 |
|
Event::on( |
|
89
|
38 |
|
UrlManager::class, |
|
90
|
38 |
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
|
91
|
38 |
|
[static::class, 'onRegisterSiteUrlRules'] |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
38 |
|
Event::on( |
|
95
|
38 |
|
Fields::class, |
|
96
|
38 |
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
|
97
|
|
|
function (RegisterComponentTypesEvent $event) { |
|
98
|
|
|
$event->types[] = ExternalIdentity::class; |
|
99
|
38 |
|
} |
|
100
|
|
|
); |
|
101
|
38 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Settings |
|
105
|
|
|
*/ |
|
106
|
16 |
|
public function getSettings(): SettingsInterface |
|
107
|
|
|
{ |
|
108
|
16 |
|
return parent::getSettings(); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @inheritdoc |
|
113
|
|
|
*/ |
|
114
|
18 |
|
public function createSettingsModel() |
|
115
|
|
|
{ |
|
116
|
18 |
|
return new Settings([ |
|
117
|
18 |
|
'myType' => SettingsInterface::IDP, |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Components |
|
123
|
|
|
*/ |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return AuthnRequest |
|
127
|
|
|
*/ |
|
128
|
4 |
|
public function getAuthnRequest() |
|
129
|
|
|
{ |
|
130
|
4 |
|
return $this->get('authnRequest'); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return Response |
|
135
|
|
|
*/ |
|
136
|
38 |
|
public function getResponse() |
|
137
|
|
|
{ |
|
138
|
38 |
|
return $this->get('response'); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return ResponseAssertion |
|
143
|
|
|
*/ |
|
144
|
4 |
|
public function getResponseAssertion() |
|
145
|
|
|
{ |
|
146
|
4 |
|
return $this->get('responseAssertion'); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return Session |
|
151
|
|
|
*/ |
|
152
|
10 |
|
public function getSession() |
|
153
|
|
|
{ |
|
154
|
10 |
|
return $this->get('session'); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Util Methods |
|
159
|
|
|
*/ |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
10 |
|
public function getProviderRecordClass() |
|
165
|
|
|
{ |
|
166
|
10 |
|
return ProviderRecord::class; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
2 |
|
public function getProviderIdentityRecordClass() |
|
173
|
|
|
{ |
|
174
|
2 |
|
return ProviderIdentityRecord::class; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return Saml2Container |
|
179
|
|
|
*/ |
|
180
|
4 |
|
public function loadSaml2Container(): AbstractContainer |
|
181
|
|
|
{ |
|
182
|
4 |
|
$container = new Saml2Container($this); |
|
183
|
|
|
|
|
184
|
4 |
|
\SAML2\Compat\ContainerSingleton::setContainer($container); |
|
185
|
|
|
|
|
186
|
4 |
|
return $container; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|