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