1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\autotest\Controller; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\Auth; |
8
|
|
|
use SimpleSAML\Configuration; |
9
|
|
|
use SimpleSAML\Error; |
10
|
|
|
use SimpleSAML\Session; |
11
|
|
|
use SimpleSAML\Utils; |
12
|
|
|
use SimpleSAML\XHTML\Template; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Controller class for the autotest module. |
18
|
|
|
* |
19
|
|
|
* This class serves the different views available in the module. |
20
|
|
|
* |
21
|
|
|
* @package simplesamlphp/simplesamlphp-module-autotest |
22
|
|
|
*/ |
23
|
|
|
class Autotest |
24
|
|
|
{ |
25
|
|
|
/** @var \SimpleSAML\Configuration */ |
26
|
|
|
protected Configuration $config; |
27
|
|
|
|
28
|
|
|
/** @var \SimpleSAML\Session */ |
29
|
|
|
protected Session $session; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \SimpleSAML\Auth\Simple|string |
33
|
|
|
* @psalm-var \SimpleSAML\Auth\Simple|class-string |
34
|
|
|
*/ |
35
|
|
|
protected $authSimple = Auth\Simple::class; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Controller constructor. |
40
|
|
|
* |
41
|
|
|
* It initializes the global configuration and session for the controllers implemented here. |
42
|
|
|
* |
43
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
44
|
|
|
* @param \SimpleSAML\Session $session The session to use by the controllers. |
45
|
|
|
* |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
Configuration $config, |
50
|
|
|
Session $session |
51
|
|
|
) { |
52
|
|
|
$this->config = $config; |
53
|
|
|
$this->session = $session; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Inject the \SimpleSAML\Auth\Simple dependency. |
59
|
|
|
* |
60
|
|
|
* @param \SimpleSAML\Auth\Simple $authSimple |
61
|
|
|
*/ |
62
|
|
|
public function setAuthSimple(Auth\Simple $authSimple): void |
63
|
|
|
{ |
64
|
|
|
$this->authSimple = $authSimple; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test attributes. |
70
|
|
|
* |
71
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
72
|
|
|
* |
73
|
|
|
* @return \SimpleSAML\XHTML\Template |
74
|
|
|
*/ |
75
|
|
|
public function attributes(Request $request): Template |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
$as = $this->getAuthSource($request); |
79
|
|
|
if (!$as->isAuthenticated()) { |
80
|
|
|
throw new Error\Exception('Not authenticated.'); |
81
|
|
|
} |
82
|
|
|
} catch (Error\Exception $e) { |
83
|
|
|
return $this->sendFailure($e); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$attributes = $as->getAttributes(); |
87
|
|
|
return $this->sendSuccess($attributes); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test login. |
93
|
|
|
* |
94
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
95
|
|
|
* |
96
|
|
|
* @return \SimpleSAML\XHTML\Template |
97
|
|
|
*/ |
98
|
|
|
public function login(Request $request): Template |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$as = $this->getAuthSource($request); |
102
|
|
|
} catch (Error\Exception $e) { |
103
|
|
|
return $this->sendFailure($e); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!$as->isAuthenticated()) { |
107
|
|
|
$as->requireAuth(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->sendSuccess(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Test logout. |
116
|
|
|
* |
117
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
118
|
|
|
* |
119
|
|
|
* @return \SimpleSAML\XHTML\Template |
120
|
|
|
*/ |
121
|
|
|
public function logout(Request $request): Template |
122
|
|
|
{ |
123
|
|
|
try { |
124
|
|
|
$as = $this->getAuthSource($request); |
125
|
|
|
} catch (Error\Exception $e) { |
126
|
|
|
return $this->sendFailure($e); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($as->isAuthenticated()) { |
130
|
|
|
$as->logout(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->sendSuccess(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the AuthSource given by the SourceID parameter from the request |
139
|
|
|
* |
140
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
141
|
|
|
* @return \SimpleSAML\Auth\Simple |
142
|
|
|
* |
143
|
|
|
* @throws \SimpleSAML\Error\BadRequest if SourceID is not part of the query parameters |
144
|
|
|
* |
145
|
|
|
*/ |
146
|
|
|
private function getAuthSource(Request $request): Auth\Simple |
147
|
|
|
{ |
148
|
|
|
$sourceId = $request->get('SourceID', null); |
149
|
|
|
|
150
|
|
|
if ($sourceId === null) { |
151
|
|
|
throw new Error\BadRequest('Missing required SourceID query parameter.'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return new $this->authSimple($sourceId); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Generate a response for success |
160
|
|
|
* |
161
|
|
|
* @param array $attributes The attributes to include in the response |
162
|
|
|
* @return \SimpleSAML\XHTML\Template |
163
|
|
|
* |
164
|
|
|
*/ |
165
|
|
|
private function sendSuccess(array $attributes = []): Template |
166
|
|
|
{ |
167
|
|
|
$t = new Template($this->config, 'autotest:success.twig'); |
168
|
|
|
|
169
|
|
|
$t->headers->set('Content-Type', 'text/plain; charset=utf-8'); |
170
|
|
|
$t->data['attributes'] = $attributes; |
171
|
|
|
|
172
|
|
|
return $t; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Generate a response for failure |
178
|
|
|
* |
179
|
|
|
* @param \SimpleSAML\Error\Exception $e The exception that was raised |
180
|
|
|
* @return \SimpleSAML\XHTML\Template |
181
|
|
|
* |
182
|
|
|
*/ |
183
|
|
|
private function sendFailure(Error\Exception $e): Template |
184
|
|
|
{ |
185
|
|
|
$t = new Template($this->config, 'autotest:failure.twig'); |
186
|
|
|
|
187
|
|
|
$t->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); |
188
|
|
|
$t->headers->set('Content-Type', 'text/plain; charset=utf-8'); |
189
|
|
|
$t->data['message'] = $e->getMessage(); |
190
|
|
|
|
191
|
|
|
return $t; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|