1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace flipbox\saml\core\services\bindings; |
4
|
|
|
|
5
|
|
|
use craft\base\Component; |
6
|
|
|
use flipbox\saml\core\exceptions\InvalidMetadata; |
7
|
|
|
use flipbox\saml\core\helpers\MessageHelper; |
8
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
9
|
|
|
use flipbox\saml\core\records\ProviderInterface; |
10
|
|
|
use SAML2\Constants; |
11
|
|
|
use SAML2\HTTPPost; |
12
|
|
|
use SAML2\HTTPRedirect; |
13
|
|
|
use SAML2\Message as SamlMessage; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Factory |
17
|
|
|
* @package flipbox\saml\core\services\bindings |
18
|
|
|
*/ |
19
|
|
|
class Factory extends Component |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @return SamlMessage |
23
|
|
|
* @throws \Exception |
24
|
|
|
*/ |
25
|
|
|
public static function receive() |
26
|
|
|
{ |
27
|
|
|
$request = \Craft::$app->request; |
28
|
|
|
switch ($request->getMethod()) { |
29
|
|
|
case 'POST': |
30
|
|
|
$binding = new HTTPPost; |
31
|
|
|
break; |
32
|
|
|
case 'GET': |
33
|
|
|
default: |
34
|
|
|
$binding = new HTTPRedirect; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $binding->receive(); |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param SamlMessage $message |
43
|
|
|
* @param ProviderInterface $provider |
44
|
|
|
* @return mixed |
45
|
|
|
* @throws InvalidMetadata |
46
|
|
|
*/ |
47
|
|
|
public static function send(SamlMessage $message, AbstractProvider $provider) |
48
|
|
|
{ |
49
|
|
|
if ($provider->getType() === $provider::TYPE_IDP) { |
50
|
|
|
$binding = static::determineBindingFromIdp($message, $provider); |
51
|
|
|
} else { |
52
|
|
|
$binding = static::determineBindingFromSp($message, $provider); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$binding->send($message); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param SamlMessage $message |
60
|
|
|
* @param AbstractProvider $provider |
61
|
|
|
* @return HTTPPost|HTTPRedirect |
62
|
|
|
*/ |
63
|
|
|
public static function determineBindingFromSp(SamlMessage $message, AbstractProvider $provider) |
64
|
|
|
{ |
65
|
|
|
if (MessageHelper::isRequest($message)) { |
66
|
|
|
|
67
|
|
|
// Get POST by default |
68
|
|
|
$endpoint = $provider->firstSpAcsService( |
69
|
|
|
Constants::BINDING_HTTP_POST |
70
|
|
|
) ?? $provider->firstSpAcsService( |
71
|
|
|
Constants::BINDING_HTTP_REDIRECT |
72
|
|
|
); |
73
|
|
|
$binding = $endpoint->getBinding() == Constants::BINDING_HTTP_POST ? new HTTPPost : new HTTPRedirect; |
74
|
|
|
} else { |
75
|
|
|
|
76
|
|
|
// Get POST by default |
77
|
|
|
$endpoint = $provider->firstSpSloService( |
78
|
|
|
Constants::BINDING_HTTP_POST |
79
|
|
|
) ?? $provider->firstSpSloService( |
80
|
|
|
Constants::BINDING_HTTP_REDIRECT |
81
|
|
|
); |
82
|
|
|
$binding = $endpoint->getBinding() == Constants::BINDING_HTTP_POST ? new HTTPPost : new HTTPRedirect; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
return $binding; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param SamlMessage $message |
91
|
|
|
* @param AbstractProvider $provider |
92
|
|
|
* @return HTTPPost|HTTPRedirect |
93
|
|
|
*/ |
94
|
|
|
public static function determineBindingFromIdp(SamlMessage $message, AbstractProvider $provider) |
95
|
|
|
{ |
96
|
|
|
|
97
|
|
|
if (MessageHelper::isRequest($message)) { |
98
|
|
|
|
99
|
|
|
// Get POST by default |
100
|
|
|
$endpoint = $provider->firstIdpSsoService( |
101
|
|
|
Constants::BINDING_HTTP_POST |
102
|
|
|
) ?? $provider->firstIdpSsoService( |
103
|
|
|
Constants::BINDING_HTTP_REDIRECT |
104
|
|
|
); |
105
|
|
|
$binding = $endpoint->getBinding() == Constants::BINDING_HTTP_POST ? new HTTPPost : new HTTPRedirect; |
106
|
|
|
} else { |
107
|
|
|
|
108
|
|
|
// Get POST by default |
109
|
|
|
$endpoint = $provider->firstSpSloService( |
110
|
|
|
Constants::BINDING_HTTP_POST |
111
|
|
|
) ?? $provider->firstSpSloService( |
112
|
|
|
Constants::BINDING_HTTP_REDIRECT |
113
|
|
|
); |
114
|
|
|
$binding = $endpoint->getBinding() == Constants::BINDING_HTTP_POST ? new HTTPPost : new HTTPRedirect; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $binding; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|