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