1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace flipbox\saml\core\containers; |
5
|
|
|
|
6
|
|
|
use craft\web\Response; |
7
|
|
|
use flipbox\saml\core\AbstractPlugin; |
8
|
|
|
use flipbox\saml\core\EnsureSAMLPlugin; |
9
|
|
|
use flipbox\saml\core\helpers\MessageHelper; |
10
|
|
|
use flipbox\saml\core\helpers\SerializeHelper; |
11
|
|
|
use SAML2\Compat\AbstractContainer; |
12
|
|
|
use flipbox\craft\psr3\Logger; |
13
|
|
|
|
14
|
|
|
class Saml2Container extends AbstractContainer implements EnsureSAMLPlugin |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
const TEMPLATE_PATH = 'saml-core/_components/post-binding-submit.twig'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Psr\Log\LoggerInterface |
21
|
|
|
*/ |
22
|
|
|
protected $logger; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var AbstractPlugin |
27
|
|
|
*/ |
28
|
|
|
protected $plugin; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new SimpleSAMLphp compatible container. |
32
|
|
|
*/ |
33
|
|
|
public function __construct(AbstractPlugin $plugin) |
34
|
|
|
{ |
35
|
|
|
$this->logger = new Logger([ |
36
|
|
|
'category' => 'saml-core', |
37
|
|
|
]); |
38
|
|
|
$this->plugin = $plugin; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getPlugin(): AbstractPlugin |
42
|
|
|
{ |
43
|
|
|
return $this->plugin; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
* @return \Psr\Log\LoggerInterface |
50
|
|
|
*/ |
51
|
|
|
public function getLogger(): \Psr\Log\LoggerInterface |
52
|
|
|
{ |
53
|
|
|
return $this->logger; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function generateId(): string |
62
|
|
|
{ |
63
|
|
|
return MessageHelper::generateId(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function debugMessage($message, $type): void |
72
|
|
|
{ |
73
|
|
|
if ($message instanceof \DOMDocument || $message instanceof \DOMElement) { |
74
|
|
|
$message = $message->ownerDocument->saveXML(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->getLogger()->debug($message, ['type' => $type]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
* @param string $url |
84
|
|
|
* @param array $data |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function redirect($url, $data = []): void |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
$url = SerializeHelper::redirectUrl($url, $data); |
91
|
|
|
|
92
|
|
|
\Craft::$app->response->redirect($url); |
93
|
|
|
|
94
|
|
|
\Craft::$app->end(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
* @param string $url |
101
|
|
|
* @param array $data |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function postRedirect($url, $data = []): void |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
$data['destination'] = $url; |
108
|
|
|
|
109
|
|
|
if (!isset($data['RelayState'])) { |
110
|
|
|
$data['RelayState'] = ''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$view = \Craft::$app->getView(); |
114
|
|
|
$view->setTemplateMode($view::TEMPLATE_MODE_CP); |
115
|
|
|
\Craft::$app->response->data = $view->renderTemplate( |
116
|
|
|
$this->getTemplatePath(), |
117
|
|
|
$data |
118
|
|
|
); |
119
|
|
|
\Craft::$app->response->format = Response::FORMAT_HTML; |
120
|
|
|
\Craft::$app->response->send(); |
121
|
|
|
\Craft::$app->end(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritDoc |
126
|
|
|
*/ |
127
|
|
|
public function getTempDir() : string |
128
|
|
|
{ |
129
|
|
|
$tempDir = CRAFT_STORAGE_PATH . DIRECTORY_SEPARATOR . 'saml2'; |
130
|
|
|
if (!file_exists($tempDir)) { |
131
|
|
|
mkdir($tempDir); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $tempDir; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function writeFile(string $filename, string $data, int $mode = null) : void |
142
|
|
|
{ |
143
|
|
|
if ($mode === null) { |
144
|
|
|
$mode = 660; |
145
|
|
|
} |
146
|
|
|
file_put_contents( |
147
|
|
|
$filename, |
148
|
|
|
$data |
149
|
|
|
); |
150
|
|
|
chmod( |
151
|
|
|
$filename, |
152
|
|
|
$mode |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* SAML Plugin Utils |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function getTemplatePath() |
164
|
|
|
{ |
165
|
|
|
return static::TEMPLATE_PATH; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|