|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: dsmrt |
|
5
|
|
|
* Date: 1/10/18 |
|
6
|
|
|
* Time: 11:52 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\saml\idp\controllers; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\UrlHelper; |
|
13
|
|
|
use flipbox\saml\core\controllers\messages\AbstractController; |
|
14
|
|
|
use flipbox\saml\core\exceptions\InvalidMessage; |
|
15
|
|
|
use flipbox\saml\core\helpers\MessageHelper; |
|
16
|
|
|
use flipbox\saml\core\helpers\SerializeHelper; |
|
17
|
|
|
use flipbox\saml\core\services\bindings\Factory; |
|
18
|
|
|
use flipbox\saml\idp\records\ProviderRecord; |
|
19
|
|
|
use flipbox\saml\idp\Saml; |
|
20
|
|
|
use flipbox\saml\idp\traits\SamlPluginEnsured; |
|
21
|
|
|
use SAML2\AuthnRequest; |
|
22
|
|
|
use flipbox\saml\core\exceptions\InvalidMetadata; |
|
23
|
|
|
use yii\web\HttpException; |
|
24
|
|
|
|
|
25
|
|
|
class LoginController extends AbstractController |
|
26
|
|
|
{ |
|
27
|
|
|
use SamlPluginEnsured; |
|
28
|
|
|
|
|
29
|
|
|
protected $allowAnonymous = [ |
|
30
|
|
|
'actionIndex', |
|
31
|
|
|
'actionRequest', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
public $enableCsrfValidation = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \yii\base\Action $action |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function beforeAction($action) |
|
41
|
|
|
{ |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @throws \flipbox\saml\core\exceptions\InvalidMessage |
|
47
|
|
|
* @throws \flipbox\saml\core\exceptions\InvalidMetadata |
|
48
|
|
|
*/ |
|
49
|
|
|
public function actionIndex() |
|
50
|
|
|
{ |
|
51
|
|
|
|
|
52
|
|
|
/** @var AuthnRequest $authnRequest */ |
|
53
|
|
|
$authnRequest = Factory::receive(); |
|
54
|
|
|
|
|
55
|
|
|
/** @var ProviderRecord $serviceProvider */ |
|
56
|
|
|
$serviceProvider = Saml::getInstance()->getProvider()->findByEntityId( |
|
57
|
|
|
MessageHelper::getIssuer($authnRequest->getIssuer()) |
|
58
|
|
|
)->one(); |
|
59
|
|
|
|
|
60
|
|
|
if (is_null($serviceProvider)) { |
|
61
|
|
|
throw new InvalidMessage("Invalid Issuer."); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
Saml::getInstance()->getAuthnRequest()->isValid($authnRequest, $serviceProvider); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check relay state |
|
68
|
|
|
*/ |
|
69
|
|
|
|
|
70
|
|
|
if ($relayState = $this->getRelayState()) { |
|
71
|
|
|
$authnRequest->setRelayState($relayState); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($user = Craft::$app->getUser()->getIdentity()) { |
|
|
|
|
|
|
75
|
|
|
$identityProvider = Saml::getInstance()->getProvider()->findOwn(); |
|
76
|
|
|
|
|
77
|
|
|
//create response and send back to the sp |
|
78
|
|
|
$response = Saml::getInstance()->getResponse()->create( |
|
79
|
|
|
$user, |
|
80
|
|
|
$identityProvider, |
|
81
|
|
|
$serviceProvider, |
|
82
|
|
|
Saml::getInstance()->getSettings(), |
|
83
|
|
|
$authnRequest |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
Saml::getInstance()->getResponse()->finalizeWithAuthnRequest($response, $authnRequest); |
|
87
|
|
|
|
|
88
|
|
|
$identity = Saml::getInstance()->getProviderIdentity()->findByUserAndProviderOrCreate( |
|
89
|
|
|
$user, |
|
90
|
|
|
$serviceProvider |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
Saml::getInstance()->getProviderIdentity()->save($identity); |
|
94
|
|
|
|
|
95
|
|
|
Factory::send($response, $serviceProvider); |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
//save to session and redirect to login |
|
100
|
|
|
Saml::getInstance()->getSession()->setAuthnRequest($authnRequest); |
|
101
|
|
|
|
|
102
|
|
|
\Craft::$app->user->setReturnUrl( |
|
103
|
|
|
UrlHelper::actionUrl( |
|
104
|
|
|
Saml::getInstance()->getHandle() . '/login/after-login' |
|
105
|
|
|
) |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$this->redirect( |
|
109
|
|
|
Craft::$app->config->general->getLoginPath() |
|
110
|
|
|
); |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
public function actionAfterLogin() |
|
115
|
|
|
{ |
|
116
|
|
|
|
|
117
|
2 |
|
if (! $authnRequest = Saml::getInstance()->getSession()->getAuthnRequest()) { |
|
118
|
2 |
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// Clear the session |
|
122
|
|
|
Saml::getInstance()->getSession()->remove(); |
|
123
|
|
|
|
|
124
|
|
|
if (! $user = \Craft::$app->getUser()->getIdentity()) { |
|
|
|
|
|
|
125
|
|
|
throw new HttpException('Unknown Identity.'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// load our container |
|
129
|
|
|
Saml::getInstance()->loadSaml2Container(); |
|
130
|
|
|
|
|
131
|
|
|
/** @var ProviderRecord $serviceProvider */ |
|
132
|
|
|
$serviceProvider = Saml::getInstance()->getProvider()->findByEntityId( |
|
133
|
|
|
MessageHelper::getIssuer($authnRequest->getIssuer()) |
|
134
|
|
|
)->one(); |
|
135
|
|
|
|
|
136
|
|
|
$identityProvider = Saml::getInstance()->getProvider()->findOwn(); |
|
137
|
|
|
|
|
138
|
|
|
$response = Saml::getInstance()->getResponse()->create( |
|
139
|
|
|
$user, |
|
140
|
|
|
$identityProvider, |
|
141
|
|
|
$serviceProvider, |
|
142
|
|
|
Saml::getInstance()->getSettings(), |
|
143
|
|
|
$authnRequest |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
Saml::getInstance()->getResponse()->finalizeWithAuthnRequest($response, $authnRequest); |
|
147
|
|
|
|
|
148
|
|
|
Factory::send($response, $serviceProvider); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function actionRequest($uid) |
|
152
|
|
|
{ |
|
153
|
|
|
//build uid condition |
|
154
|
|
|
$uidCondition = [ |
|
155
|
|
|
'uid' => $uid, |
|
156
|
|
|
]; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @var ProviderRecord $sp |
|
160
|
|
|
*/ |
|
161
|
|
|
if (! $serviceProvider = Saml::getInstance()->getProvider()->findBySp( |
|
162
|
|
|
$uidCondition |
|
163
|
|
|
)->one() |
|
164
|
|
|
) { |
|
165
|
|
|
throw new InvalidMetadata('IDP Metadata Not found!'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if ($user = Craft::$app->getUser()->getIdentity()) { |
|
|
|
|
|
|
169
|
|
|
$identityProvider = Saml::getInstance()->getProvider()->findOwn(); |
|
170
|
|
|
|
|
171
|
|
|
//create response and send back to the sp |
|
172
|
|
|
$response = Saml::getInstance()->getResponse()->create( |
|
173
|
|
|
$user, |
|
174
|
|
|
$identityProvider, |
|
175
|
|
|
$serviceProvider, |
|
176
|
|
|
Saml::getInstance()->getSettings() |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
$response->setRelayState($this->getRelayState()); |
|
180
|
|
|
|
|
181
|
|
|
$identity = Saml::getInstance()->getProviderIdentity()->findByUserAndProviderOrCreate( |
|
182
|
|
|
$user, |
|
183
|
|
|
$serviceProvider |
|
184
|
|
|
); |
|
185
|
|
|
|
|
186
|
|
|
Saml::getInstance()->getProviderIdentity()->save($identity); |
|
187
|
|
|
|
|
188
|
|
|
Factory::send($response, $serviceProvider); |
|
189
|
|
|
return; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
//save to session and redirect to login |
|
193
|
|
|
\Craft::$app->user->setReturnUrl( |
|
194
|
|
|
\Craft::$app->request->getAbsoluteUrl() |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
|
|
$this->redirect( |
|
198
|
|
|
Craft::$app->config->general->getLoginPath() |
|
199
|
|
|
); |
|
200
|
|
|
return; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function getRelayState(): string |
|
207
|
|
|
{ |
|
208
|
|
|
return \Craft::$app->request->getParam('RelayState') ?? ''; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: