|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace flipbox\saml\idp\services\messages; |
|
4
|
|
|
|
|
5
|
|
|
use craft\base\Component; |
|
6
|
|
|
use craft\elements\User; |
|
7
|
|
|
use flipbox\saml\core\exceptions\AccessDenied; |
|
8
|
|
|
use flipbox\saml\core\helpers\MessageHelper; |
|
9
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
|
10
|
|
|
use flipbox\saml\core\services\bindings\Factory; |
|
11
|
|
|
use flipbox\saml\idp\events\ResponseEvent; |
|
12
|
|
|
use flipbox\saml\idp\models\Settings; |
|
13
|
|
|
use flipbox\saml\idp\records\ProviderRecord; |
|
14
|
|
|
use flipbox\saml\idp\records\ProviderRecord as Provider; |
|
15
|
|
|
use flipbox\saml\idp\Saml; |
|
16
|
|
|
use SAML2\AuthnRequest as SamlAuthnRequest; |
|
17
|
|
|
use SAML2\Constants; |
|
18
|
|
|
use SAML2\Response as ResponseMessage; |
|
19
|
|
|
use SAML2\XML\saml\Issuer; |
|
20
|
|
|
use yii\base\Event; |
|
21
|
|
|
|
|
22
|
|
|
class Response extends Component |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
const CONSENT_IMPLICIT = Constants::CONSENT_IMPLICIT; |
|
26
|
|
|
const EVENT_AFTER_MESSAGE_CREATED = 'eventAfterMessageCreated'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param User $user |
|
30
|
|
|
* @param SamlAuthnRequest $authnRequest |
|
31
|
|
|
* @param Provider $identityProvider |
|
32
|
|
|
* @param Provider $serviceProvider |
|
33
|
|
|
* @param Settings $settings |
|
34
|
|
|
* @return ResponseMessage |
|
35
|
|
|
* @throws \Exception |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function create( |
|
38
|
|
|
User $user, |
|
39
|
|
|
Provider $identityProvider, |
|
40
|
|
|
Provider $serviceProvider, |
|
41
|
|
|
Settings $settings, |
|
42
|
|
|
SamlAuthnRequest $authnRequest = null |
|
43
|
|
|
) { |
|
44
|
|
|
// Check Conditional login on the user |
|
45
|
2 |
|
if (! $this->isAllowed($user, $serviceProvider)) { |
|
46
|
2 |
|
throw new AccessDenied( |
|
47
|
2 |
|
sprintf( |
|
48
|
2 |
|
'Entity (%s) Access denied for user %s', |
|
49
|
2 |
|
$serviceProvider->getEntityId(), |
|
50
|
2 |
|
$user->username |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
2 |
|
$response = $this->createGeneral($identityProvider, $serviceProvider); |
|
57
|
|
|
|
|
58
|
2 |
|
Saml::getInstance()->getResponseAssertion()->create( |
|
59
|
2 |
|
$user, |
|
60
|
2 |
|
$response, |
|
61
|
2 |
|
$identityProvider, |
|
62
|
2 |
|
$serviceProvider, |
|
63
|
2 |
|
$settings, |
|
64
|
2 |
|
$authnRequest |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
2 |
|
$response->setSignatureKey( |
|
69
|
2 |
|
$identityProvider->keychainPrivateXmlSecurityKey() |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
2 |
|
$response->setCertificates( |
|
73
|
|
|
[ |
|
74
|
2 |
|
$identityProvider->keychain->getDecryptedCertificate(), |
|
75
|
|
|
] |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Kick off event here so people can manipulate this object if needed |
|
81
|
|
|
*/ |
|
82
|
2 |
|
$event = new ResponseEvent(); |
|
83
|
2 |
|
$event->response = $response; |
|
84
|
2 |
|
$event->user = $user; |
|
85
|
2 |
|
$this->trigger(static::EVENT_AFTER_MESSAGE_CREATED, $event); |
|
86
|
|
|
|
|
87
|
2 |
|
return $response; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param SamlAuthnRequest $authnRequest |
|
92
|
|
|
* @param Provider $identityProvider |
|
93
|
|
|
* @return ResponseMessage |
|
94
|
|
|
* @throws \Exception |
|
95
|
|
|
*/ |
|
96
|
2 |
|
protected function createGeneral( |
|
97
|
|
|
Provider $identityProvider, |
|
98
|
|
|
Provider $serviceProvider |
|
99
|
|
|
) { |
|
100
|
|
|
|
|
101
|
2 |
|
$acsService = $serviceProvider->firstSpAcsService( |
|
102
|
2 |
|
Constants::BINDING_HTTP_POST |
|
103
|
2 |
|
) ?? $serviceProvider->firstSpAcsService(); |
|
104
|
2 |
|
$response = new ResponseMessage(); |
|
105
|
2 |
|
$issuer = new Issuer(); |
|
106
|
2 |
|
$issuer->setFormat(Constants::NAMEID_ENTITY); |
|
107
|
2 |
|
$issuer->setValue($identityProvider->getEntityId()); |
|
108
|
2 |
|
$response->setIssuer( |
|
109
|
2 |
|
$issuer |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
2 |
|
$response->setId($requestId = MessageHelper::generateId()); |
|
113
|
2 |
|
$response->setDestination( |
|
114
|
2 |
|
$acsService->getLocation() |
|
115
|
|
|
); |
|
116
|
2 |
|
$response->setConsent(static::CONSENT_IMPLICIT); |
|
117
|
2 |
|
$response->setStatus( |
|
118
|
|
|
[ |
|
119
|
2 |
|
'Code' => Constants::STATUS_SUCCESS, |
|
120
|
|
|
'Message' => Constants::STATUS_SUCCESS, |
|
121
|
|
|
] |
|
122
|
|
|
); |
|
123
|
2 |
|
$response->setIssueInstant( |
|
124
|
2 |
|
(new \DateTime())->getTimestamp() |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
2 |
|
return $response; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Utils |
|
133
|
|
|
*/ |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param ResponseMessage $response |
|
137
|
|
|
* @param SamlAuthnRequest $authnRequest |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function finalizeWithAuthnRequest(ResponseMessage $response, SamlAuthnRequest $authnRequest) |
|
140
|
|
|
{ |
|
141
|
2 |
|
$response->setInResponseTo( |
|
142
|
2 |
|
$authnRequest->getId() |
|
143
|
|
|
); |
|
144
|
2 |
|
$response->setRelayState( |
|
145
|
2 |
|
$authnRequest->getRelayState() |
|
146
|
|
|
); |
|
147
|
2 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param User $user |
|
151
|
|
|
* @param AbstractProvider $serviceProvider |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
2 |
|
protected function isAllowed(User $user, AbstractProvider $serviceProvider): bool |
|
155
|
|
|
{ |
|
156
|
2 |
|
$options = $serviceProvider->getGroupOptions(); |
|
157
|
2 |
|
if ($options->shouldAllowAny()) { |
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
2 |
|
if ($options->shouldAllowNoGroupAssigned($user)) { |
|
162
|
|
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
2 |
|
foreach ($user->getGroups() as $group) { |
|
166
|
2 |
|
if ($options->shouldAllow($group->id)) { |
|
167
|
2 |
|
return true; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
2 |
|
return false; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|